How to get current location(GPS) after click on radio button in my android app?
i have taken one radio button as 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.....
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..
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
LocProvider locProvider = LocProvider.getInstance();
locProvider.init(this);
locProvider.initializeLocationSearch();
locProvider.setLocationCallbackListener(<crete ownlistener and assign here>);
精彩评论