2017-08-03 81 views
0

我一直在嘗試使用MapKit在swift中創建應用程序。我設法創建了多個註釋,但每次我嘗試將註釋放在另一個註釋附近時,它都不會顯示出來。這對於代碼來說並不是很大,因爲第二個註釋在我將其更改爲更遠的位置後就出現了。如何在同一區域附近設置多個註釋

回答

0

簡單的方法是在數組中添加註釋,然後在數組中循環並在地圖上添加註釋。您還需要調整地圖的縮放級別。

let locations = [ 
    ["title": "New York, NY", "latitude": 40.713054, "longitude": -74.007228], 
    ["title": "Los Angeles, CA", "latitude": 34.052238, "longitude": -118.243344], 
    ["title": "Chicago, IL",  "latitude": 41.883229, "longitude": -87.632398] 
] 

for location in locations { 
    let annotation = MKPointAnnotation() 
    annotation.title = location["title"] as? String 
    annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"] as! Double, longitude: location["longitude"] as! Double) 
    mapView.addAnnotation(annotation) 
} 
+0

我已經這樣做了,我仍然嘗試將位置放在彼此附近,只有其中一個出現。 – Aloush

+0

你可以分享你的代碼@Aloush –

+0

'讓位置= [ [「標題」:「藍山」,「緯度」: - 33.499997,「經度」:150.250000], [「標題」:「猴溪」 「緯度」:-33.499688,「經度」:150.260267], ] 位置在位置{ 讓註解= MKPointAnnotation() annotation.title = location [「title」] as?字符串 annotation.coordinate = CLLocationCoordinate2D(latitude:location [「latitude」] as!Double,longitude:location [「longitude」] as!Double) Map.addAnnotation(annotation) }' – Aloush