地图方向线未显示在地图

时间:2018-04-11 05:42:38

标签: ios swift api google-maps direction

尝试在用户位置与定义位置之间指定方向线。精确定位是正确的,恰好在所需的位置,但没有'线'将用户位置引导到定义位置的方向。

就像这张图片一样:

enter image description here

和我绘制地图的代码以及解析谷歌地图API JSON:

func DrawDirection(url: String, cord: CLLocationCoordinate2D){
    request(url).responseJSON { response in
        let parsed = try? JSONSerialization.jsonObject(with: response.data!, options: []) as! [String:Any]
        let routes = parsed!["routes"] as! [[String:Any]]
        for route in routes {
            let dictArr = route["legs"] as? [[String:Any]]
            let dict = dictArr![0]["steps"] as? [[String:Any]]
            let start = dict![0]["start_location"] as? [String:Any]
            let lat = start!["lat"] as! Double
            let long = start!["lng"] as! Double
            let dotcoordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat), longitude: CLLocationDegrees(long))
            let routePoly = route["overview_polyline"] as! [String:Any]
            let points = routePoly["points"] as! String
            let line = points
            DispatchQueue.main.async {
                let bounds = GMSCoordinateBounds(coordinate: cord, coordinate: dotcoordinate)
                let update = GMSCameraUpdate.fit(bounds, with: UIEdgeInsetsMake(170, 30, 30, 30))
                self.CinemaLoc.moveCamera(update)
            }
            self.AddingPolyLine(encString: line, dir: cord, dot: dotcoordinate)
        }
    }
}

func AddingPolyLine(encString: String, dir: CLLocationCoordinate2D, dot: CLLocationCoordinate2D){
    let dotpath: GMSMutablePath = GMSMutablePath()
    dotpath.add(CLLocationCoordinate2DMake(dir.latitude, dir.longitude)) //original loc
    dotpath.add(CLLocationCoordinate2DMake(dot.latitude, dot.longitude)) //starting points
    let dotpoly = GMSPolyline(path: dotpath)
    dotpoly.map = self.CinemaLoc
    dotpoly.strokeWidth = 3.0
    let styles:[Any] = [GMSStrokeStyle.solidColor(.blue), GMSStrokeStyle.solidColor(.clear)]
    let lengths:[Any] = [10,5]
    dotpoly.spans = GMSStyleSpans(dotpoly.path!,styles as! 
[GMSStrokeStyle], lengths as! [NSNumber], GMSLengthKind.rhumb)
    let poly = GMSPolyline(path: dotpath)
    poly.strokeWidth = 3.0
    poly.strokeColor = .red
    poly.map = self.CinemaLoc
}

cord是用户位置

一直尝试所有可能的堆栈溢出方式,但没有得到任何更改,有时会有日志说"无法在路径加载优化模型〜/ GoogleMaps.bundle / GMSCacheStorage.momd /"

我的代码有什么问题吗?

2 个答案:

答案 0 :(得分:0)

对于特定路径,路径应仅创建一次,然后您可以向路径添加坐标。你把let dotpath: GMSMutablePath = GMSMutablePath()放在方法调用中就会出错,每次循环都会调用它。

答案 1 :(得分:0)

对不起伙计们,问题解决了。我的网址上放错了地方。并且用于添加折线应该是这样的

let path = GMSMutablePath(fromEncodedPath: "String from overview json")
let poly = GMSPolyline(path: dotpath)
poly.strokeWidth = 3.0
poly.strokeColor = .red
poly.map = self.CinemaLoc

在路线循环上,它应该没有问题,因为谷歌地图api只提供1个路线阵列,但为了更好的练习。它应该不在循环中

谢谢你,编码愉快!