Thursday, September 13, 2007

Distance between two Latitude/Longitude points

So when I asked my friend Kerry (who BTW is the king when it comes to map projections) how to calculate the distance between two lat/lon points ? he pointed me to this link. He followed by saying "This is a good start, but what I have implemented in Java, takes care of all the 'weird' conditions!". This is why he is the king :-). So you can find the AS3 implementation here. And here is a simple MXML calculator.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import com.esri.aws.awx.utils.Utils;
private function onClick() : void
{
var lat1n : Number = Utils.parseDeg(lat1.text);
var lon1n : Number = Utils.parseDeg(lon1.text);

var lat2n : Number = Utils.parseDeg(lat2.text);
var lon2n : Number = Utils.parseDeg(lon2.text);

var dist : Number = Utils.distVincenty( lat1n, lon1n, lat2n, lon2n);
distLabel.text = dist.toFixed(2) + " meter(s)"
}
]]>
</mx:Script>
<mx:Form width="100%" height="100%">
<mx:FormHeading label="Geodesic Distance Calculator"/>
<mx:FormItem label="lat 1:">
<mx:TextInput id="lat1" width="200" text="53 09 02N"/>
</mx:FormItem>
<mx:FormItem label="lon 1:">
<mx:TextInput id="lon1" width="200" text="001 50 40W"/>
</mx:FormItem>
<mx:FormItem label="lat 2:">
<mx:TextInput id="lat2" width="200" text="52 12 19N"/>
</mx:FormItem>
<mx:FormItem label="lon 2:">
<mx:TextInput id="lon2" width="200" text="000 08 33W"/>
</mx:FormItem>
<mx:FormItem label="dist">
<mx:Label id="distLabel"/>
</mx:FormItem>
<mx:FormItem>
<mx:Button
label="Calculate"
click="onClick()"/>
</mx:FormItem>
</mx:Form>
</mx:Application>

2 comments:

Anonymous said...

Calculate Distance Between two cities. Also get the directions for going to one city from another city. Distance also shown on Google maps. Visit Netinfobase.com To view the online Google Maps Distance Calculator

Unknown said...

Thanks for this! :)