Forum Moderators: phranque

Message Too Old, No Replies

Ideas on how to show listings based on distance from the user

         

csdude55

2:10 am on Apr 6, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There has to be a clever way to do this using Google Maps, but I'm hoping you guys might know of something before I try to invent it myself.

This is for a Classifieds page. People regularly post Yard Sales, and give their physical address.

I would like to set up a feature that allows the current user to enter their own address, then return a listing of the yard sales in order of driving distance to their location. Maybe even with a map...

Thoughts?

NickMNS

2:21 am on Apr 6, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I do this, using straight line distance. In the search fields I provide a distance field, that allows the user to choose between 50 or 100 miles. I'm using MongoDB that provides geospatial indexing so this is really straightforward. Basically I take the users geo-coordinates and find all records that fall within the radius.

Using driving distance makes the wholes thing far more complex because there needs to be a route calculation done and that is not obvious. Try reading through the Google maps API documents to see if they offer something like that.

TorontoBoy

2:43 am on Apr 6, 2018 (gmt 0)

5+ Year Member Top Contributors Of The Month



Consider researching plus.codes [plus.codes...] , something similar but simpler than GPS locations. You might be able to calculate distance between plus codes, but this will be "as the crow flies" and not driving distance.

csdude55

6:59 am on Apr 7, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've been messing with Google Maps API:

[developers.google.com...]

It does give me a driving distance for one listing at a time, but do you guys have any idea how I might get multiple results from a single feed? The API has a limit of 2500 queries per day... we have 50+ yard sales listed at any given time, so if I have to do a different feed for each listing just to get the distance then it would only take 50 people looking at the list to take up all of my quota!

NickMNS

3:01 pm on Apr 7, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I draw you attention to a few statements in the docs, the first being the most notable:
Calculating directions is a time and resource intensive task.

Whenever possible, use the service described here to calculate known addresses ahead of time and store the results in a temporary cache of your own design.

Note: This service is not designed to respond in real time to user input.


Assuming you could do it, comparing the reference address to 50 other locations would take a long time. The only solution, or really a work around, is to reduce the number of comparisons. This can be done by either using the measure as a tie breaker. Rank the locations based on straight-line distance and when two locations are sufficiently close to each other, look-up both locations to find the nearest based on driving time. I suspect that this solution may not work so well in your situation as your location likely to clustered, so you may end with many ties to break. Another model would be only to provide the driving time for the three closest.

As for the 2500 queries limit, this shouldn't be an issue if your able to make the requests client side, using AJAX. Then each request or group of requests would be from a different IP. (note I'm not sure if this possible as it may still require your API key). Otherwise the issue is a real concern, let say 50 API calls for each time a user request the page will leave you 2500/50 = 50 page requests. 10 API calls per request = 250 etc...

The final, and likely easiest solution is to rank the location by straight-line distance and then provide a link for each to show directions.

csdude55

10:51 pm on Apr 8, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Good news, I found this and I think it'll work:

[developers.google.com...]

It only returns distances, but that's enough for my purposes. And since it lets me plug in multiple destinations in one query, I should be able to work within the 2500 quota :-)