androidgoogle-mapscurrentlocation

Android Google maps get current location not working


I have a problem with obtaining the current user location. Code at first works but after some time it just stops showing the current location. I tried several approaches to this problem but with the same result.

Here's my code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mapscreen);
    
    mapView = (MapView) findViewById(R.id.mapView);
    mapController = mapView.getController();
    

    
    
    String provider = getProvider();
    
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    
    //getLastKnownPoint();
    locationManager.requestLocationUpdates(provider, 0, 0, locationListener);
    drawCurrPositionOverlay();
    
    
}


private LocationListener locationListener = new LocationListener() {
    
    
    @Override
    public void onLocationChanged(Location location) {
        GeoPoint currentPoint = getCurrentPoint(location);
        animateToCurrentPoint(currentPoint);
        currentLocation = location;
        currentGeoPoint = currentPoint;
        
    }
    
    @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
        
    }
    
    
};

public GeoPoint getCurrentPoint (Location location){
    GeoPoint currentPoint = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6));
    return currentPoint;
    
}

public GeoPoint getLastKnownPoint (){
    
    GeoPoint lastKnownPoint = null;
    Location lastKnownLocation = locationManager.getLastKnownLocation(getProvider());
    if(lastKnownLocation != null){
         lastKnownPoint = getCurrentPoint(lastKnownLocation);
    }
    
    return lastKnownPoint;
}

public void animateToCurrentPoint(GeoPoint currentPoint){
    
    mapController.animateTo(currentPoint);
    mapController.setCenter(currentPoint);
    mapController.setZoom(15);
}


public void drawCurrPositionOverlay(){
    List<Overlay> overlays = mapView.getOverlays();
    CustomItemizedOverlay<CustomOverlayItem> itemizedOverlay;
    overlays.remove(currPos);
    Drawable marker = getResources().getDrawable(R.drawable.me);
    currPos = new MapOverlay(marker,mapView);
    GeoPoint drawMyPoint = null;
    
    if(currentGeoPoint==null){
        drawMyPoint = getLastKnownPoint();
    }
    
   else {
        
       drawMyPoint = currentGeoPoint;
   }
    
    OverlayItem overlayitem = new OverlayItem(drawMyPoint, "I'm here ", "wee");
    currPos.addOverlay(overlayitem);
    overlays.add(currPos);
    currPos.setCurrentLocation(currentLocation);
    
    
    
    
}

public String getProvider() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
    criteria.setAccuracy(Criteria.NO_REQUIREMENT);
    String Provider = locationManager.getBestProvider(criteria, true);
    return Provider;
}




@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(getProvider(), 100, 1, locationListener);
  
}
 
@Override
protected void onPause() {
    super.onPause();
  locationManager.removeUpdates(locationListener);
   
}

}

And I also had a problem with the drawCurrPosition (when code "was working") function (which uses custom balloons), method put a marker on the last known location, not on my current location (so marker was always one step back). Function put the marker on the current location only on the opening app.

Please can you help me as I can't see what's wrong with my code


Solution

  • drawCurrPositionOverlay() is called in onCreate() and should really just be called in the onLocationChanged(). That way your overlay is always being updated with the current location instead of containing just the first location.