gpt4 book ai didi

ios - 如何将 Google Map 动画到当前标记位置并在 Swift 中绘制线条

转载 作者:行者123 更新时间:2023-12-01 16:07:39 24 4
gpt4 key购买 nike

我正在从 Firestore 获取位置并成功地在谷歌地图上的特定位置显示标记,但是当我在模拟器中运行应用程序时遇到的问题它没有动画到当前标记位置。而且我不知道如何在标记之间绘制路线。请帮忙?

这是快速从 Firestore 获取位置的代码。

for document in snapshot!.documents {
print(document.data())
let marker = GMSMarker()

self.location.append(Location(trackingData: document.data()))

print(self.location)

let latitude = document.data()["Latitude"] ?? 0
print(latitude)
let longitude = document.data()["longitude"] ?? 0
print(longitude)

marker.position = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees , longitude: longitude as! CLLocationDegrees)
marker.map = self.mapView
marker.userData = self.location
marker.icon = UIImage(named: "marker")
print("Data stored in marker \(marker.userData!)")
}

enter image description here

最佳答案

您可以通过 GMSCoordinateBounds 实现此目的

引用:https://stackoverflow.com/a/45169325/8447312

在你的 for 循环之前有一个 bounds 对象:

var bounds = GMSCoordinateBounds()

然后在您的 for 循环中,您将使每个标记的位置包含在边界对象中:
var bounds = GMSCoordinateBounds()

for document in snapshot!.documents {
print(document.data())
let marker = GMSMarker()

self.location.append(Location(trackingData: document.data()))
let latitude = document.data()["Latitude"] ?? 0
let longitude = document.data()["longitude"] ?? 0

marker.position = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees , longitude: longitude as! CLLocationDegrees)
marker.map = self.mapView
marker.userData = self.location
marker.icon = UIImage(named: "marker")

//include position for each marker
bounds = bounds.includingCoordinate(marker.position)
}

//when your loop is completed you included all your marker's coordinate in your bounds now, you need to animate:

mapView.animate(with: GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0)))

我为每边设置了 20 个插图,您可以将其更改为您想要的值。

关于画线试试这个:

在您创建边界的正上方创建一条路径:
let path = GMSMutablePath()

然后在您的 for 循环中将每个标记的位置添加到此路径,就像您为边界所做的那样:
path.add(marker.position)

最后,在为您的 mapView 设置动画之前创建如下方向:
let directions = GMSPolyline(path: path)
directions.strokeWidth = 1.0
directions.map = mapView

整个代码(添加的路径):
var bounds = GMSCoordinateBounds()
var path = GMSMutablePath()

for document in snapshot!.documents {
print(document.data())
let marker = GMSMarker()

self.location.append(Location(trackingData: document.data()))
let latitude = document.data()["Latitude"] ?? 0
let longitude = document.data()["longitude"] ?? 0

marker.position = CLLocationCoordinate2D(latitude: latitude as! CLLocationDegrees , longitude: longitude as! CLLocationDegrees)
marker.map = self.mapView
marker.userData = self.location
marker.icon = UIImage(named: "marker")

bounds = bounds.includingCoordinate(marker.position)
path.add(marker.position)
}

let directions = GMSPolyline(path: path)
directions.strokeWidth = 1.0
directions.map = mapView

mapView.animate(with: GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0)))

关于ios - 如何将 Google Map 动画到当前标记位置并在 Swift 中绘制线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54348104/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com