如何获得在谷歌地图Android的API V2当前位置? [英] How to get the current location in Google Maps Android API v2?

查看:135
本文介绍了如何获得在谷歌地图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.

推荐答案

在谷歌地图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.

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

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