views:

108

answers:

1

would like to find all the nearby places for a given place or address. Is there any google map api ? or we need to follow a sequence of steps like

  1. Find the lat long of the place.

  2. Do some processing and get near by lat longs.

  3. do reverse geo coding with the obtained lat longs from step 2 and find the places for each lat long pairs.

clarify me. Also if you can elobrate step 2 that would be great.

Will this approach works? can you clarify on this.

+1  A: 

It really depends if the Nearby things you are looking for are in the Google database as places of interest, or if they are arbitrary addresses / locations.

If they are addresses / locations, something similar to what you specify is correct.

  1. Geocode all the places you want to include and store them in a DB (with Lat/Lon)
  2. Geocode the place you are at (where you want information for)
  3. Use a maths routine for distance on a sphere to list the places within a specified distance

The Maths is:

  1. Get the current latitude and longitude in Radians (LatOrLon * PI / 180)
  2. Get the target latitude and longitude in Radians (LatOrLon * PI / 180)
  3. Get the Radius of your sphere (in this case earth = 3963.1 Miles)
  4. Use the long complex and confusing forumla

Below (where Lat1 and Lon1 are your current location in Radians and Lat2 and Lon2 are your target in Radians):

Acos(
    Cos(Lat1) * Cos(Lon1) * Cos(Lat2) * Cos(Lon2) + 
    Cos(Lat1) * Sin(Lon1) * Cos(Lat2) * Sin(Lon2) + 
    Sin(Lat1) * Sin(Lat2)
    ) * EarthRadiusInMiles

This can be done using an SQL Function (MSSQL, but you can easily code this logic into most other languages).

CREATE FUNCTION [dbo].[CoordinateDistanceMiles](
@Latitude1 float,
@Longitude1 float,
@Latitude2 float,
@Longitude2 float
)
RETURNS float 
AS  
BEGIN

-- SAME LOCATION, RETURN 0
IF @Latitude1 = @Latitude2 AND @Longitude1 = @Longitude2 RETURN 0.00

-- CONSTANTS
DECLARE @EarthRadiusInMiles float;
SET @EarthRadiusInMiles = 3963.1
DECLARE @PI float;
SET @PI = PI();

-- RADIANS conversion
DECLARE @lat1Radians float;
DECLARE @long1Radians float;
DECLARE @lat2Radians float;
DECLARE @long2Radians float;

SET @lat1Radians = @Latitude1 * @PI / 180;
SET @long1Radians = @Longitude1 * @PI / 180;
SET @lat2Radians = @Latitude2 * @PI / 180;
SET @long2Radians = @Longitude2 * @PI / 180;

RETURN Acos(
Cos(@lat1Radians) * Cos(@long1Radians) * Cos(@lat2Radians) * Cos(@long2Radians) + 
Cos(@lat1Radians) * Sin(@long1Radians) * Cos(@lat2Radians) * Sin(@long2Radians) + 
Sin(@lat1Radians) * Sin(@lat2Radians)
) * @EarthRadiusInMiles;
END

Then from any calling query you can sort by the distance, for example:

CREATE PROCEDURE [dbo].[NearbyThings]
(
@Lat float,
@Lon float,
@Num int = 10
)

AS

SET NOCOUNT ON

Select Top (@Num)
'Distance' = Round(dbo.CoordinateDistanceMiles(@Lat, @Lon, Lat, Lon), 1),
ThingId, 
Postcode, 
Lat, 
Lon
From 
Things
Order by
dbo.CoordinateDistanceMiles(@Lat, @Lon, Lat, Lon) ASC
GO
badbod99