I am now moving with a project related to Google Map service. I would like to point out my current location in my map. For that, I have got an idea of using itemizedoverlay. I retrieve the current location by the help of Geopoint. But when I'm used the itemizedoverlay in my code, the location cannot be picked up and I am using this in my OnCreate method. If I am moving this to OnLocationChanged method, then GeoPoint(point) will not be picked up by the itemizedoverlay and will end with an error and in the present condition, the app gets crushed down.
MapView mapView;
Location location;
LocationManager locationmanager;
LocationListener locationlistener;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
//locationmanager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// locationlistener = new GPSLocationListener();
// locationmanager.requestLocationUpdates(LocationManager.GPS_PROVIDER 0,0,locationlistener);
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();
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable,this);
OverlayItem overlayitem = new OverlayItem(point,"","" );
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
/* private class GPSLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
if (location != null) {
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();
OverlayItem overlayitem = new OverlayItem(point,"","" );
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
}
} */
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(getBaseContext(),"GPS Disabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(getBaseContext(),"GPS Enabled",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
Can you please suggest me a solution for this?? Thanks In Advance and sorry if I made any mistakes here.
You can use com.google.android.maps.MyLocationOverlay which will automatically get show your location on your map view. It will even update your location when travelling.
Note: you show call enableMyLocation and disableMyLocation on onResume and onPause respectively for better performance.
@Override
protected void onResume() {
super.onResume();
if (locationOverlay != null)
locationOverlay.enableMyLocation();
}
@Override
protected void onPause() {
if (locationOverlay != null)
locationOverlay.disableMyLocation();
super.onPause();
}
EDITED
MyLocationOverlay locationOverlay;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setTraffic(true);
mapView.setBuiltInZoomControls(true);
locationOverlay = new MyLocationOverlay(this, mapView);
List<Overlay> mapOverlays = mapView.getOverlays();
mapOverlays.add(locationOverlay);
}
also add the onResume() and onPause() methods.