ios - 在 iOS 的谷歌地图中绘制路线是否有航点限制

标签 ios swift google-maps

对于 iOS,它显示我们不能使用超过 10 个航点来绘制路线(包括源航点和目标航点)。

对于安卓系统,航路点没有限制。

我用下面的代码画了一条路线:

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)
    }
}

最佳答案

据我在 Maps SDK for iOS 中所读,您可以使用 Google Maps iOS SDK 和其他 API 来构建与位置相关的应用程序和网站,例如 Google Maps Directions API使用 HTTP 请求计算位置之间的方向。使用 Directions API 时,请注意:

The same daily usage limits apply regardless of how you use the service. The requests per day are calculated as the sum of client-side and server-side queries.

  • 对于标准API的用户,限制是:

    Up to 23 waypoints allowed in each server-side request, or up to 8 waypoints when using the Directions service in the Google Maps JavaScript API.

  • 虽然 Google Maps APIs Premium Plan 客户获得的限制为:

    Up to 23 waypoints allowed in each request, plus the origin and destination, whether client-side or server-side queries. That is, the limit is the same when using the Directions service in the Google Maps JavaScript API.

这些 SO 帖子中给出的解决方案也可能有帮助:

关于ios - 在 iOS 的谷歌地图中绘制路线是否有航点限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40089130/

相关文章:

ios - 在应用程序之间切换时保持用户登录

ios - iPhone 是否支持硬件加速的 AES 加密?

ios - 在 Swift 的旋转过程中是否有类似 didRotateFromInterfaceOrientation 的方法?

android - 如何使用谷歌地图 V2 在 android 中使用 MapView?

google-maps - 追踪手机位置

ios - 调整 UIView 的 subview

ios - 在模拟器上查找保存的文件?

ios - 未找到用于将 Swift 转换为 kotlin 的 Swiftkotlin 命令行

ios - 多个 UICollectionViewCells 内的 UIButton 无法正常工作

google-maps - Google Maps是否具有像HERE Maps这样的Map Tile API?