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

前端 未结 13 1146
Happy的楠姐
Happy的楠姐 2020-11-27 12:32

Using

mMap.setMyLocationEnabled(true)

can set the myLocation layer enable.
But the problem is how to get the myLocation when the user

13条回答
  •  情歌与酒
    2020-11-27 13:14

    Ensure that you have turned ON the location services on the device. Else you won't get any location related info.

    This works for me,

        map = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
        map.setMyLocationEnabled(true);
        GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange (Location location) {
               LatLng loc = new LatLng (location.getLatitude(), location.getLongitude());
               map.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));
            }
        };
        map.setOnMyLocationChangeListener(myLocationChangeListener);
    

    }

提交回复
热议问题