Retrieving Lat & Lon from an address without a map

I have a web form that enables users to enter an address, and is supposed to return a latitude and longitude for that address. Unfortunately, I've been unable to find any articles or code that can return these values.

Any help is greatly appreciated. Thanks!

[282 byte] By [DCRoss] at [2007-12-25]
# 1
What you are after is called "Geocoding".
There are many paid and free services available for different countries. As i'm sure your aware you can sort of acheive this using VE but it more about showing that location on a map. Microsoft have a web service called mappoint that offers this functionlaity at a price. I would do some searching for Geocoding for the region of the world you are interested it. Most likely if it is not for profit it will be free or else you'll pay per geocode :(
John.
SoulSolutions at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 2

Thanks, John,

We're using VE maps elsewhere on the site, but for some reason, the client doesn't want a map on this one, single page ?

At any rate, is there any way to use the Find() method without the map to geocode?

Thanks again!

DCRoss at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 3
You can of course hide the map, defining the div as:
<div id="map" style="display: none;"></div>
The user still has to load the map etc (so it will take some time to load) but you can use the Find method :-)
J-Thread at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 4
Thanks, that is a possible workaround but if the address is invalid, incorrect coordinates are returned.
DCRoss at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 5

I have used a local.live.com/search.ashx service to get the geocoding in my application. Here is the source code:

Make sure you have added:

using System.Net;

using System.Text.RegularExpressions;

using System.Web;

using System.Web.Configuration;

using System.Web.Security;

using System.Xml;

using System.Xml.XPath;

using System.IO;

--

privatevoid button1_Click(object sender, EventArgs e)

{

double Latitude = 0;

double Longitude = 0;

GeoCode(textBox1.Text, out Latitude, out Longitude);

lblLat.Text = Latitude.ToString();

lblLong.Text = Longitude.ToString();

}

privatebool GeoCode(string Address, outdouble Latitude, outdouble Longitude)

{

bool RetVal = true;

double long2 = 0;

double long1 = 0;

double lat2 = 0;

double lat1 = 0;

Latitude = 0;

Longitude = 0;

string text1 = "a=&b=" + Address + "&c=0.0&d=0.0&e=0.0&f=0.0&g=&i=&r=0";

string text2 = DoSearchRequest(text1);

if ((text2 == null) || (text2 == string.Empty))

{

returnfalse;

}

Regex regex1 = newRegex(@"SetViewport\((?<lat1>\S+),(?<long1>\S+),(?<lat2>\S+),(?<long2>\S+)\)");

Match match1 = regex1.Match(text2);

if (!match1.Success)

{

returnfalse;

}

lat1 = double.Parse(match1.Groups["lat1"].Value);

long1 = double.Parse(match1.Groups["long1"].Value);

lat2 = double.Parse(match1.Groups["lat2"].Value);

long2 = double.Parse(match1.Groups["long2"].Value);

// we use the midpoint of the viewport here

Latitude = (lat1 + lat2) / 2;

Longitude = (long1 + long2) / 2;

return RetVal;

}

privatestring DoSearchRequest(string searchParams)

{

string text1 = string.Empty;

HttpWebRequest request1 = (HttpWebRequest)WebRequest.Create("http://local.live.com/search.ashx");

request1.Method = "POST";

request1.ContentType = "application/x-www-form-urlencoded";

UTF8Encoding encoding1 = newUTF8Encoding();

byte[] buffer1 = encoding1.GetBytes(searchParams);

request1.ContentLength = buffer1.Length;

try

{

Stream stream1 = request1.GetRequestStream();

stream1.Write(buffer1, 0, buffer1.Length);

stream1.Close();

text1 = GetSearchResults(request1);

}

catch (WebException)

{

}

return text1;

}

privatestring GetSearchResults(HttpWebRequest searchRequest)

{

string text1 = string.Empty;

HttpWebResponse response1 = (HttpWebResponse)searchRequest.GetResponse();

Stream stream1 = response1.GetResponseStream();

Encoding encoding1 = Encoding.GetEncoding("utf-8");

StreamReader reader1 = newStreamReader(stream1, encoding1);

char[] chArray1 = newchar[0x100];

for (int num1 = reader1.Read(chArray1, 0, 0x100); num1 > 0; num1 = reader1.Read(chArray1, 0, 0x100))

{

string text2 = newstring(chArray1, 0, num1);

text1 = text1 + text2;

}

response1.Close();

return text1;

}

SVadali at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 6

Thats really awesome Svadali.

Thanks for sharing the code. Now I wonder if you can use the service to do reverse geocoding.....

John

SoulSolutions at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 7

Excellent, SVadali Thanks!

DCRoss at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 8

Hey is there anywhere that actually gives definitions for the varibles passed?

a=&b=" + Address + "&c=0.0&d=0.0&e=0.0&f=0.0&g=&i=&r=0"

I assume this is the what, where, some sort of current map bounds, any idea on what g and i are?

John.

SoulSolutions at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 9
SoulSolutions wrote:

Thats really awesome Svadali.

Thanks for sharing the code. Now I wonder if you can use the service to do reverse geocoding.....

John

John, I tried to reverse Geocode using this service.. but could not get any information back... will try again some other time...

SVadali at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 10
Senks!!!! You are super GURU!!!
Danger at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 11
Uauh..

this code is great.

I need one link like http://local.live.com/search.ashx that retrive the distance between 2 points. Anyone knows if it possible?

Thanks

busternelson at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 12

Hi,

I am trying to convert your code to vb.net

However, I am having trouble understanding the following code? Could you please explain?

char[] chArray1 = newchar[0x100];

for (int num1 = reader1.Read(chArray1, 0, 0x100); num1 > 0; num1 = reader1.Read(chArray1, 0, 0x100))

{

string text2 = newstring(chArray1, 0, num1);

text1 = text1 + text2;

Thanks

CuriousLearner at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 13
Hi,
Did you convert the code to VB.Net? If so would you please share it?

Thanks

CuriousLearner at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...
# 14

CuriousLearner,

Not sure whether you actually got what you were after but you don't need to have the code converted to VB.NET.

Use the code provided to create a class. You just need to get rid of the button_click event that is in there and alter this to be VB in your application. Then the new class can be made available to other apps if you need to.

I also have a VB website project so have converted as above. Reply to this post if you need the entire code/instructions for creating the class and how to reference it in your VB code.

Woody.

Woody_In_Sheffield at 2007-10-8 > top of Msdn Tech,Windows Live Developer Forums,Virtual Earth: Map Control Development...

Windows Live Developer Forums

Site Classified