如何在 Google Maps Android API v2 中获取当前位置? [英] How to get the current location in Google Maps Android API v2?

查看:27
本文介绍了如何在 Google Maps Android API v2 中获取当前位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用

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.

推荐答案

Google Maps 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.

OnMyLocationChangeListener 接口已弃用.使用 com.google.android.gms.location.FusedLocationProviderApi 代替.FusedLocationProviderApi 提供改进的位置查找和电源使用,并由我的位置"蓝点使用.请参阅示例应用程序文件夹中的 MyLocationDemoActivity 以获取示例代码或位置开发人员指南.

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.

这篇关于如何在 Google Maps Android API v2 中获取当前位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆