onLocationChanged未被调用

时间:2017-04-06 12:35:53

标签: android location locationmanager

我正在尝试在地图上显示当前位置,但未调用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);

1 个答案:

答案 0 :(得分:0)

您是否获得了两种权限

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