我的应用程序成功从Google Directions API获取JSON,然后我扫描它,创建坐标路径,并在地图上创建路线的成功折线。但是,它几乎总是绘制第二条折线,它从直线起点到终点(就像你直接从起点飞到终点一样)
我希望它不画直线!
if let directions = (try? JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers)) as? NSDictionary
{
//print("JSON result: \(directions)")
if let routes = directions["routes"] as? NSArray
{
if let legs = routes.value(forKey: "legs") as? NSArray
{
if let steps = legs.value(forKey: "steps") as? NSArray
{
if steps.count != 0
{
if let firstStep = steps.object(at: 0) as? NSArray
{
if firstStep.count != 0
{
if let paths = firstStep.object(at: 0) as? NSArray
{
if paths.count > 0
{
for i in 0 ..< paths.count
{
if let polyline = (paths[i] as AnyObject).value(forKey: "polyline") as? NSDictionary {
if let points = polyline.value(forKey: "points") as? String {
let gmsPath = GMSPath(fromEncodedPath: points)
for index in 0 ..< gmsPath!.count() {
let coordinate = gmsPath!.coordinate(at: index)
self.path.add(coordinate)
//print("coordinate \(index): \(coordinate)")
}
}
}
}
}
}
}
}
}
}
}
}
}
})
blah = GMSPolyline(path: path)
blah.strokeWidth = 10.0
addPoly(polyline: blah)
blah.map = mapView
抱歉看起来丑陋的代码。任何人都可以看到为什么/我的代码使得直线折线被绘制?谢谢!
编辑:记录所有坐标后:
前几个条目: CLLocationCoordinate2D(纬度:36.272760000000005,经度:-81.602230000000006) CLLocationCoordinate2D(北纬:36.311060000000005,经度:-81.619120000000009) CLLocationCoordinate2D(纬度:36.311140000000002,经度:-81.619060000000005) CLLocationCoordinate2D(纬度:36.311400000000006,经度:-81.618890000000007)
最后几个: CLLocationCoordinate2D(纬度:36.274280000000005,经度:-81.561430000000001) CLLocationCoordinate2D(纬度:36.273980000000002,经度:-81.561780000000013) CLLocationCoordinate2D(纬度:36.273860000000006,经度:-81.56195000000001) CLLocationCoordinate2D(北纬:36.273860000000006,经度:-81.56195000000001)