gpt4 book ai didi

ios - 使用 'MKMapView' 显示多个位置

转载 作者:行者123 更新时间:2023-11-28 14:01:58 25 4
gpt4 key购买 nike

我正在尝试使用以下代码显示保存在 Mysql 中的多个位置。数据正在加载,但我不知道如何根据纬度和经度显示多个位置。Mysql 通过 PHP 文件连接到应用程序。这是我的代码,我从 NSObject 调用的部分:

func downloadItems() {
// the download function

// return the nsuserdefaults which hold the lati and longi from the notification table
UserDefaults.standard.string(forKey: "test");
let myUrl = UserDefaults.standard.string(forKey: "test");


let urlPath: String = myUrl!
let url: URL = URL(string: urlPath)!
let defaultSession = Foundation.URLSession(configuration: URLSessionConfiguration.default)

let task = defaultSession.dataTask(with: url) { (data, response, error) in

if error != nil {
print("Failed to download data")
}else {
print("Data downloaded")
self.parseJSON(data!)
}

}

task.resume()

}

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

}

locations.add(location)

}

DispatchQueue.main.async(execute: { () -> Void in

self.delegate.itemsDownloaded(items: locations)


})
}

我不知道如何在 map 上显示几个位置。

最佳答案

试试这段代码....

 var locations = NSMutableArray()
var mapView = GMSMapView()

for i in 0..< location.count{
let obj = location[i]
lat = obj["lati"] as? Double
lng = obj["longi"] as? Double
let markerPoint = GMSMarker()
markerPoint.position = CLLocationCoordinate2D(latitude: lat!, longitude: lng!)
markerPoint.iconView = self.avtarImage() // Your image name
markerPoint.map = mapView // your mapview object name
markerPoint.zIndex = Int32(i)
markerPoint.infoWindowAnchor = CGPoint(x: 0, y: 0)
markerPoint.accessibilityLabel = String(format: "%d", i)

}

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

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