Google maps - Android - How to get current user coordinateslocation on demand

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

I have an Activity with a button, I just want the button to get current user location via gps/internet/etc and show a toast with those coordinates. That s all, just check the coords once, and show them. Only and every time I click the button, the coords should be updated and shown.

How can I do this? I m having some trouble understanding LocationManager (if that s what I should be using)

I have these in manifest

"android.permission.INTERNET"
"android.permission.ACCESS_FINE_LOCATION"
"android.permission.ACCESS_NETWORK_STATE"
"android.permission.ACCESS_COARSE_LOCATION"

The logcat only shows Couldn t get connection factory client on app startup. The app shows a mapview at first, and that is working ok, so I don t know if that error is my problem.

I did:

...
LocationManager mlocManager = 

(LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
....
    public class MyLocationListener implements LocationListener
    {    @Override public void onLocationChanged(Location loc)
{           
TextView textView = (TextView) findViewById(R.id.textView2);
textView.setText("Latitud: "+loc.getLatitude()+" , Longitud"+loc.getLongitude());
}
...
}

But it s not changing textview2 value

Answers

Use a global variable in onLocationChanged(Location l) and onClick() off the button you se the value. The value will always be actualized.

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/9114366/android-how-to-get-current-user-coordinates-location-on-demand

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils