Swift Google Maps从json获取坐标

时间:2015-12-03 07:26:46

标签: ios json xcode swift google-maps

我试图用json服务器的坐标显示标记。

我正在使用此代码获取坐标:

    let query = PFQuery(className:"locations")
    query.findObjectsInBackgroundWithBlock {
        (objects:[PFObject]?, error:NSError?) -> Void in
    if error == nil {
            for object in objects! {
                self.long = object["longitude"] as! Double
                self.lat = object["latitude"] as! Double
                print(self.lat, self.long)

            }
        } else {
            print(error)
        }
    }

我从 print(self.lat,self.long)

获得此结果
39.570355 2.679148
39.570364 2.687386
39.569988 2.691598
39.569756 2.695900

我用它来在地图上显示它:

marker.position = CLLocationCoordinate2DMake(self.lat, self.long)

现在我想循环这个结果并首先在地图上显示带坐标的第一行,当我触摸一个按钮时,我希望它显示下一行。这有可能吗?我无法弄清楚如何去做。

1 个答案:

答案 0 :(得分:0)

首先你应该为Latitude创建一个数组,为经度创建另一个数组。从您的示例中获取的纬度和经度值

var latitudeArr : NSMutableArray = NSMutableArray()
var longitudeArr : NSMutableArray = NSMutableArray()

    let lat1 : Double = 63.82869
    let lat2 : Double = 63.828743
    let lat3 : Double = 63.828528
    let lat4 : Double = 63.82862

    latitudeArr = NSMutableArray(array: [lat1,lat2,lat3,lat4])

    let long1 : Double = 20.263622
    let long2 : Double = 20.264524
    let long3 : Double = 20.263274
    let long4 : Double = 20.264034

    longitudeArr = NSMutableArray(array: [long1,long2,long3,long4])

现在你必须运行一个循环来在mapView上设置注释

for index in 0...3 {

    let annotation: MKPointAnnotation = MKPointAnnotation()
    annotation.title = "name"
    let latitude: Double = latitudeArr.objectAtIndex(index) as! Double
    let longitude: Double = longitudeArr.objectAtIndex(index) as! Double
    annotation.coordinate = CLLocationCoordinate2DMake(latitude, longitude)
    self.mapView.addAnnotation(annotation)

    }

肯定会对你有用