I've used Google Maps API to GeoCode data into Latitude and Longitude fields in SFDC. You then have to use a Google Maps Mashup to display the SFDC records based on SOQL queries. This was before SFDC offered Lat/Lon fields and SOQL calls for distance. Back then you had to use a bounding box algorithm with SOQL to find the records.
I have opensource code here: https://github.com/corycowgill/sfbusradar
You can look at my Dreamforce 2011 Presentation here which has details: http://www.youtube.com/watch?v=wwTdGqOygBQ
Note: Minutes 9:30 - about 14 detail how to use GeoCoding Service and LAt/lon fields on your Object.
Also you can take a look at FindNearby on the AppExchange from Force.com Labs.
You'll have to use a GeoCoding service obviously as everyone else said above, either Google Maps which is free but limited to 2500 GeoCoded Records per Day as well as T&C that requires you to display in Google Map Mashup. My source code and video show you how to get started with GeoCoding. Or you could go and purchase a product like ArrowPointe or a few others on the AppExchange that do just GeoCoding.
Finally, here are the Google GeoCoding Docs: https://developers.google.com/maps/documentation/geocoding/
Good Luck!
The documentation states:
Distance is calculated as a straight line—as the crow flies—regardless of geography and topography between the two points.
In reality Salesforce uses the haversine formula to calculate the great-circle distance between two points.
For any two points on a sphere, the haversine of the central angle between them is given by
where
- haversin is the haversine function:
- d is the distance between the two points (along a great circle of the sphere; see spherical distance),
- r is the radius of the sphere,
- : latitude of point 1 and latitude of point 2
- : longitude of point 1 and longitude of point 2
On the left side of the equals sign d/r is the central angle, assuming angles are measured in radians (note that φ and λ can be converted from degrees to radians by multiplying by π/180 as usual).
Solve for d by applying the inverse haversine (if available) or by using the arcsine (inverse sine) function:
where h is haversin(d/r), or more explicitly:
You can confirm this by creating a custom object with two geolocation fields and a formula field which uses the DISTANCE
formula to calculate the distance between the two locations, and then comparing the results to this haversine calculator.
Here are some examples:
Wellington (-41.31666667, 174.81666667) to Salamanca (40.96666667, -5.5)
- Salesforce: 19,968.02154120 km
- Haversine Calculator: 19970 km (to 4 SF)
London (51.507222, -0.1275) to Tokyo (35.689506, 139.6917)
- Salesforce: 9,558.73252169 km
- Haversine Calculator: 9559 km (to 4 SF)
Moscow (55.75, 37.616667) to Nice (43.7034, 7.2663)
- Salesforce: 2,529.41039443 km
- Haversine Calculator: 2529 km (to 4 SF)
Madrid (40.383333, -3.716667) to Sydney (-33.859972, 151.211111)
- Salesforce: 17,686.13743193 km
- Haversine Calculator: 17690 km (to 4 SF)
Pensacola (30.433333, -87.2) to Asmara (15.333333, 38.933333)
- Salesforce: 12,329.08258719 km
- Haversine Calculator: 12330 km (to 4 SF)
A couple of related posts have since appeared in the Salesforce documentation.
How SOQL Calculates and Compares Distances
The DISTANCE function approximates the haversine, or “great circle,” distance calculation within 0.0002%. This formula assumes that the Earth is a perfect sphere, when in fact it’s an ellipsoid: an irregular one. Errors from this assumption can be up to 0.55% crossing the equator, but are usually below 0.3%, depending on latitude and direction of travel.
The DISTANCE function is fine for calculating the 10 stores closest to a customer’s current location. But don’t fuel your plane for a flight from San Francisco to Sydney based on it.
Blog Post: Geolocation Field Types in Salesforce1 Platform
The distance calculation is based on the haversine formula, which is used in mapping and navigation applications and is illustrated below.
Best Answer
You can use the DISTANCE and GEOLOCATION formula functions to create a custom field that will calculate the distance in miles to a fixed location. Then you can use this field in your Visualforce pages as required.
Failing that, you can do the haversine formula calculation yourself in Apex:
Note that this includes the conversion from kilometres to miles in case you are in one of the three countries in the world that don't use the metric system :)
You may want to work to, say, 4 significant figures, as there are a number of simplifications in this approach.