views:

298

answers:

2

I have a database containing orders and each order has an associated location. Currentl, when a user is logged in ,I am using Bing Maps API to geocode each order location and then compute driving distance to the logged in user. Based on these distances, the user via a dropdownbox can then specify the maximal distances with the results displayed in a gridview. However, with more than 100 orders the process becomes painfully slow. I would appreciate some tips on optimizing either the bing queries, possibly caching the results(so they can be reused without reaccessing the bing maps api) or utilizing Ajax to somehow background process the orders. Thanks.

+1  A: 

I am planning to do something similar in the very near future, so I have a few suggestions, but no actual code to share yet. I hope it is useful.

I expect to store the lat/lon for each item in my db (so it is geocoded only once). To select items within a certain distance from a point, I will calculate the lat/lon numbers which are 'x' miles north/south/east/west of my center point. Then the selection becomes a simple matter of picking records where the lat/lon values fall between the values of my square.

And yes, I know that technically I should use a circle to precisely control the distance, but this is so much easier and faster. If you really need to use a circle for a more precise limit, use this method first, then use more complex calculations to weed out the items outside the circle in the corners.

I am not familiar with Bing's licensing, but if I remember correctly about google, you need to have a paid (commercial) license to store the results of geocoding. And it ain't cheap. So that may negate any value my suggestion might have had :(

Edit; I just read the question a little more carefully, and I see that it is talking about driving miles, not linear miles. So, my answer isn't really applicable, unless you want to use it as a way to narrow down the number of driving distance calculcations you have to do.

Also, on the subject of geocoding and licenses, you could look at geocoder.us which is pretty cheap.

Ray
A: 

What you probably want to do is create a minimum spanning tree assuming you have the same destination location for the user. The MST still is O(V^2), but you're effectively caching many of the shortest paths since a lot of them will reuse the same roads.

Another option is to estimate using linear distances first as a substitute for road miles, but that all depends on what you're sending back to the user.

Good luck !

Grembo