我试图用下面的代码显示保存在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)


    })
}

}
我不知道如何在地图上标出几个位置。

最佳答案

试试这个代码。。。。

 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)

                }

10-08 08:06