如何在 Android 上动态更新 Azure Maps 图层?
How to update Azure Maps layers dynamically on Android?
我正在开发一个 Android 应用程序,其中显示地图并在地图上的一个点上跟踪设备的位置。我将 Azure Maps SDK 用于地图本身,将 FusedLocationProviderClient(Play 服务)用于获取 GPS 坐标。我现在的做法是使用 FusedLocationProviderClient 将我的坐标写入变量,然后在 Azure 中创建地图时再次读取它们。这种方法的问题是它只会在我创建应用程序时显示我的位置,但不会在我四处移动时动态更改它。我想要的是一种在创建后修改地图的方法。这是怎么做到的?
对于某些上下文,这是我用于检索坐标的代码 (Kotlin:)
var location = fusedLocationClient.lastLocation.result
var currentLong : Double = location?.longitude?:0.0
var currentLat : Double = location?.latitude?:0.0
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return
for (location in locationResult.locations) {
currentLat = location.latitude
currentLong = location.longitude
// This is just so I make sure that the values actually change
latitudeView.text = currentLat.toString()
longitudeView.text = currentLong.toString()
task = client.checkLocationSettings(builder.build())
}
}
}
fusedLocationClient.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
这是在地图上写点的代码
mapControl.onReady {map : AzureMap ->
//Create a data source and add it to the map.
val dataSource =
DataSource()
map.sources.add(dataSource)
//Create a list of points.
dataSource.add(Feature.fromGeometry(Point.fromLngLat(currentLong, currentLat)))
//Create a LineString feature and add it to the data source.
//Create a line layer and add it to the map.
map.layers.add(SymbolLayer(dataSource))
}
我是这个领域的新手,文档没有帮助,所以感谢所有帮助。
只需清除数据源并向其中添加新功能。例如,尝试在位置回调中添加以下内容:
var loc = locationResult.getLastLocation()
dataSource.clear()
dataSource.add(Feature.fromGeometry(Point.fromLngLat(loc.longitude, loc.latitude)))
您需要在 Activity 中为数据源设置一个全局变量,以便从应用的各个部分轻松访问。
我正在开发一个 Android 应用程序,其中显示地图并在地图上的一个点上跟踪设备的位置。我将 Azure Maps SDK 用于地图本身,将 FusedLocationProviderClient(Play 服务)用于获取 GPS 坐标。我现在的做法是使用 FusedLocationProviderClient 将我的坐标写入变量,然后在 Azure 中创建地图时再次读取它们。这种方法的问题是它只会在我创建应用程序时显示我的位置,但不会在我四处移动时动态更改它。我想要的是一种在创建后修改地图的方法。这是怎么做到的?
对于某些上下文,这是我用于检索坐标的代码 (Kotlin:)
var location = fusedLocationClient.lastLocation.result
var currentLong : Double = location?.longitude?:0.0
var currentLat : Double = location?.latitude?:0.0
locationCallback = object : LocationCallback() {
override fun onLocationResult(locationResult: LocationResult?) {
locationResult ?: return
for (location in locationResult.locations) {
currentLat = location.latitude
currentLong = location.longitude
// This is just so I make sure that the values actually change
latitudeView.text = currentLat.toString()
longitudeView.text = currentLong.toString()
task = client.checkLocationSettings(builder.build())
}
}
}
fusedLocationClient.requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
这是在地图上写点的代码
mapControl.onReady {map : AzureMap ->
//Create a data source and add it to the map.
val dataSource =
DataSource()
map.sources.add(dataSource)
//Create a list of points.
dataSource.add(Feature.fromGeometry(Point.fromLngLat(currentLong, currentLat)))
//Create a LineString feature and add it to the data source.
//Create a line layer and add it to the map.
map.layers.add(SymbolLayer(dataSource))
}
我是这个领域的新手,文档没有帮助,所以感谢所有帮助。
只需清除数据源并向其中添加新功能。例如,尝试在位置回调中添加以下内容:
var loc = locationResult.getLastLocation()
dataSource.clear()
dataSource.add(Feature.fromGeometry(Point.fromLngLat(loc.longitude, loc.latitude)))
您需要在 Activity 中为数据源设置一个全局变量,以便从应用的各个部分轻松访问。