本文介绍了如何在谷歌地图上绘制合适的多段线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经成功绘制了从a点到b点的多段线,但出于某种原因,它显示的是线性线不正确的路标

I have successfully drawn a polyline from point a to point b, but for some reason , it shows linear line not the correct waypoints

以下是代码

let path = GMSMutablePath()
        path.addLatitude(3.1970044, longitude:101.7389365)
        path.addLatitude(3.2058354, longitude:101.729536)
        let polyline = GMSPolyline(path: path)
        polyline.strokeWidth = 5.0
        polyline.geodesic = true
        polyline.map = mapView

我期待它会做一些航点,但它只显示直线多段线

I was expecting that it would be doing some waypoints, but it just shows straight polylines


推荐答案

对于Swift 3.0
请使用此代码...

For Swift 3.0Please use this code...

func getDirections(origin: String!, destination: String!, waypoints:    Array<String>!, travelMode: AnyObject!, completionHandler: ((_ status:   String, _ success: Bool) -> Void)?) {

        if let originLocation = origin {
            if let destinationLocation = destination {
                var directionsURLString = baseURLDirections + "origin=" + originLocation + "&destination=" + destinationLocation
                if let routeWaypoints = waypoints {
                    directionsURLString += "&waypoints=optimize:true"

                    for waypoint in routeWaypoints {
                        directionsURLString += "|" + waypoint
                    }
                }
                print(directionsURLString)
                directionsURLString = directionsURLString.addingPercentEscapes(using: String.Encoding.utf8)!
                let directionsURL = NSURL(string: directionsURLString)
                DispatchQueue.main.async( execute: { () -> Void in
                    let directionsData = NSData(contentsOf: directionsURL! as URL)
                    do{
                        let dictionary: Dictionary<String, AnyObject> = try JSONSerialization.jsonObject(with: directionsData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as! Dictionary<String, AnyObject>

                        let status = dictionary["status"] as! String

                        if status == "OK" {
                            self.selectedRoute = (dictionary["routes"] as! Array<Dictionary<String, AnyObject>>)[0]
                            self.overviewPolyline = self.selectedRoute["overview_polyline"] as! Dictionary<String, AnyObject>

                            let legs = self.selectedRoute["legs"] as! Array<Dictionary<String, AnyObject>>

                            let startLocationDictionary = legs[0]["start_location"] as! Dictionary<String, AnyObject>
                            self.originCoordinate = CLLocationCoordinate2DMake(startLocationDictionary["lat"] as! Double, startLocationDictionary["lng"] as! Double)

                            let endLocationDictionary = legs[legs.count - 1]["end_location"] as! Dictionary<String, AnyObject>
                            self.destinationCoordinate = CLLocationCoordinate2DMake(endLocationDictionary["lat"] as! Double, endLocationDictionary["lng"] as! Double)

                            let originAddress = legs[0]["start_address"] as! String
                            let destinationAddress = legs[legs.count - 1]["end_address"] as! String

                            let originMarker = GMSMarker(position: self.originCoordinate)
                            originMarker.map = self.mapView
                            originMarker.icon = UIImage(named: "mapIcon")
                            originMarker.title = originAddress

                            let destinationMarker = GMSMarker(position: self.destinationCoordinate)
                            destinationMarker.map = self.mapView
                            destinationMarker.icon = UIImage(named: "mapIcon")
                            destinationMarker.title = destinationAddress

                            if waypoints != nil && waypoints.count > 0 {
                                for waypoint in waypoints {
                                    let lat: Double = (waypoint.components(separatedBy: ",")[0] as NSString).doubleValue
                                    let lng: Double = (waypoint.components(separatedBy: ",")[1] as NSString).doubleValue

                                    let marker = GMSMarker(position: CLLocationCoordinate2DMake(lat, lng))
                                    marker.map = self.mapView
                                    marker.icon = UIImage(named: "mapIcon")

                                }
                            }

                            let route = self.overviewPolyline["points"] as! String

                            let path: GMSPath = GMSPath(fromEncodedPath: route)!
                            let routePolyline = GMSPolyline(path: path)
                            routePolyline.map = self.mapView
                            routePolyline.strokeColor = UIColor(red: 44, green: 134, blue: 200)
                            routePolyline.strokeWidth = 3.0
                        }
                        else {
                            print("status")
                            //completionHandler(status: status, success: false)
                        }
                    }
                    catch {
                        print("catch")

                        // completionHandler(status: "", success: false)
                    }
                })
            }
            else {
                print("Destination is nil.")
                //completionHandler(status: "Destination is nil.", success: false)
            }
        }
        else {
            print("Origin is nil")
            //completionHandler(status: "Origin is nil", success: false)
        }
    }

这篇关于如何在谷歌地图上绘制合适的多段线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 09:21