首页 > 解决方案 > 如何在 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))
}

我是该领域的新手,文档没有帮助,因此感谢所有帮助。

标签: androidkotlinazure-maps

解决方案


只需清除数据源并向其中添加新功能即可。例如,尝试在位置回调中添加以下内容:

var loc = locationResult.getLastLocation() 

dataSource.clear()
dataSource.add(Feature.fromGeometry(Point.fromLngLat(loc.longitude, loc.latitude)))

您需要在 Activity 中为数据源提供一个全局变量,以便从应用程序的各个部分轻松访问。


推荐阅读