【问题标题】:How to get current location using googleapiClient then put marker如何使用 googleapiClient 获取当前位置然后放置标记
【发布时间】:2016-05-17 17:17:44
【问题描述】:

我确实想使用下面的代码通过 GoogleApiClient 获取当前位置,

@Override
public void onConnected(@Nullable Bundle bundle) {
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

    if(mLastLocation != null)
    {
        currentLat = mLastLocation.getLatitude();
        currentLon = mLastLocation.getLongitude();
    }else
    {
        Toast.makeText(getApplicationContext(), "Cannot get lat and lon", Toast.LENGTH_SHORT).show();
    }
}

然后我确实想在当前位置放置标记,我的问题是 mLastlocation 仍然为空

@Override
public void onMapReady(GoogleMap googleMap) {
    dGoogleMap = googleMap;

    if(mLastLocation != null)
    {
        MarkerOptions marker = new MarkerOptions()
                .position(new LatLng(currentLat, currentLon))
                .title("My Current Location");

        dGoogleMap.addMarker(marker);
        dGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(currentLat, currentLon), 16));
    }
}

或者这是我的错,误解了异步的流程,或者只是我糟糕的逻辑需要改进。

【问题讨论】:

  • 你的清单中是否有 权限?并在您正在测试的手机中启用位置
  • 我已经将它们放在我的 android 清单中,并在我的手机中启用了位置。没成功
  • 你在 marshmellow 上测试它吗?
  • 不,我在棒棒糖先生@ShashankUdupa 上测试它

标签: android google-maps-markers google-api-client


【解决方案1】:

在您的 onMapReady 方法中添加以下行

dGoogleMap.setMyLocationEnabled(true);

【讨论】:

    猜你喜欢
    • 2011-12-24
    • 1970-01-01
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    相关资源
    最近更新 更多