Question

I'm developing a Here Maps based we application.

The user can upload a .xls spreadsheet file to plan directions. These files can have up to 1000 geographically correct addresses.

If I'm correct, to be able to plan a route with these addresses, I first need to geocode them all, using Here Maps' geocoding service:

function geoCodeUploadedAddresses() {
    for(var i = 0; i< uploadedWaypoints.length; i++) {
     nokia.places.search.manager.geoCode({
     searchTerm :  uploadedWaypoints[i],
     onComplete:  onGeocodeComplete
    }); 
    }

}

function onGeocodeComplete(data, requestStatus, requestId) {
    if (requestStatus == "OK") {        
        console.log(data);
    } else if(requestStatus == "ERROR") {
        console.log("error")
    }
}

The problem with this is that it doesn't return the addresses in the same order they were sent.

For example, the uploadedWaypoints array before geocoding:

  1. 1081 Budapest, Blaha Lujza tér
  2. 2119 Pécel, Lázár Vilmos utca
  3. 1214 Budapest, Szent Imre tér

After geocoding the addresses come back in a completely random order:

Ex.:

  1. 2119 Pécel, Lázár Vilmos utca
  2. 1081 Budapest, Blaha Lujza tér
  3. 1214 Budapest, Szent Imre tér

So how can I geocode all of these addresses while keeping the same order they were in the array?

Was it helpful?

Solution

You can create request using jsoncallback parameter and generate a different callback dynamically for each request so you can match with every request you're doing.

See the documentation:

http://developer.here.com/rest-apis/documentation/geocoder/topics/resource-geocode.html

jsoncallback

xs:string - Specifies the name of a user-defined function used to wrap the JSON response.

Also, 1000 geocoding operations on the client side is a lot to do and it might take a long time. Also there are restrictions associated to the type of key you're using and so you might be facing the limitations in terms of requests you can do on a specific 24h period.

You might be interested in Batch geocoding in the Here for Enterprise offer that would be better in your case.

OTHER TIPS

async library seems to suit your needs.

Here is how you could implement it.

var getGeoCode = function(waypoint, callback) {
    nokia.places.search.manager.geoCode({
        searchTerm :  waypoint,
        onComplete:  function(data, requestStatus, requestId) {
                         if (requestStatus == "OK") {        
                             callback(null, data);
                         } else if(requestStatus == "ERROR") {
                             // You can either call with error to break pending calls or some error value - callback({error : requestStatus});
                             console.log("error");
                             callback(null, data);
                         }
                         // You need to callback on other cases too.
                     }
    });
};


async.map(uploadedWaypoints, 
          getGeoCode,
          function(err, results){
               // results is now an array of geocodes for each address
          }
);

Also quoting from the docs:

... since this function applies the iterator to each item in parallel there is no guarantee that the iterator functions will complete in order, however the results array will be in the same order as the original array.

Just showing you the way how to get ordered results. Ideally, you should look into bulk geocoding services.

HTH.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top