天天看點

利用google map擷取特定地區或位址的經緯度資訊

最近一個項目需要拿到一地地理位置的 經緯度 總不能在google earth上一個個找吧

于是希望可以找到相應的web service 發現以前的msn search 支援 現在 也不支援了 發現這些地理資訊的服務 都僅限于你在你的網站上嵌入一個 地圖 然後你可以在他的地圖上 添加什麼東西   SDK都是基于javascript的 沒有完全的開放啊

後來實在沒有辦法了 想到 google map可以輸入位址 然後定位到那個區域 那麼 這其中拿到的結果一定有經緯度的資訊 于是 在google query了一個 然後view source了一下 發現 果不其然 這個經緯度資訊 是明文放在網頁裡面的

 是以 采用以下辦法就可以了

class Getgeo

{

public struct geo

{

public string Latitude;

public string Longtitude;

}

public static geo Getgeo(

string location)

{

string geo="";

geo mygeo = new geo();

string results = String.Empty;

HttpWebRequest searchRequest =

(HttpWebRequest)WebRequest.Create(@"http://maps.google.com/maps?f=q&geocode=&q="+location +"&output=js");

WebResponse myresponse = searchRequest.GetResponse();

Stream responseStream = myresponse.GetResponseStream();

byte[] buffer = new byte[9999 ];

responseStream.Read(buffer, 0, (int)9999);

results = System.Text.Encoding.ASCII.GetString(buffer);

System.Text.RegularExpressions.Regex myregex = new Regex(@"center..lat\:[^,]+,lng\:.{0,1}[0-9]+.[0-9]+");

MatchCollection mc = myregex.Matches(results);

foreach (Match mymatch in mc)

{

geo = mymatch.Value;

string latitude=geo .Substring (geo .IndexOf ("lat:")+4,5);

string longtitude=geo .Substring (geo .IndexOf ("lng:")+4,5);

mygeo.Latitude = latitude;

mygeo.Longtitude = longtitude;

if (mygeo.Latitude == null)

{

System.Windows.Forms.MessageBox.Show("Null seen");

}

}

myresponse.Close();

responseStream.Close();

myresponse.Close();

return mygeo;

}

public static string GetLongtitude(

string location)

{

geo mygeo = Getgeo(location );

return mygeo.Longtitude;

}

public static string GetLatitude(

string location)

{

geo mygeo = Getgeo(location );

return mygeo.Latitude;

}

}

具體說來就是http://maps.google.com/maps?f=q&geocode=&q="+你要查找的位置+"&output=js" 的httprequest 然後在得到的字元串裡面 比對一下  (@"center..lat\:[^,]+,lng\:.{0,1}[0-9]+.[0-9]+") 正規表達式就可以啦

利用google map擷取特定地區或位址的經緯度資訊