【问题标题】:Android: how get from (setMyLocationEnabled) -> longitude and latitude?Android:如何从(setMyLocationEnabled)-> 经度和纬度获取?
【发布时间】:2016-03-24 12:56:54
【问题描述】:

如何从 setMyLocationEnabled 获取纬度和经度?而我的代码是这样的。 我需要用它来放大位置,

    private void setUpMap() {
    mMap.addMarker(new MarkerOptions().position(new LatLng(0,0)).title("Marker"));
    mMap.setMyLocationEnabled(true);

    mMap.animateCamera(CameraUpdateFactory.newLatLng());
    mMap.animateCamera(CameraUpdateFactory.zoomBy(13));
}

【问题讨论】:

标签: android google-maps location currentlocation


【解决方案1】:

为了获得单独的纬度和经度,以下对我有用。

 mMap.setMyLocationEnabled(true);
        double lat = mMap.getMyLocation().getLatitude();
        double longt = mMap.getMyLocation().getLongitude();

【讨论】:

    【解决方案2】:

    Google 地图位置 API 有监听器,您可以使用它获取当前位置。

    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));
        }
    }
    };
    

    然后为地图设置监听器。

    mMap.setOnMyLocationChangeListener(myLocationChangeListener);
    

    【讨论】:

    • setOnMyLocationChangeListener 方法现在已弃用。您可以改用 FusedLocationProviderApi。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2012-06-30
    • 2021-02-12
    • 1970-01-01
    相关资源
    最近更新 更多