0

i'm developping an android app which shows in a map the current location for the user.

Few hours later, i have discovered the error, locationManager doesn't actualize the location.

I put the code relative a locationManager:

mapa = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.mapaTapas)).getMap();

String serviceString = Context.LOCATION_SERVICE;
LocationManager locationManager = (LocationManager)getSystemService (serviceString);
String proovedor1 = LocationManager.GPS_PROVIDER;
locationManager.getLastKnownLocation(proovedor1);
String proovedor2 = LocationManager.NETWORK_PROVIDER;
locationManager.getLastKnownLocation(proovedor2);

locationManager.requestLocationUpdates(proovedor1, 5000, 5, new LocationListener()
{
@Override
public void onStatusChanged(String arg0, int arg1,Bundle arg2) { }

@Override
public void onProviderEnabled(String arg0) {}

@Override
public void onProviderDisabled(String arg0) {}

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

locationManager.requestLocationUpdates(proovedor2, 5000, 5, new LocationListener()
{
@Override
public void onStatusChanged(String arg0, int arg1,Bundle arg2) { }

@Override
public void onProviderEnabled(String arg0) {}

@Override
public void onProviderDisabled(String arg0) {}

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

location = locationManager.getLastKnownLocation(towers);

    if(location != null){

        glat = location.getLatitude();
        glon = location.getLongitude();

        Log.i("location", "latitud: "+glat);
        Log.i("location", "latitud: "+glon);

}

cp = new CameraPosition.Builder()
        .target(new LatLng(glat, glon))
        .zoom(15)
        .build();     
    mapa.animateCamera(CameraUpdateFactory.newCameraPosition(cp));

i need to save in glat and glon the current value of the location and the if it's posibble implement some method to refresh it automatically

wiki
  • 299
  • 4
  • 16

1 Answers1

0

There's no need to manually request location updates from LocationManager now that OnMyLocationChangeListener interface was introduced.

You only need to enable the my-location layer (map.setMyLocationEnabled(true)), set the listener (map.setOnMyLocationChangeListener(this)) and do whatever you want with the current location (e.g. animate camera) passed back to you in the corresponding callback (provided by implementing that interface).

Nevermore
  • 995
  • 1
  • 9
  • 11