Those nice folks at Streetmap (http://www.streetmap.co.uk ) have a very handy webpage that lets you obtain the Longitude/Latitude of a UK Postcode at -
http://www.streetmap.co.uk/gridconvert.html
This is so nice for geo-encoding UK postcodes, i.e plotting customers on a map etc.. Given the flexibility of the service, I decided to make it line-of-business friendly and wrap the postcode to long/lat lookup into a web-service.
The return string is of the form -
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://tempuri.org/">51.625128:-0.150448:Provided By www.streetmap.co.uk:http://www.streetmap.co.uk/streetmap.dll?grid2map?X=528126&Y=193432&arrow=Y&adkey="~"</string>
So we return a decimal longitude, latitude and a credit back to streetmap and a link to the streetmap, map.
Here's the source, the string handling is a little brutal but it works great.
using System;using System.Web;using System.Web.Services;using System.Web.Services.Protocols;using System.Net;using System.IO;
[WebService(Namespace = "http://tempuri.org/")][WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]public class Service : System.Web.Services.WebService{public Service () {
//Uncomment the following line if using designed components //InitializeComponent(); }
[WebMethod]public string GeoEncode(string UK_Postcode) {
if (UK_Postcode == "") return "No Input"string lookuppostcode = UK_Postcode.Replace("-", "");lookuppostcode = lookuppostcode.Replace(" ", "");string request = @"http://www.streetmap.co.uk/streetmap.dll?GridConvert?type=Postcode&name=" + lookuppostcode;string result = "ERROR"
HttpWebRequest myReq =(HttpWebRequest)WebRequest.Create(request);
// Sends the HttpWebRequest and waits for the response.HttpWebResponse myHttpWebResponse = (HttpWebResponse) myReq.GetResponse();try{Stream response = myHttpWebResponse.GetResponseStream();StreamReader readStream = new StreamReader(response, System.Text.Encoding.GetEncoding("utf-8"));string htmlresponse = readStream.ReadToEnd();string whattofind = "Lat</strong> (WGS84) </td> <td width=\"50%\" align=\"center\" valign=\"middle\">"int indexoflonglatbit = htmlresponse.IndexOf(whattofind);if (indexoflonglatbit > -1){htmlresponse = htmlresponse.Substring(indexoflonglatbit + whattofind.Length);string latitude=htmlresponse.Substring(htmlresponse.IndexOf("("));latitude=latitude.Substring(2,latitude.IndexOf(")")-3);whattofind="Long</strong> (WGS84) </td> <td width=\"50%\" align=\"center\" valign=\"middle\">"indexoflonglatbit = htmlresponse.IndexOf(whattofind);
if (indexoflonglatbit > -1){htmlresponse = htmlresponse.Substring(indexoflonglatbit + whattofind.Length);string longitude = htmlresponse.Substring(htmlresponse.IndexOf("("));longitude = longitude.Substring(2, longitude.IndexOf(")")-3);
string url_to_follow = htmlresponse.Substring(htmlresponse.IndexOf( "http://www.streetmap.co.uk/streetmap.dll?grid2map"));url_to_follow=url_to_follow.Substring(0,url_to_follow.IndexOf("\"")+3);
result = latitude + ":" + longitude+":Provided By www.streetmap.co.uk:"+url_to_follow;}
}
}finally{// Releases the resources of the response.myHttpWebResponse.Close();}return result;
}}
Theme design by Jelle Druyts
Pick a theme: BlogXP calmBlue
My Virtual Earth Balloon Races www.racingballoon.com Richard JonesPowered By The Fridge Server