android - 如何在 Android 上动态更新 Azure Maps 图层?
问题描述
我正在开发一个 Android 应用程序,其中显示地图并在地图上的某个点上跟踪设备的位置。我将 Azure Maps SDK 用于地图本身,并将 FusedLocationProviderClient(播放服务)用于获取 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 中为数据源提供一个全局变量,以便从应用程序的各个部分轻松访问。
推荐阅读
- reactjs - React 表单选项中的值不变
- django - 为什么要使用form_valid的函数来添加逻辑创建的?
- charts - 有没有办法在 amcharts 中自由移动而不捕捉到图表值?
- javascript - 将 rgb 数字数组转换为十六进制颜色字符串
- google-cloud-firestore - Firestore 读取文档数据
- reactjs - 使用带有 jest 和 react-testing-library 的 next/head 测试 next.js 标题,给出误报
- postgresql - PostgreSQL - 我应该在 Postgresql 11 中使用 EXCLUSIVE LOCK 模式逻辑复制吗?
- ssl - CloudFlare SSL 证书始终无效
- javascript - 我在 JScript 上遇到未经处理的拒绝错误
- node.js - 在节点 js 中使用 socket.io 的聊天应用程序