【问题标题】:onLocationChanged is not being called未调用 onLocationChanged
【发布时间】:2017-04-06 12:35:53
【问题描述】:

我正在尝试在地图上显示当前位置,但未调用 onLocationChanged。

@Override
public void onLocationChanged(Location location) {
    Log.d("Called", "onLocationChanged");
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Current Position");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
    mMap.addMarker(markerOptions);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
}

这是我的 onCreate 代码

LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);

【问题讨论】:

  • 您是否启用了定位功能?
  • 是的,位置已启用并且还请求许可

标签: android location locationmanager


【解决方案1】:

你是否获得了这两个权限

 requestPermissions({
        android.Manifest.permission.ACCESS_FINE_LOCATION,
        android.Manifest.permission.ACCESS_COARSE_LOCATION,
    }, REQUESTPERM);

并且还覆盖onRequestPermissionsResult 来处理授权/拒绝?

@Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {}

【讨论】:

    猜你喜欢
    • 2017-08-09
    • 2019-02-07
    • 2023-03-13
    • 2015-09-28
    • 2014-11-23
    • 2017-08-27
    • 2012-08-09
    • 2017-10-21
    • 1970-01-01
    相关资源
    最近更新 更多