如何在同一区域附近设置多个注释

时间:2020-11-29 21:20:21

I've been trying to create an app in swift using MapKit. I've managed to create multiple annotations but every time i try to put a annotation near another one it never shows up. This isn't a big with the code because the second annotation showed up as soon i changed it to a further location.

我一直在尝试使用MapKit在swift中创建一个应用程序。我已经设法创建了多个注释,但每当我尝试将注释放在另一个注释附近时它就永远不会出现。这对代码来说不是很大,因为第二个注释会在我将其更改为更远的位置后立即出现。

1 个解决方案

#1


1  

simple way would be adding the annotations in an array and then looping through the array and adding the annotations on the map. Also you need to adjust the zoom level of the map.

简单的方法是在数组中添加注释,然后循环遍历数组并在地图上添加注释。您还需要调整地图的缩放级别。

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)
}

#1


1  

simple way would be adding the annotations in an array and then looping through the array and adding the annotations on the map. Also you need to adjust the zoom level of the map.

简单的方法是在数组中添加注释,然后循环遍历数组并在地图上添加注释。您还需要调整地图的缩放级别。

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)
}