jjLin jjLin - 1 year ago 207
Android Question

How to get the current location in Google Maps Android API v2?



can set the myLocation layer enable.

But the problem is how to get the myLocation when the user clicks on the button?
I want to get the longitude and latitude.

Answer Source

The Google Maps API location now works, even has listeners, you can do it using that, for example:

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
    public void onMyLocationChange(Location location) {
        LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
        mMarker = mMap.addMarker(new MarkerOptions().position(loc));
        if(mMap != null){
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));

and then set the listener for the map:


This will get called when the map first finds the location.

No need for LocationService or LocationManager at all.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download