【问题标题】:Swift Google Maps get coordinates from jsonSwift Google Maps 从 json 获取坐标
【发布时间】:2015-12-03 07:26:45
【问题描述】:

我正在尝试使用来自 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)

现在我想循环这个结果并首先在地图上显示第一行坐标,当我触摸一个按钮时,我希望它显示下一行。这在某种程度上可能吗?我不知道该怎么做。

【问题讨论】:

  • 将坐标保存到数组中并为其设置索引,然后在按钮操作中,从数组中获取索引处的坐标并添加标记 - 哦并增加索引。
  • 好吧,我会试试的。

标签: ios json xcode swift google-maps


【解决方案1】:

首先,您应该创建两个数组,一个用于 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)

    }

肯定对你有用

【讨论】:

  • 为什么要提出两个数组?
  • 这也不是他想要的......我的评论描述了......他只想要 1 个标记并根据按钮按下来改变它
  • 现在我用这段代码得到一个数组: let locationCoordinates = NSMutableArray(arrayLiteral: object["longitude"], object["latitude"]) 得到这个结果: ( ”2.679148”, ” 39.570355" ) ( ”2.687386”, ”39.570364" ) ( ”2.691598”, ”39.569988" ) ( ”2.695900”, ”39.569756" ) 是否可以使用上面的代码在这个数组中循环?
猜你喜欢
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多