4

For iOS its showing that we can't use more than 10 waypoints to draw a route (including source and destination waypoints).

for android there is no limit for the waypoints.

I used the following code to draw a route :

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)
    }
}
Sajib Ghosh
  • 410
  • 1
  • 6
  • 17

1 Answers1

2

As far as I have read in Maps SDK for iOS, you can use Use Google Maps iOS SDK along with other APIs to build location-relevant apps and sites such as Google Maps Directions API to calculate directions between locations using an HTTP request. When using Directions API, it was noted that:

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.

  • For the users of the standard API, limit is:

    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.

  • While Google Maps APIs Premium Plan customers get a limit of :

    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.

Solutions given in these SO posts might also help:

Community
  • 1
  • 1
Teyam
  • 7,686
  • 3
  • 15
  • 22