I am developing application based on google maps in that current location of user has to display in map. i have google it and find solution but when i run the code it doesnt shows the current location instead it showing google map alone. My code is below
public class GPSLocation extends MapActivity {
private MapView mapView;
private MapController mapController;
private LocationManager locationManager;
private GPSLocationListener locationListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// mapView = (MapView) findViewById(R.id.mapView);
// enable Street view by default
// mapView.setStreetView(true);
// enable to show Satellite view
// mapView.setSatellite(true);
// ena开发者_运维问答ble to show Traffic on map
// mapView.setTraffic(true);
// mapView.setBuiltInZoomControls(true);
// mapController = mapView.getController();
// mapController.setZoom(16);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new GPSLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}
private class GPSLocationListener implements LocationListener
{
public GPSLocationListener() {
// TODO Auto-generated constructor stub
}
@Override
public void onLocationChanged(Location location) {
if (location != null) {
System.out.println("onLocationChanged");
GeoPoint point = new GeoPoint(
(int) (location.getLatitude() * 1E6),
(int) (location.getLongitude() * 1E6));
Toast.makeText(getBaseContext(),
"Latitude: " + location.getLatitude() +
" Longitude: " + location.getLongitude(),
Toast.LENGTH_SHORT).show();
mapController.animateTo(point);
mapController.setZoom(16);
mapView.invalidate();
}
else
{
System.out.println("OnLocationChanged value is null");
}
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
I dont know where i have done mistake.Its not even entering into GPSLocationListener class. please help me.. Thanks in advance.
Check is your gps active. Change min distance and min time. For example:
requestLocationUpdates(LocationManager.GPS_PROVIDER,10,30,locationListener);
Are you testing on a device or emulator? Because on the emulator you have to manually input coordinates.
Also, make sure one of the follwing is in your manifest file:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" />
Recent Google Maps V2 lets you implement OnMyLocationChangeListener, so you don't need to implement (and set) a custom LocationSource.
Please add this line of code also. It worked for me.
LocationManager.requestLocationUpdates(LocationManager.NETWORk_PROVIDER,0,0,locationListener);
If it still doesn't work can you please give the Log Details?
精彩评论