SitePoint Sponsor 

User Tag List
Results 1 to 8 of 8
Thread: google maps distance conversion

Jul 1, 2010, 23:03 #1
 Join Date
 Aug 2007
 Posts
 328
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
google maps distance conversion
Hi Guys,
I've found out how to find the distance between 2 lat lng points using google's distanceFrom method:
Code:// NR14 7PZ var loc1 = new GLatLng(52.5773139, 1.3712427); // NR32 1TB var loc2 = new GLatLng(52.4788314, 1.7577444); alert(loc2.distanceFrom(loc1) / 1000);
The main reason I'm doing this is so that I can figure out the bounds of a certain area of a coordinate.
For example I specify a point, and then I need to draw a box around this area going x distance North, South, East and West, but until I know how many units make up a kilometre this wont be easy.
I guess I could go to google maps, click on a point and then click on another point and keep doing that until I get exactly 1km away from the original point, then just minus or plus for the lat and lng to find out the unit value for 1km, but I have a feeling that this wont be accurate.
Can anyone lend any advice, I realize that what I'm asking is more a map question than a google maps javascript api question, but perhaps someone has come across this before?

Jul 1, 2010, 23:53 #2
 Join Date
 Oct 2005
 Posts
 1,849
 Mentioned
 5 Post(s)
 Tagged
 1 Thread(s)
I have no experience with Google Maps. However, I did learn how to create a bounding box around a point and also find the GPS coordinates a distance away from a starting point. Maybe this can be of help to you:
http://www.movabletype.co.uk/scripts/latlong.html
I trust you already searched Google for bounding box information using Google Maps.

Jul 4, 2010, 04:32 #3
 Join Date
 Aug 2007
 Posts
 328
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Hi,
thanks for the tip.
That will come in very handy, however it looks like for that to work I'd need to specify the outer bounds, which is what I'm trying to achieve.
If I know a central point, I want to know the lat lng of a point x miles or km east and x miles or km north, that way I can use the code that you kindly pointed me to.
Does anyone know how this can be achieved?

Jul 6, 2010, 18:48 #4
 Join Date
 Oct 2005
 Posts
 1,849
 Mentioned
 5 Post(s)
 Tagged
 1 Thread(s)
The code I pointed to will allow you to achieve that.
http://www.movabletype.co.uk/scripts/latlong.html
Destination point given distance and bearing from start point
Given a start point, initial bearing, and distance, this will calculate the destination point and final bearing travelling along a (shortest distance) great circle arc.
Take a starting point, plug it into the Destination Point functions on that site (you may need to convert to a different format, converter is at the bottom of that page) and then compare it with a known distance. For example, when I made my function in PHP, I started with a known point and used the function result to compare it to various intersections where I knew the distance between them.
Many major roads are plotted a certain distance away from each other like a half mile or a full mile. Start at an known intersection, plug the numbers into the webpage, then compare the results on Google Maps with other intersections where you know the distance.
maps.google.com/maps?f=q&q={latitude},{longitude}&z=15&iwloc=A
Replace the {latitude} and {longitude} with the results from the webpage function in decimal format. Then copy and paste it into your browser. It should produce the desired result.
If you need the coordinates of a starting point (or any point), use this tool:
http://mapki.com/getLonLat.php
It works great.
Also see this:
http://www.codecodex.com/wiki/Calcul...on_a_Globe#PHP

Jul 11, 2010, 04:13 #5
 Join Date
 Aug 2007
 Posts
 328
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Thanks for the explanation, that makes it quite a bit clearer.
I've attempted to convert the javascript he gave into vb.net code so I can more easily use it in my project.
It's kind of working, but I don't think I've got it completely correct.
Can you take a look at my code and tell me where I'm going wrong:
Code:Dim lat1 = txtLat.Text Dim lng1 = txtLng.Text Dim brng = 90 Dim distance = 1 Dim radians = 6365 Dim lat2 = Math.Asin(Math.Sin(lat1) * Math.Cos(distance / radians) + Math.Cos(lat1) * Math.Sin(distance / radians) * Math.Cos(brng)) Dim lng2 = lng1 + Math.Atan2(Math.Sin(brng) * Math.Sin(distance / radians) * Math.Cos(lat1), Math.Cos(distance / radians)  Math.Sin(lat1) * Math.Sin(lat2)) Label1.Text = lng2 & " " & lat2
With the above code, I would have expected it to generate a lat long exactly 1km north of my starting point, but it doesn't seem to.
Firstly, the distance doesn't seem to be a static value, for example, if I set it to 1, it puts a point right next to the starting point, but if I put 10, it puts the point much further than 10 times the original distance away, as well as it not being exactly north as I would expect.
And for the radians value, I just looked up "earth's radius" on google and got a figure of "between 6,357 km to 6,378 km", so I gave it a value of 6365.
I've got a feeling the the radians is the value that I'm screwing up, but not quite sure how.
If you can clarify for me it would be greatly appreciated.

Jul 11, 2010, 08:43 #6
 Join Date
 Oct 2005
 Posts
 1,849
 Mentioned
 5 Post(s)
 Tagged
 1 Thread(s)
LOL. Radians isn't radius. The radian is a unit of measured based on the radius of a sphere.
http://en.wikipedia.org/wiki/Radian
Why don't you look at the thread below and try and adapt that to VB.NET. I don't know VB.NET, but I am sure they have similar functions. Just do a search on Google for the PHP functions if you are not sure what they are and find a VB.NET equivalent. Remember: You must convert your bearing, latitude, and longitude from degree format into radian format. I didn't find a function in VB.NET, but you can see this second link below.
http://www.sitepoint.com/forums/show...6&postcount=11
http://msdn.microsoft.com/enus/libr....math.sin.aspx
The angle, a, must be in radians. Multiply by Math.PI/180 to convert degrees to radians.

Jul 11, 2010, 10:03 #7
 Join Date
 Aug 2007
 Posts
 328
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
doh,
I didn't think radians and radius were the same thing, but since it just said d is distance and r is earth's radius, I thought I didn't really have to worry about it too much.
so:
d/r=radians
???
Profit
Apparently I was quite wrong lol.
I've just looked at your post and it's exactly what I need.
I only know a tiny bit of php but your code seems quite easy on the eyes.
I'll have a crack at it later tonight and let you know how I'm getting on.
Thanks again for your help

Jul 18, 2010, 07:02 #8
 Join Date
 Aug 2007
 Posts
 328
 Mentioned
 0 Post(s)
 Tagged
 0 Thread(s)
Sorry to revive a slightly old post but I wanted to finally post my solution incase anyone else needed it.
With a lot of help from Cheesedude's code as well as a few other sources I found on the internet I was able to construct the following:
ASP.Net/VB.Net Code:
Code:Public Function getNewLatLng(ByVal latitude As Object, ByVal longitude As Object, ByVal bearing As Object, ByVal distance As Object, ByVal unit As String) As List(Of String) Dim coordList As New List(Of String) Dim radius As Integer = 0 If unit = "m" Then radius = 3959 ElseIf unit = "km" Then radius = 6371 End If 'New latitude in degrees. Dim new_latitude = rad2deg(Asin(Sin(deg2rad(latitude)) * Cos(distance / radius) + Cos(deg2rad(latitude)) * Sin(distance / radius) * Cos(deg2rad(bearing)))) 'New longitude in degrees. Dim new_longitude = rad2deg(deg2rad(longitude) + Atan2(Sin(deg2rad(bearing)) * Sin(distance / radius) * Cos(deg2rad(latitude)), Cos(distance / radius)  Sin(deg2rad(latitude)) * Sin(deg2rad(new_latitude)))) coordList.Add(new_latitude) coordList.Add(new_longitude) Return coordList End Function Public Function rad2deg(ByVal radians As Single) As Single Return radians * 180.0 / PI End Function Public Function deg2rad(ByVal degrees As Single) As Single Return degrees * PI / 180 End Function
Code:var map = null; var geocoder = null; function initialize() { if (GBrowserIsCompatible()) { map = new GMap2(document.getElementById("map_canvas")); var nw = new GLatLng(coordinates of nw corner of bounding box go here); var se = new GLatLng(coordinates of nw corner of bounding box go here); var bounds = new GLatLngBounds(nw, se); // Center map in the center of the bounding box // and calculate the appropriate zoom level map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); geocoder = new GClientGeocoder(); } }
The only issue I have now is that I need to find some way to limit the number of results back from a sql query on all items.
Would something along the lines of the following work well?
select * from locations
where (lat > southernmost lat point (in bounding box) and lat < northern most lat point)
and
(lng> western most lng point and lng< eastern most lng point)
Bookmarks