Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

Try with this -

/**
* Setting Google Map to provided location 
*/
 private void setMaptoLocation(Location location) {
  mapView.setBuiltInZoomControls(true);
  mapView.setSatellite(true);
  int LAT = (int)(location.getLatitude() * 1E6) ;
  int LNG = (int)(location.getLongitude() * 1E6) ;
  GeoPoint point = new GeoPoint(LAT, LNG);
  mapController = mapView.getController();
  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);
  mapController.setZoom(ZOOM_LEVEL - ZOOM_LEVEL / 2); // Set zoom level per requierment
  mapController.animateTo(point);

 }

location is no where initialize by you in onCreate() so i guess its null. And one more thing if you got Lat and Long, itemized will surely show location on Map. Check permission required Access_Course_location and Access_fine_location in Manifest.

I think you have to go with the Google Maps Android API v2

For the more information check the Google Maps Android API v2.

please check the link of the sample code from the above link. sample code

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top