Domanda

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.

È stato utile?

Soluzione

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 ;)

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top