1

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

Abhinav singh
  • 1,448
  • 1
  • 14
  • 31
hyperN
  • 2,674
  • 9
  • 54
  • 92
  • Refer answer from this question... http://stackoverflow.com/questions/9586530/positing-user-current-location-and-show-it-in-google-maps/9596183#9596183 – Prem Mar 22 '12 at 06:05
  • Thanks, but still not working =( Maybe I did something wrong, but I can't see what, i get error Couldn't get to factory client and I get numbers form time, and nothing else is displayed – hyperN Mar 22 '12 at 11:53

1 Answers1

0

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.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
SoundsDangerous
  • 708
  • 6
  • 13
  • if you hit onPause and then onResume do your location updates resume properly? – SoundsDangerous Mar 22 '12 at 01:34
  • Thank you for helping with drawCurrPositionOverlay(), and no, when i hit onPause and onResume, location isn't updated properly. – hyperN Mar 22 '12 at 09:35
  • And for some strange reason when i restart my phone app works fine again – hyperN Mar 22 '12 at 09:45
  • `public void removeUpdates (LocationListener listener)` Removes any current registration for location updates of the current activity with the given LocationListener. Following this call, updates will no longer occur for this listener. When you resume try creating a 'new' listener and pass that. – SoundsDangerous Mar 24 '12 at 15:39