Latitude, Longitude Question

Not sure if this is the right forum for this question.

Does anyone know how to get the Latitude, Longitude coordinates
for given address in code ?

Clarify “address”?

Jeff, I would have though Jerry means street address.

Jerry I would look at some of the API’s at


Me too.
Good links there. I was considering the Google API myself, but I asked due to the possibility that the OP is talking about IP address.
Visions of ‘Spooks’ software zooming in on some hacker’s computer…

By address I mean: 123 Street, City, State, Zip

Then, you are concerned only about the U.S.?

Jim Wagner

Yes and Canada if possible. My program stores campground information and when
viewing maps it would be nice to get the map from Latitude and Longitude coordinates
of the campground. As it is now i just get the maps by address. There not as close
as gps (Latitude and Longitude coordinates) would be.

But you said you want to get the lat/long from an address. Won’t you just end up with inacurate coordinates

Why would I get inaccurate coordinates from an address. I can get them from zip codes now
but they are not as accurate as from a full address would be. Getting them from zip only gets
you close as zip codes are based on the city. Some campgrounds have a city address but could
be 5 or so miles from the city. GPS coordinates will get you right at the location.

Can’t you skip the lat/long step and just get the map from the address directly?

I could but my users want GPS coordinates and I have a place in the program
for them but they had to enter them manually. I just thought it would be nice
to get the GPS coordinates when they plug in the street, city, state and zip.
They use them for their GPS units. It’s kinda an auto fill if you will.

How did you get on with the API’s Jerry?

Not finding what I want but my lack of knowledge of API’s may not be helping me either.
I’m not really a strong programer, I’ve never done any programing until I bought RS
a couple years ago but I like it and at my age it keeps the mind working. LOL

I’m still digging though.

Well I checked the API’s out and did play with a couple of them but did not
find what I needed. Oh well… Customer can just type them in the program
if they have them.

The google one is pretty easy.

Add these controls to a window

addressFld (TextField)
latLonResultsFld (TextField)
downloadLatLonButton (PushButton)
latLonSocket (HttpSocket)

add these events

Sub Action()

dim head, query, tail As String //build url
head = “
query = addressFld.Text.Trim.ReplaceAll(" ", “+”)
tail = “&sensor=false”

latLonSocket.Get(head + query + tail) //request data

End Sub

Sub PageReceived(url as string, httpStatus as integer, headers as internetHeaders, content as string)

dim doc As new XmlDocument(content) //parse to tree

dim lat, lon As String
lat = xqlFirstInnerText(doc, “//geometry/location/lat”) //extract values
lon = xqlFirstInnerText(doc, “//geometry/location/lng”)

latLonResultsFld.Text = "latitude: " + lat + ", longitude: " + lon //show

End Sub[/code]

and add this utility method for xml handling

Function xqlFirstInnerText(doc As XmlDocument, query As String) As String dim nl As XmlNodeList = doc.Xql(query) if nl.Length = 0 then return "" dim n As XmlNode = nl.Item(0).FirstChild if n = nil or not n IsA XmlTextNode then return "" return n.Value End Function

This is limited to 2500 requests per day, not sure how that’s enforced. There’s also this restriction: The Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited.

@ Will Shank

Thanks Will. That is just what I needed.

It works like a champ. Should I check if the user has an internet connection
before invoking this routine ? It just seems to ignore it without an error if they don’t.

I don’t know how to check for internet connection before calling Get, but if you add the Error event to the socket that’ll get fired instead of PageReceived when there’s no internet.

@ Will Shank

I was just wondering if the code example you gave could be changed or added to
to get Elevation by the Latitude/Longitude coordinates ?

The google geocoding results don’t include elevation data. But if you follow Mikes link, to the left is ‘Elevation API’ which is pretty similar mechanics, you’ll just need to get it with a second Get. This example the lat/lon is hardcoded and the socket recoded just for elevation. You’ll need 2 sockets or some sorta switching logic to control the 2 separate gets.

[code]Sub Action()
dim url As String
url = “,-104.9847034&sensor=false
End Sub

Sub PageReceived(url as string, httpStatus as integer, headers as internetHeaders, content as string)

dim doc As new XmlDocument(content)

dim elev As String = xqlFirstInnerText(doc, “//elevation”)

latLonResultsFld.Text = "elevation: " + elev

End Sub[/code]