我正在尝试使用Android设备上的LocationEngine来获取用户位置,但是当我为应用加注星标时,用户的标记位于(0.0,0.0)并且永远不会更新它的位置。 怎么可能?
我尝试复制示例location Layer
private void enableLocationPlugin() {
// Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(this)) {
// Create an instance of LOST location engine
initializeLocationEngine();
LocationLayerOptions options = LocationLayerOptions.builder(this)
.build();
locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine, options);
locationLayerPlugin.setRenderMode(RenderMode.COMPASS);
locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
locationLayerPlugin.setLocationLayerEnabled(true);
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
private void initializeLocationEngine() {
locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.setInterval(1000);
locationEngine.setFastestInterval(500);
locationEngine.addLocationEngineListener(this);
locationEngine.activate();
locationEngine.requestLocationUpdates();
Location lastLocation = locationEngine.getLastLocation();
if (lastLocation != null) {
setCameraPosition(lastLocation);
locationEngine.addLocationEngineListener(this);
} else {
locationEngine.addLocationEngineListener(this);
}
}
private void enableLocationPlugin() {
// Check if permissions are enabled and if not request
if (PermissionsManager.areLocationPermissionsGranted(this)) {
// Create an instance of LOST location engine
initializeLocationEngine();
LocationLayerOptions options = LocationLayerOptions.builder(this)
.build();
locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap, locationEngine, options);
locationLayerPlugin.setRenderMode(RenderMode.COMPASS);
locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
locationLayerPlugin.setLocationLayerEnabled(true);
} else {
permissionsManager = new PermissionsManager(this);
permissionsManager.requestLocationPermissions(this);
}
}
private void initializeLocationEngine() {
locationEngine = new LocationEngineProvider(this).obtainBestLocationEngineAvailable();
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
locationEngine.setInterval(1000);
locationEngine.setFastestInterval(500);
locationEngine.addLocationEngineListener(this);
locationEngine.activate();
locationEngine.requestLocationUpdates();
Location lastLocation = locationEngine.getLastLocation();
if (lastLocation != null) {
setCameraPosition(lastLocation);
locationEngine.addLocationEngineListener(this);
} else {
locationEngine.addLocationEngineListener(this);
}
}
我在grandle中使用这个版本的MapBox
答案 0 :(得分:2)
我有同样的问题。问题是您正在尝试获取高精度位置。高精度应结合使用GPS,蜂窝数据,WiFi等来获得最准确的位置;但由于某种原因,我不知道,它是行不通的。
有四个选项:
NO_POWER
LOW_POWER
BALANCED_POWER_ACCURACY
HIGH_ACCURACY
我更改了以下代码行:
echo $data[69]['passenger_vehicle_lanes']['maximum_lanes'].'<br>';
对此:
locationEngine.setPriority(LocationEnginePriority.HIGH_ACCURACY);
成功了!
答案 1 :(得分:0)
关于您的代码段的几点注释。
LocationLayerOptions options = LocationLayerOptions.builder(this)
.build();
不需要,因为您实际上并未更改任何选项。
locationLayerPlugin.setLocationLayerEnabled(true);
不需要,因为您正在呼叫setRenderMode
正上方自动启用它
https://github.com/mapbox/mapbox-plugins-android/issues/460已经报告了在nullisland(0.0,0.0)处查看用户位置的问题。
对于位置更新,看起来您正在附加一个侦听器。您是否在该侦听器中接收更新,并且位置图层没有更新或者您没有收到任何更新?如果是后者,那么使用locationEngine而不是位置图层插件更容易出现问题。
答案 2 :(得分:0)
抱歉,我没有足够的评论意见。但是,我遇到了类似的麻烦。它只发生在我运行应用程序的第一次。后来,它更新并正常工作。