Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:00:32+00:00 2026-05-31T18:00:32+00:00

I have a problem with obtaining the current user location. Code at first works

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:00:34+00:00Added an answer on May 31, 2026 at 6:00 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The problem: Getting the user's current location within a threshold ASAP and at the
I have a problem obtaining Location header from the WebResponse: private CookieContainer _cookieContainer =
I have problem in some JavaScript that I am writing where the Switch statement
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem when I try insert some data to Informix TEXT column via
I have been looking in google for a solution to my problem but no
I have problem with $_POST data. Some fields are missing from input form when
I have a problem in obtaining the index, if in an array there are
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.