点击我的Android应用程序中的单选按钮后如何获取当前位置(GPS)?

时间:2011-06-22 09:43:36

标签: android

我已经选择了一个单选按钮作为GPS。 现在我想在点击该单选按钮时显示我当前的位置.. 怎么实现呢? 请给我一些相同的示例代码...

提前致谢---

2 个答案:

答案 0 :(得分:4)

在radiobutton onclick事件中使用此代码.....

        Location location;
        protected LocationManager locationManager;

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location != null) {
        String message = String.format(
                        "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                        location.getLongitude(), location.getLatitude());
                Toast.makeText(LbsGeocodingActivity.this, message,
                        Toast.LENGTH_LONG).show();

                List<Overlay> mapOverlays = mapView.getOverlays();
                Drawable drawable = this.getResources().getDrawable(
                        R.drawable.new3pin);
                HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
                        drawable, LbsGeocodingActivity.this);
            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);

        } else {
            Toast.makeText(LbsGeocodingActivity.this, "No Location Found",
                    Toast.LENGTH_LONG).show();
        }

第二个HelloItemizedOverlay.java类..

public class HelloItemizedOverlay extends ItemizedOverlay {

    private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
    Context mContext;

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return mOverlays.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return mOverlays.size();
    }


    public void addOverlay(OverlayItem overlay) {
        mOverlays.add(overlay);
        populate();
    }

    public HelloItemizedOverlay(Drawable defaultMarker, Context context) {
        super(boundCenterBottom(defaultMarker));
        mContext = context;
    }


}

答案 1 :(得分:0)

我已粘贴示例代码Here

Crete一个接口,用于在获得位置时通知..将代码放入单选按钮的OnClick事件(或onSelected)

LocProvider locProvider = LocProvider.getInstance(); locProvider.init(this); locProvider.initializeLocationSearch(); locProvider.setLocationCallbackListener(<crete ownlistener and assign here>);