ios - 使用 'MKMapView' 缩放多个位置

标签 ios swift mkmapview

我使用下面的代码从 MySQL 检索数据,以使用 Swift 在 MKMapView 上显示多个位置。

数据和位置显示在 map 上,但我不知道如何调整缩放以覆盖该区域的所有位置。

 func parseJSON(_ data:Data) {
        var jsonResult = NSArray()

        do {
            jsonResult = try JSONSerialization.jsonObject(with: data, options:JSONSerialization.ReadingOptions.allowFragments) as! NSArray
        } catch let error as NSError {
            print(error)
        }

        var jsonElement = NSDictionary()
        let locations = NSMutableArray()

        for i in 0 ..< jsonResult.count
        {
            jsonElement = jsonResult[i] as! NSDictionary

            let location = LocationModel()

            //the following insures none of the JsonElement values are nil through optional binding
            if let evIdL = jsonElement["id"] as? String,
               let evUserNameL = jsonElement["username"] as? String,
               let evNotikindL = jsonElement["notikind"] as? String,
               let evLatiL = jsonElement["lati"] as? String,
               let evLongiL = jsonElement["longi"] as? String,
               let evLocatL = jsonElement["locat"] as? String,
               let evTimedateL = jsonElement["timedate"] as? String,
               let evDistanceL = jsonElement["distance"] as? String
            {
                location.evId = evIdL
                location.evUsername = evUserNameL
                location.evNotikind = evNotikindL
                location.evLati = evLatiL
                location.evLongi = evLongiL
                location.evLocat = evDistanceL
                location.evTimedate = evTimedateL
                location.evDisatnce = evDistanceL
                location.evLocat = evLocatL

                // the code to show locations
                let latiCon = (location.evLati as NSString).doubleValue
                let longiCon = (location.evLongi as NSString).doubleValue

                  let annotations = locations.map { location -> MKAnnotation in
                     let annotation = MKPointAnnotation()
                     annotation.title = evNotikindL
                     annotation.coordinate = CLLocationCoordinate2D(latitude:latiCon, longitude: longiCon)
                     return annotation
                }

                self.map.showAnnotations(annotations, animated: true)
                self.map.addAnnotations(annotations)
            }

            locations.add(location)
        }

        DispatchQueue.main.async(execute: { () -> Void in
            self.itemsDownloaded(items: locations)
        })
    }

我正在使用 PHP 文件连接 MySQL,正如我所说,代码可以工作并显示位置,但缩放仅集中在一个位置。

最佳答案

你可以试试

DispatchQueue.main.async {
    self.map.addAnnotations(annotations)
    self.map.showAnnotations(annotations, animated: true)
    // make sure itemsDownloaded needs main ??
    self.itemsDownloaded(items: locations)

}

关于ios - 使用 'MKMapView' 缩放多个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53500169/

相关文章:

ios - 表格 View 行高变化时如何添加动画?

objective-c - 类似 Canvas 的元素

ios - 当我执行 segue 到 ViewController 时,圆圈已经填满

cocoa-touch - 像 iOS7 Apple Maps 中的 MKMapView 夜间模式?

objective-c - CLLocation 没有显示准​​确的结果

iphone - 如何在 mkMapview 上显示当前位置的详细信息?

ios - AF网络3问题

ios - UIViewControllers 中的 UIViewController

swift - SpriteKit applyImpulse 崩溃

Swift 在十进制格式中失去精度