Search code examples
androidgoogle-mapsgpsoverlays

can't display my location right on map?


the overlay's did not appear and position is not specified
changed overlay's and map code from onCreat() method to updateOverlays(), i just want to get right location and set overlay's correctly on map

    public class tabsActivity extends MapActivity
    {
    private static final String LIST_TAB_TAG = "Notification";
    private static final String MAP_TAB_TAG = "Map";

    private TabHost tabHost;
    private ListView listView;
    private MapView mapView;
    MyLocationOverlay Compass;
    double longitude , latitude;
    MapController mc;
    GeoPoint point;

    protected LocationManager locationManager;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maps_notification_tabs);

        LocationListener listener = new LocationListener() 
        {

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) 
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderEnabled(String provider) 
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onProviderDisabled(String provider) 
            {
                // TODO Auto-generated method stub

            }

            @Override
            public void onLocationChanged(Location location) 
            {
                updateOverlays(location);
            }
        };

        LocationManager locMgr = (LocationManager) getBaseContext().getSystemService(Context.LOCATION_SERVICE);
        locMgr.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, listener);

        // setup map view
        mapView = (MapView) findViewById(R.id.mapview);


        //Compass Setup
        Compass = new MyLocationOverlay(this, mapView);
        mapView.getOverlays().add(Compass);
        final MapController control = mapView.getController();



    }

    public void updateOverlays(Location location)
    {
        mapView.setBuiltInZoomControls(true);
        mapView.postInvalidate();
        mapView.setSatellite(true);
        mc = mapView.getController();

        point = new GeoPoint((int) location.getLatitude() , (int) location.getLongitude());
        mc.animateTo(point);
        mc.setZoom(10); 
        mapView.invalidate();

        OverlayItem overlayitem = new OverlayItem(point, "Hint", "Your Are Here");

        List<Overlay> mapOverlays = mapView.getOverlays();
        Drawable drawable =   this.getResources().getDrawable(R.drawable.black);
        MapOverlays itemizedoverlay = new MapOverlays(drawable, this);
        itemizedoverlay.addOverlay(overlayitem);
        mapOverlays.add(itemizedoverlay);
    }

Solution

  • Your issue is easy to explain: You only use the location that you got while your program is still in the onCreate() method.

    You need to update your overlay from the listener. So make a separate method that can be called from the LocationListener that updates the overlay.

    Edit:

    basically do that (not complete but should give you the idea!)

    public class MyMapActivity extends MapActivity {
        MapView mapView;
        LocationListener locListener;
    
        public onCreate() {
            // setup your map
            mapView = findViewById(R.id.my_map);
            // setup listener
            locListener = new LocationListener() {
                // override methods
                public void onLocationChanged(Location location) {
                    updateOverlays(location);
                }
            }
        }
    
        public void updateOverlays(Location location) {
            // this is basically your code just a bit modified (removed unnecessary code and added new code)
            mc = mapView.getController();
            p = new GeoPoint(location.getLatitudeE6(), location.getLongitudeE6());
    
            mc.animateTo(p);
            mc.setZoom(10); 
            mapView.invalidate();
    
            // remove all existing overlays!
            List<Overlay> mapOverlays = mapView.getOverlays();
            mapOverlays.clear();
    
            //Compass Setup
            Compass = new MyLocationOverlay(this, mapView);
            mapOverlays.add(Compass);
    
            Drawable drawable = getResources().getDrawable(R.drawable.black);
            MapOverlays itemizedoverlay = new MapOverlays(drawable, this);
    
            OverlayItem overlayitem = new OverlayItem(p, "Hint", "Your Are Here");
            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);
        }
    }
    

    Edit2:

    You have an error in your GeoPoint calculation. You don't give 1E6 integers, you just give some small doubles. Change

    point = new GeoPoint((int) location.getLatitude() , (int) location.getLongitude());
    

    to

    point = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));