gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 05:49:14 24 4
gpt4 key购买 nike

我正在使用下面的代码从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com