如何获得在谷歌地图Android的API V2当前位置?当前位置、如何获得、地图、Android

2023-09-12 01:59:14 作者:眼角眉梢都是你

使用

mMap.setMyLocationEnabled(true)

可以设置myLocation层启用。 但问题是,如何当用户点击按钮获取myLocation? 我想获得的经度和纬度。

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.

推荐答案

在谷歌地图API的位置,现在的工作,甚至有听众,你可以用它做了,例如:

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.

无需LocationService或LocationManager的。

No need for LocationService or LocationManager at all.