【发布时间】:2020-03-07 17:56:53
【问题描述】:
我正在一个带有两个标记的 Android 应用程序中显示 Google 地图。在 onMapReady 方法上,一切正常,地图渲染,标记与相关位图和信息一起出现。
我什至使用带有 LatLngBounds 的 CameraUpdateFactory() 函数实现了缩放边界。同样,到目前为止,一切正常且符合预期。这是sn-p:
override fun onMapReady(googleMap: GoogleMap?) {
myGoogleMap = googleMap
// myMapMarker is a global variable of type MarkerOptions
// the options are set only once here and never changed elsewhere in the code
myMapMarker = MarkerOptions()
.position(LatLng(34.1478, 118.1445))
.title("My Location")
.snippet("Description of my location...")
.icon(BitmapDescriptorFactory.fromBitmap(pinBitmap))
// myMapMarker is added to the map only once here and
// never removed or added again elsewhere in the code
myGoogleMap?.addMarker(myMapMarker)
}
但是,当我尝试使用标记 setPosition 函数 (在 Kotlin 中只是 position) 重新定位标记时,标记位置发生了变化但引脚保持在原来的位置,信息窗口仍然有效。我知道标记位置已更新,因为下一次调用 zoom to bounds 函数会放大到新边界。
// only one attempt is made to move myMapMarker here
// the marker location is successfully changed
// >>> evidenced by println(myMapMarker?.location)
// the marker icon/pin is not re-positioned
myMapMarker?.position(LatLng(mapData.latitude, mapData.longitude))
我了解到标记应该重新定位,而不是删除和再次添加。那么,让标记正确重新定位是否缺少步骤?
我还尝试将 Google Play Maps API 从 17 降级到 16,但仍然得到相同的结果。
TIA。
编辑:添加屏幕截图来说明问题
原始标记位于 452 Ford Place(红色标记)。执行 setPosition() 后,红色标记应出现在蓝色标记所在的位置,但它没有。它位于福特广场 452 号。
【问题讨论】:
-
别针是什么意思?
-
表示可以点击以显示信息窗口的标记的图标。
-
你能提供任何问题的屏幕截图
-
@AbuYousuf 我用截图扩展了我的答案。任何方向将不胜感激。谢谢。
-
我认为您正在重新初始化
myMapMarker以便之前的标记保持不变
标签: android google-maps kotlin google-play-services google-maps-markers