【问题标题】:How to setup multiple annotations near the same area如何在同一区域附近设置多个注释
【发布时间】:2017-08-03 12:15:54
【问题描述】:

我一直在尝试使用 MapKit 快速创建一个应用程序。我设法创建了多个注释,但是每次我尝试将注释放在另一个注释附近时,它都不会出现。这对代码来说并不是什么大问题,因为第二个注释在我将其更改到另一个位置时就出现了。

【问题讨论】:

    标签: swift xcode swift3 mapkit mapkitannotation


    【解决方案1】:

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

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

    【讨论】:

    • 我已经这样做了,我仍然尝试将位置彼此靠近,并且只出现其中一个。
    • let locations = [ ["title": "Blue Mountains", "latitude":-33.499997, "longitude": 150.250000], ["title": "Monkey Creek", "latitude": -33.499688, "longitude": 150.260267], ] 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) Map.addAnnotation(annotation) }
    • 你去吧,我不知道它是否将你标记为原始评论。
    • 请在for循环后面加上这个,看看能不能看到图上的两个大头针let latDelta: CLLocationDegrees = 0.05 let lonDelta: CLLocationDegrees = 0.05 let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta) let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(-33.499997, 150.250000) let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span) Map.setRegion(region, animated: true)
    猜你喜欢
    • 1970-01-01
    • 2014-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-24
    • 2013-07-08
    相关资源
    最近更新 更多