gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 02:13:32 25 4
gpt4 key购买 nike

对于 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com