我一直在尝试使用for循环向谷歌地图添加多个标记,但它不会添加到谷歌地图。添加一个标记有效,或者如果没有for循环为每个标记编写代码。
声明
@IBOutlet weak var googleMapView: GMSMapView! // set in story board
var shopsLatLong = [Double: Double]() // shops dictionary for lat long
for循环不起作用:
for (key,value) in shopsLatLong{
//Setting camera
self.googleMapView.camera = GMSCameraPosition.camera(withLatitude: key, longitude: value, zoom: 6.0)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(key, value)
marker.title = "Store Name"
marker.snippet = "Pakistan"
marker.map = self.googleMapView
print("Marker is \(marker)")
}
如何让这个工作?请帮助我解决这个问题
答案 0 :(得分:1)
不要让class variable
作为标记。您必须为local variable
创建marker
。
如果您创建一个类变量,那么您将只获得一个标记。因为你要覆盖for循环中的值
for (key,value) in shopsLatLong{
//Get the latitude and longitude of the marker and pass it here.
let lat = //latitude for marker
let lon = //longitude for marker
var marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(lat, lon)
marker.title = "Store Name"
marker.snippet = "Pakistan"
marker.map = self.googleMapView
}
答案 1 :(得分:1)
首先创建一个像
这样的数组func setData(){
//create dictionary and store in mai array
var dicMapData : NSMutableDictionary = NSMutableDictionary()
dicMapData["title"] = "Kalasagar Shopping Hub"
dicMapData["image"] = "background9"
dicMapData["subtitle"] = "ShoppingHub"
dicMapData["latitude"] = 23.0669 // store latitude for specigic location
dicMapData["longitude"] = 72.5328 // store longitude for specigic location
arrMultipleAnotation.add(dicMapData)
dicMapData = NSMutableDictionary()
dicMapData["title"] = "Satadhar Cross Road"
dicMapData["image"] = "background8"
dicMapData["subtitle"] = "Crossroad"
dicMapData["latitude"] = 23.0629
dicMapData["longitude"] = 72.5328
arrMultipleAnotation.add(dicMapData)
dicMapData = NSMutableDictionary()
dicMapData["title"] = "Sola Bridge"
dicMapData["image"] = "menu-icon"
dicMapData["subtitle"] = "Bridge"
dicMapData["latitude"] = 23.0695
dicMapData["longitude"] = 72.5232
arrMultipleAnotation.add(dicMapData)
let initialLocation = CLLocation(latitude: 23.0669, longitude: 72.5289)
centerMapOnLocation(location: initialLocation)
//For Displayig Multiple Pin in mapview using array and loop
for locations in arrMultipleAnotation{
//Setting camera
self.googleMapView.camera = GMSCameraPosition.camera(withLatitude: dicCurrent["latitude"] as! Double, longitude: dicCurrent["longitude"] as! Double, zoom: 6.0)
self.marker = GMSMarker()
self.marker.position = CLLocationCoordinate2D(latitude: dicCurrent["latitude"] as! Double, longitude: dicCurrent["longitude"] as! Double)
self.marker.title = dicCurrent["title"] as? String
self.marker.snippet = dicCurrent["subtitle"] as? String
self.marker.map = self.googleMapView
print("Marker is \(self.marker)")
}
我希望它对你有用
答案 2 :(得分:0)
更改此代码..
@IBOutlet weak var googleMapView: GMSMapView! // set in story board
var shopsLatLong = [Double: Double]() // shops dictionary for lat long
for (key,value) in shopsLatLong{
//Setting camera
self.googleMapView.camera = GMSCameraPosition.camera(withLatitude: key, longitude: value, zoom: 6.0)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(key, value)
marker.title = "Store Name"
marker.icon = image
marker.snippet = "Pakistan"
marker.map = self.googleMapView
print("Marker is \(self.marker)")
}
答案 3 :(得分:0)
您应该使用markers
属性,这是一个数组,并将您的标记添加到此数组。这样就可以强烈引用所有标记对象,并且可以轻松使用它们。
此外,字典实际上并不是一个很好的数据结构。我会建议一个struct数组,其中struct具有纬度和经度,甚至是(Double,Double)
的元组数组
@IBOutlet weak var googleMapView: GMSMapView! // set in story board
var shopsLatLong = [(Double,Double)]() // shops dictionary for lat long
var markers = [GMSMarker]()
for (position) in shopsLatLong{
//Setting camera
self.googleMapView.camera = GMSCameraPosition.camera(withLatitude: position.0, longitude: position.1, zoom: 6.0)
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(position.0, position.1)
marker.title = "Store Name"
marker.snippet = "Pakistan"
marker.map = self.googleMapView
self.markers.append(marker)
print("Marker is \(marker)")
}
答案 4 :(得分:0)
我也有类似的问题,我能够弄清楚。实际上我并没有压倒准备segue,按钮也直接链接到故事板,因为我的视图在我获取数据之前首先自我呈现。所以这就是我所做的也许它也适合你。
- 覆盖准备segue并在那里设置变量
- 删除直接与按钮链接的segue链接(如果有)
- 从视图控制器的顶部向新的segue添加新的segue。
- 构建并运行。
醇>
我希望它能为你解决这个问题。