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

前端 未结 13 1163
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:27

    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() {
        @Override
        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:

    mMap.setOnMyLocationChangeListener(myLocationChangeListener);
    

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

    No need for LocationService or LocationManager at all.

    OnMyLocationChangeListener interface is deprecated. use com.google.android.gms.location.FusedLocationProviderApi instead. FusedLocationProviderApi provides improved location finding and power usage and is used by the "My Location" blue dot. See the MyLocationDemoActivity in the sample applications folder for example example code, or the Location Developer Guide.

提交回复
热议问题