我正在尝试显示带有来自 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
我用它在 map 上显示它:
marker.position = CLLocationCoordinate2DMake(self.lat, self.long)
现在我想循环这个结果并首先显示 map 上的第一行坐标,当我触摸按钮时我希望它显示下一行。这在某种程度上可能吗?我不知道该怎么做。
最佳答案
首先,您应该创建两个数组,一个用于 Latitude ,另一个用于 longitude 。从您的示例中获取的纬度和经度值
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)
}
它肯定适合你
关于ios - Swift Google map 从 json 获取坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34060091/