0

Find addresses that are nearby to my location using google maps api

I have an table of companies with addresses which are in various locations around the world. What would be the best way to show the companies as markers on a google map that are within a radius of 50km of from my current location. Is it possible to do this using google maps api?

Community
  • 1
  • 1
user1005319
  • 231
  • 1
  • 4
  • 8

2 Answers2

0

Yes, yes it is. What you need to do is use the Geocoding service to convert those addresses into latitudes and longitudes (assuming you don't already have those). It sounds like you'd then want to use the Geometry Library to calculate the distance between your location and your lat/lngs to see if you need to plot their markers.

duncan
  • 31,401
  • 13
  • 78
  • 99
  • cool. what if I have 1000 addresses and my location is New York. 200 of those addresses are in New York. Is there any way to exclude those unnecessary 800 addresses without having to pass all 1000 to the Geometry Library? – user1005319 May 17 '13 at 10:30
  • You might want to look at the [Distance Matrix API](https://developers.google.com/maps/documentation/distancematrix/). It would also be worthwhile to save your companies' latitudes and longitudes in your DB too – duncan May 17 '13 at 11:02
0

To compute distances between two close points, given their latitude and longitude, the following methodology works well. If (Current_Latitude, Current_Longitude) is your current location, and (Latitude, Longitude) is another location, simply compute as follows:

float LatDiff = Current_Latitude - Latitude;
float LongDiff = Current_Longitude - Longitude;
float CosCurLat = Math.cos(Current_Latitude);
float ConversionFac = 6371000 * Math.PI / 180; // 6371000 is earth radius in metres
float Dist_metres = ConversionFac * Math.sqrt(LatDiff*LatDiff + LongDiff*LongDiff*CosCurLat *CosCurLat );

The basic idea of this method is that it works out how many metres a degree of latitude and longitude is worth at your current location. For implementation, note that the ConversionFac is a constant, so (depending on your language) you can probably set it as a global static constant somewhere. Also, CosCurLat only needs to be calculated once for all the other locations that you're interested in.

Implementation notes: The ConversionFac assumes that the latitudes and longitudes are in degrees. However a lot of cos functions take radians as the argument, so be sure to convert as required.

Stochastically
  • 7,616
  • 5
  • 30
  • 58
  • would i have to use that formula for all 1000 addresses against my current location?isnt there a more efficient way.How many lat or long degrees is a km?couldnt i just do my_location_lat_degrees+(however_many_lat_degrees_is_a_km*50)...is that at all possible? – user1005319 May 17 '13 at 14:49
  • I think that what I've posted is the most efficient way. I've now added some explanation. When you cut it right down, it's a simple formula based on the difference in latitude and the difference in longitude. – Stochastically May 17 '13 at 14:58
  • There is somthing wrong with your formula. These 2 addresses should be about 20km apart and I get the answer 4245km Below is my code: $Current_Latitude = -33.80638590; $Current_Longitude=18.48144890; $Latitude=-33.920; $Longitude=18.43050; $LatDiff= $Current_Latitude- $Latitude; $LongDiff= $Current_Latitude- $Longitude; $CosCurLat = cos($Current_Latitude); $ConversionFac = 6371000 * pi() / 180; // 6371000 is earth radius in metres $Dist_metres = $ConversionFac * sqrt ($LatDiff*$LatDiff+ $LongDiff*$LongDiff*$CosCurLat *$CosCurLat ); echo $Dist_metres/1000; – user1005319 May 18 '13 at 23:50
  • There is somthing wrong with your formula. These 2 addresses should be about 20km apart and I get the answer 4245km Below is my code: $Current_Latitude = -33.80638590; $Current_Longitude=18.48144890; $Latitude=-33.920; $Longitude=18.43050; $LatDiff= $Current_Latitude- $Latitude; $LongDiff= $Current_Latitude- $Longitude; $CosCurLat = cos($Current_Latitude); $ConversionFac = 6371000 * pi() / 180; // 6371000 is earth radius in metres $Dist_metres = $ConversionFac * sqrt ($LatDiff*$LatDiff+ $LongDiff*$LongDiff*$CosCurLat *$CosCurLat ); echo $Dist_metres/1000; – user1005319 May 18 '13 at 23:56
  • Your formula for LongDiff uses Current_Latitude when it should use Current_Longitude. Also beware to make sure that the units for the `cos()` are correct, because some `cos()` functions use radians rather than degrees. In your example, `CosCurLat = 0.830922462`. I get an answer of 13.48km, when the road distance given by google is 18km. On the map, that looks plausible to me. Is any of this useful to you? – Stochastically May 19 '13 at 07:54
  • In which case, it's customary on stackoverflow.com to mark questions as 'accepted' and 'useful'. – Stochastically May 19 '13 at 11:40