简体   繁体   English

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

[英]How to get current location(GPS) after click on radio button in my android app?

i have taken one radio button as GPS. 我已将一个单选按钮用作GPS。 now i want to show my current location when i clicked on that radio button.. how to achieve it? 现在,我想在单击该单选按钮时显示当前位置。如何实现? please give me some sample code for the same... 请给我一些相同的示例代码...

Thanks in Advance--- 提前致谢 - -

use this code in radiobutton onclick event..... 在单选按钮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();
        }

second HelloItemizedOverlay.java class.. 第二个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;
    }


}

i have pasted the sample code Here 我已经在此处粘贴了示例代码

Crete one interface to notify when location is obtained.. put code in OnClick event(or onSelected) of your Radio button 克里特岛一个界面,通知何时获得位置..将代码放入单选按钮的OnClick事件(或onSelected)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM