简体   繁体   中英

Cannot use loop on NMACoreRouter (swift) (HereMap SDK)

I've an issue that I cannot use for loop on NMACoreRouter (HereMap SDK) If i did it once time by action, it still work!. i dont know why cannot apply into loop. Funny that the error return at the second loop it "NMARoutingError" only

Here is my code

//route
var coreRouter: NMACoreRouter!
var mapRoute: NMAMapRoute?
typealias RouteContainer = (plan: [NMAGeoCoordinates], mode: NMARoutingMode)

 for var shop in pickedShopData {
            
                self.calculateRouteLength(startLat: self.curentUserGeo.latitude, startLong: self.curentUserGeo.longitude, endLat: shop.shopLat, endLong: shop.shopLong)
               
        }

func calculateRouteLength(startLat: Double, startLong: Double, endLat: Double, endLong: Double) {
    
    coreRouter = NMACoreRouter()
    /* Define waypoints for the route */
    let startPoint = NMAGeoCoordinates(latitude: startLat, longitude: startLong)
    let endPoint = NMAGeoCoordinates(latitude: endLat, longitude: endLong)
    /* Initialize a RoutePlan */
    let routePlan = [startPoint, endPoint]
    /*
     * Initialize a RouteOption. HERE Mobile SDK allows users to define their own parameters for the
     * route calculation,including transport modes,route types and route restrictions etc.Please
     * refer to API doc for full list of APIs
     */
    let routeMode = NMARoutingMode()
    /* Other transport modes are also available e.g Pedestrian */
    routeMode.transportMode = NMATransportMode.scooter
    /* Disable highway in this route. */
    routeMode.routingOptions.insert(NMARoutingOption.avoidHighway)
    /* Calculate the shortest route available. */
    routeMode.routingType = NMARoutingType.fastest
    /* Calculate 1 route. */
    routeMode.resultLimit = 3
    
    coreRouter.calculateRoute(withStops: routePlan, routingMode: routeMode,
                              { [self] (result, error) in
            // check error and unwrap route
            guard let route = result?.routes?.first, error == NMARoutingError.none else {
                print("error \(error)")
                return
            }
            // check if map object already exist
            if let tempMapRoute = self.mapRoute {
                self.mapView?.remove(mapObject: tempMapRoute)
            }
            // create map object from route
            guard let mapRoute = NMAMapRoute(route) else {
                return
            }
            print("distance \(mapRoute.route.length)")
    })
    
}

I think your problem is that the NMACoreRouter is still calculating the first route while the second route is started.

Check NMACoreRouter.isBusy to ensure that the router has finished its calculation until you start the next one.

-Karin

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM