gpt4 book ai didi

swift - 无法在 GSMapView xCode Swift 3 中添加多个标记

转载 作者:行者123 更新时间:2023-11-30 12:19:28 25 4
gpt4 key购买 nike

我正在尝试在我的应用中的 Googlemaps 上添加多个标记。

在 viewDidLoad 下的 View Controller 中,我能够加载 map 和单个标记。

override func viewDidLoad() {
super.viewDidLoad()
title = NSLocalizedString("section_map", comment: "test")
let camera = GMSCameraPosition.camera(withLatitude: 48.7784, longitude:9.18121, zoom: 12)
let mapView = GMSMapView.map(withFrame: .zero, camera: camera)


view = mapView
mapView.settings.myLocationButton = true

mapView.delegate = self

let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: 48.7784,longitude: 9.18121)
marker.title = "title"
marker.snippet = "snipple"
marker.icon = UIImage(named:"pin_you")
marker.map = mapView

mapData()
}

它调用mapData()并从那里生成json文件解析后调用setPin来设置标记

  func setPin(){


DispatchQueue.main.async {

for item in self.mapItems {
print (" \(item.name) \(item.marker) \(item.latitude) \(item.longitude)")

let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: item.latitude,longitude: item.longitude)
marker.title = item.name
marker.snippet = item.fulladdress

var iconImage: String
switch (item.marker){
case 1:
iconImage = "pin_silver"
case 2:
iconImage = "pin_blue"
case 3:
iconImage = "pin_gold"
case 6:
iconImage = "pin_you"
default:
iconImage = "pin_silver"
}
marker.icon = UIImage(named:iconImage)
marker.map = self.mapView


}

}
}

未显示引脚。

for item in self.mapItems中的打印显示

Position number A  1 48.76947562 9.15440351
Position number B 1 48.75716485 9.17081058
Position number C 1 48.81191625 9.22752149
Position number D 2 48.81192516 9.22766708

这意味着所有正确的数据都可用。然而, map 是在 viewDidLoad 中制作的一个引脚函数 setPin 中的标记未显示或可能未设置。

有人有想法吗?

最佳答案

我已经解决了这个问题,也更改了 setPin(mapView: GMSMapView!) 的 mapData(mapView: GMSMapView!)

  func setpin(mapView: GMSMapView!){


DispatchQueue.main.async {

for item in self.mapItems {
print (" \(item.name) \(item.marker) \(item.latitude) \(item.longitude)")

let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: item.latitude,longitude: item.longitude)
marker.title = item.name
marker.snippet = item.fulladdress

var iconImage: String
switch (item.marker){
case 1:
iconImage = "pin_silver"
case 2:
iconImage = "pin_blue"
case 3:
iconImage = "pin_gold"
case 6:
iconImage = "pin_you"
default:
iconImage = "pin_silver"
}
marker.icon = UIImage(named:iconImage)
marker.map = mapView


}

}
}

关于swift - 无法在 GSMapView xCode Swift 3 中添加多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44972143/

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