2

Let me explain the project a bit. We have a huge list of stores with addresses. Each one of theses addresses in the database have a geo location.

Now my question is: when a user fills in his address and postal code etc., we have his location and his geo location, is it possible to find the closest store in google maps based on the location of the user. If not through geo location then maybe based on postal code?

I have looked in to google maps api but didn't find something jet to do this. Important is that it searches in the stores we have added to google maps. I rather not use google maps but only the: http://maps.googleapis.com/maps/api/geocode/json?

And if it's possible then we can leave google maps out of it but just searched the database based on geo location that would be even better.

The only question then is how do you do the matching based on geo location or something else? You just check witch one is smaller or bigger or is there more to it then comparing the two?

It would be really nice if somebody could give me a good lead on how to do this.

Iason
  • 372
  • 1
  • 5
  • 20

2 Answers2

2

first of all use a map and ask the user to set his approximate location, get that values, with this code get the distance to each store:

google.maps.geometry.spherical.computeDistanceBetween (latLngA, latLngB);

from https://developers.google.com/maps/documentation/javascript/reference?hl=en-US#spherical

now choose the shorter and output it

but I'd rather ask him to input the City and get the distance through Wolfram api request

 $city=customer_city;
 $north=store_north; //coordinate_l
 $west=store_west; //coordinate_L
 $wolfram_query="http://api.wolframalpha.com/v2/query?appid=".$appid."&input=distance%20from%20".$city."%20to%20".$north."N%20".$west."W%20in%20km&format=image,plaintext";

and get the distance from the xml answer leaving google maps out

an other option using zipcodes is to calculate the spherical distance from two coordinates with this php function inside a while loop going through all the lat and long of the sores:

function calcDist($lat_A, $long_A, $lat_store[i], $long_store[i]) {

$distance = sin(deg2rad($lat_A))
            * sin(deg2rad($lat_B))
            + cos(deg2rad($lat_A))
            * cos(deg2rad($lat_B))
            * cos(deg2rad($long_A - $long_B));

$distance = (rad2deg(acos($distance))) * 69.09;

return $distance;
}  

distance is in miles, if you want the distance in km use this

 function miles2kms($miles) {
 $ratio = 1.609344;
 $kms = $miles * $ratio;
 return $kms;
 }

and you can retrieve the lat and long of the zip code using this database: http://sourceforge.net/projects/zips/files/#files or this http://postcodedb.sourceforge.net/dumpCSV.php

to improve the result of the distance you should use Haversine or Vincenty calculations... which are a little more complex..

... but what we like of the net is that someone has surely done this before we did, and of course shared his efforts! http://www.phpclasses.org/package/6480-PHP-Calculate-the-distance-between-Earth-two-points.html this is a class that implements haversine distance! this might be better... if you want to use Vincenty try this: Implementing Vincenty's Formula in PHP , try which one gives you the best results, even if the most accurate will always be wolfram, mathematicians worked on that, so that works pretty well ;)

Community
  • 1
  • 1
Anze
  • 707
  • 9
  • 15
  • O great this one might even be better, ill definitely look in to this one. Thanks Anze – Iason May 30 '13 at 16:19
  • you welcome! if this solves your problem mark it as the answer, if you need hel, ask! – Anze May 30 '13 at 16:28
  • I am not sure if the wolfram script is going to work because there are not 2 stores, a north and a south like the script does assume. It could contain 1000 stores and from the 1000 stores I need only the closest one. I think you can't place all those vars in one url. – Iason May 30 '13 at 16:34
  • you have to put the coordinates of the each store in a database, or an array, then with a while loop you evaluate the distance to each store from the new location set by the user, then you find the shorter! :) – Anze May 30 '13 at 18:00
  • How accurate is the php code because when I fill in my own geo code and the nearest shop in google maps it show me a distance of almost twice the km in google maps. So google maps says for instance 25km and your code gives me back something like this DISTANCE: 11.345454736866 I understand that it could be smaller because of the radius and it's drawing a direct line from point to point but could it be this big of difference? – Iason May 31 '13 at 08:42
  • Small other question what is the 69.09? – Iason May 31 '13 at 08:48
  • because google maps gives the length of the road to reach the place, if you consider all the times the road goes around an obstacle as a building or a one-way road, so you have to choose another one, the script given there gives the straight distance of two points located on a sphere as big as the earth is, without considering hills, mountains nor roads. The 69 is a a correction factor to get the distance on the sphere(the 69.09 is the radius of the sphere), that formula is very commonly used you can find it everywhere on the web! – Anze May 31 '13 at 18:18
  • By the way Anze, what is the outcome (miles, km, meters)? – Iason Jun 12 '13 at 07:37
0

When you say "Geo location", what specific information do you have about the location? I've done something similar using co-ordinates (lat and long) of an entity, and a large collection of co-ordinates of nearby places of interest. Because lat and long (in decimal) are just...decimals, then it's one equation to calculate which point is closest to the other by finding the smallest difference between both the lat and long values.

This gets much more complex if you want to consider the travel time between the points, for example, but for simple applications, comparing the co-ordinates should be adequate.

dKen
  • 3,078
  • 1
  • 28
  • 37
  • I have all the information about the location, address, postal code, country, city, state and lat and long are added manual through a php script that gets the lat and long with api, I mentioned above. But thanks for the help, i also found this: http://stackoverflow.com/questions/4057665/google-maps-api-v3-find-nearest-markers?lq=1 – Iason May 30 '13 at 16:15
  • That link looks good, especially that they use the haversine formula, which is technically more accurate. Good luck! – dKen May 30 '13 at 16:31