Mapbox 未在 Android 模拟器上显示真实位置
Mapbox not showing real location on Android Emulator
Mapbox 在我的真实设备 (Samsung S8) 上使用 LocationComponent
显示当前位置没有问题,但是我已经尝试了多个模拟器 (Pixel 2, 3, 3XL) Android Pie/Q 并且地图始终显示 Google 总部而不是实际位置:
这是我的代码:
class MapFragment : Fragment(), LocationEngineCallback<LocationEngineResult> {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
mapView = view.findViewById(R.id.mapView)
mapView?.onCreate(savedInstanceState)
mapView?.getMapAsync { mapboxMap ->
mapbox = mapboxMap
mapbox.setStyle(Style.MAPBOX_STREETS) {
style -> showUserLocation(style)
}
}
...
@SuppressWarnings("MissingPermission")
private fun showUserLocation(style: Style){
Log.d(TAG, "showUserLocation")
if (PermissionsManager.areLocationPermissionsGranted(homeActivity)){
val locationComponentOptions = LocationComponentOptions.builder(homeActivity)
.bearingTintColor(Color.WHITE)
.accuracyAlpha(0.1f)
.build()
val locationComponentActivationOptions = LocationComponentActivationOptions
.builder(homeActivity, style)
.locationComponentOptions(locationComponentOptions)
.useDefaultLocationEngine(true)
.build()
locationComponent = mapbox.locationComponent
locationComponent?.activateLocationComponent(locationComponentActivationOptions)
locationComponent?.isLocationComponentEnabled = true
locationComponent?.cameraMode = CameraMode.TRACKING
locationComponent?.renderMode = RenderMode.COMPASS
createLocationEngine()
Toast.makeText(homeActivity, "Show location", Toast.LENGTH_LONG).show()
Log.d(TAG, "Show location")
} else {
Toast.makeText(homeActivity, "Permissions not granted", Toast.LENGTH_LONG).show()
}
}
...
@SuppressWarnings("MissingPermission")
private fun createLocationEngine(){
locationEngine = LocationEngineProvider.getBestLocationEngine(homeActivity)
val request = LocationEngineRequest.Builder(LOCATION_INTERVAL)
.setFastestInterval(LOCATION_INTERVAL)
.setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY)
.setMaxWaitTime(MAX_WAIT_TIME)
.build()
locationEngine?.getLastLocation(this)
}
fragment.xml
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraZoom="@integer/defaultZoom"
mapbox:mapbox_cameraBearing="34.33"
mapbox:mapbox_cameraZoomMax="@integer/defaultZoom"
mapbox:mapbox_cameraZoomMin="@integer/defaultZoom"
mapbox:mapbox_uiRotateGestures="false"
mapbox:mapbox_uiTiltGestures="false"
mapbox:mapbox_uiScrollGestures="false"
mapbox:mapbox_uiDoubleTapGestures="false" />
知道为什么它不能在模拟器上运行吗?
以下是我用过的模拟器:
模拟器使用模拟位置。由于它们仅用于开发目的,因此使用模拟位置很有意义。您可以为模拟器设置任何位置(您的真实位置也是如此)。为此,请点击模拟器控制器按钮底部的三重点 (...)。在打开的 window 上,您可以看到 latitude 和 longitude 字段。参见:
模拟器无法追踪实时位置,但您可以通过 GPS 模拟器插件设置坐标。
Mapbox 在我的真实设备 (Samsung S8) 上使用 LocationComponent
显示当前位置没有问题,但是我已经尝试了多个模拟器 (Pixel 2, 3, 3XL) Android Pie/Q 并且地图始终显示 Google 总部而不是实际位置:
这是我的代码:
class MapFragment : Fragment(), LocationEngineCallback<LocationEngineResult> {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
mapView = view.findViewById(R.id.mapView)
mapView?.onCreate(savedInstanceState)
mapView?.getMapAsync { mapboxMap ->
mapbox = mapboxMap
mapbox.setStyle(Style.MAPBOX_STREETS) {
style -> showUserLocation(style)
}
}
...
@SuppressWarnings("MissingPermission")
private fun showUserLocation(style: Style){
Log.d(TAG, "showUserLocation")
if (PermissionsManager.areLocationPermissionsGranted(homeActivity)){
val locationComponentOptions = LocationComponentOptions.builder(homeActivity)
.bearingTintColor(Color.WHITE)
.accuracyAlpha(0.1f)
.build()
val locationComponentActivationOptions = LocationComponentActivationOptions
.builder(homeActivity, style)
.locationComponentOptions(locationComponentOptions)
.useDefaultLocationEngine(true)
.build()
locationComponent = mapbox.locationComponent
locationComponent?.activateLocationComponent(locationComponentActivationOptions)
locationComponent?.isLocationComponentEnabled = true
locationComponent?.cameraMode = CameraMode.TRACKING
locationComponent?.renderMode = RenderMode.COMPASS
createLocationEngine()
Toast.makeText(homeActivity, "Show location", Toast.LENGTH_LONG).show()
Log.d(TAG, "Show location")
} else {
Toast.makeText(homeActivity, "Permissions not granted", Toast.LENGTH_LONG).show()
}
}
...
@SuppressWarnings("MissingPermission")
private fun createLocationEngine(){
locationEngine = LocationEngineProvider.getBestLocationEngine(homeActivity)
val request = LocationEngineRequest.Builder(LOCATION_INTERVAL)
.setFastestInterval(LOCATION_INTERVAL)
.setPriority(LocationEngineRequest.PRIORITY_HIGH_ACCURACY)
.setMaxWaitTime(MAX_WAIT_TIME)
.build()
locationEngine?.getLastLocation(this)
}
fragment.xml
<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
mapbox:mapbox_cameraZoom="@integer/defaultZoom"
mapbox:mapbox_cameraBearing="34.33"
mapbox:mapbox_cameraZoomMax="@integer/defaultZoom"
mapbox:mapbox_cameraZoomMin="@integer/defaultZoom"
mapbox:mapbox_uiRotateGestures="false"
mapbox:mapbox_uiTiltGestures="false"
mapbox:mapbox_uiScrollGestures="false"
mapbox:mapbox_uiDoubleTapGestures="false" />
知道为什么它不能在模拟器上运行吗?
以下是我用过的模拟器:
模拟器使用模拟位置。由于它们仅用于开发目的,因此使用模拟位置很有意义。您可以为模拟器设置任何位置(您的真实位置也是如此)。为此,请点击模拟器控制器按钮底部的三重点 (...)。在打开的 window 上,您可以看到 latitude 和 longitude 字段。参见:
模拟器无法追踪实时位置,但您可以通过 GPS 模拟器插件设置坐标。