摘要:Google Geocoding API 延伸
小編在前兩篇介紹了Google Geocoding API 跟使用方式,相信讀者們必定能吸收並且把它功用發揮到更大。
這篇 Google Geocoding API 延伸 主要是是利用EnGeocoding的方式,反向的操作來擷取Google Maps的資訊。
EnGeocoding反向作業是將地圖上的經緯度座標點轉變為地址,就稱為「反向地理編碼」。
其實跟前兩篇的所用的原理大同小異,讀者們只要熟記以下幾點步驟就可以了。
第一步:使用JSON格式的URL
http://maps.googleapis.com/maps/api/geocode/json?latlng=24.1500261,120.6839536&sensor=true
第二步:將讀者想要的位置經緯度的修改到 latlng=24.18,120.70 這個參數之中。
第三步:把修改好的的網址貼到瀏覽器上,就會回傳一個JSON格式檔。
{
"results" : [
{
"address_components" : [
{
"long_name" : "129",
"short_name" : "129",
"types" : [ "street_number" ]
},
{
"long_name" : "國立臺中科技大學",
"short_name" : "國立臺中科技大學",
"types" : [ "establishment" ]
},
{
"long_name" : "三民路三段",
"short_name" : "三民路三段",
"types" : [ "route" ]
},
{
"long_name" : "錦平里",
"short_name" : "錦平里",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "北區",
"short_name" : "北區",
"types" : [ "locality", "political" ]
},
{
"long_name" : "台中市",
"short_name" : "台中市",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "台灣",
"short_name" : "TW",
"types" : [ "country", "political" ]
},
{
"long_name" : "404",
"short_name" : "404",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "404台灣台中市北區國立臺中科技大學三民路三段129號",
"geometry" : {
"location" : {
"lat" : 24.1500261,
"lng" : 120.6839536
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 24.1513750802915,
"lng" : 120.6853025802915
},
"southwest" : {
"lat" : 24.14867711970849,
"lng" : 120.6826046197085
}
}
},
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "國立台中科技大學",
"short_name" : "國立台中科技大學",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"long_name" : "新興里",
"short_name" : "新興里",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "北區",
"short_name" : "北區",
"types" : [ "locality", "political" ]
},
{
"long_name" : "台中市",
"short_name" : "台中市",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "台灣",
"short_name" : "TW",
"types" : [ "country", "political" ]
},
{
"long_name" : "404",
"short_name" : "404",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "404台灣台中市北區國立台中科技大學",
"geometry" : {
"location" : {
"lat" : 24.149684,
"lng" : 120.684135
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 24.1510329802915,
"lng" : 120.6854839802915
},
"southwest" : {
"lat" : 24.1483350197085,
"lng" : 120.6827860197085
}
}
},
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "國立台中科技大學",
"short_name" : "國立台中科技大學",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"long_name" : "錦平里",
"short_name" : "錦平里",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "北區",
"short_name" : "北區",
"types" : [ "locality", "political" ]
},
{
"long_name" : "台中市",
"short_name" : "台中市",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "台灣",
"short_name" : "TW",
"types" : [ "country", "political" ]
},
{
"long_name" : "404",
"short_name" : "404",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "404台灣台中市北區國立台中科技大學",
"geometry" : {
"location" : {
"lat" : 24.149589,
"lng" : 120.683845
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 24.1509379802915,
"lng" : 120.6851939802915
},
"southwest" : {
"lat" : 24.1482400197085,
"lng" : 120.6824960197085
}
}
},
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "錦平里",
"short_name" : "錦平里",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "北區",
"short_name" : "北區",
"types" : [ "locality", "political" ]
},
{
"long_name" : "台中市",
"short_name" : "台中市",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "台灣",
"short_name" : "TW",
"types" : [ "country", "political" ]
},
{
"long_name" : "404",
"short_name" : "404",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "404台灣台中市北區錦平里",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 24.154717,
"lng" : 120.686196
},
"southwest" : {
"lat" : 24.148323,
"lng" : 120.679694
}
},
"location" : {
"lat" : 24.1514619,
"lng" : 120.6825368
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 24.154717,
"lng" : 120.686196
},
"southwest" : {
"lat" : 24.148323,
"lng" : 120.679694
}
}
},
"types" : [ "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "北區",
"short_name" : "北區",
"types" : [ "locality", "political" ]
},
{
"long_name" : "台中市",
"short_name" : "台中市",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "台灣",
"short_name" : "TW",
"types" : [ "country", "political" ]
},
{
"long_name" : "404",
"short_name" : "404",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "404台灣台中市北區",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 24.1739834,
"lng" : 120.7045623
},
"southwest" : {
"lat" : 24.1422531,
"lng" : 120.6625956
}
},
"location" : {
"lat" : 24.1573171,
"lng" : 120.6832556
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 24.1739834,
"lng" : 120.7045623
},
"southwest" : {
"lat" : 24.1422531,
"lng" : 120.6625956
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "404",
"short_name" : "404",
"types" : [ "postal_code" ]
},
{
"long_name" : "北區",
"short_name" : "北區",
"types" : [ "locality", "political" ]
},
{
"long_name" : "台中市",
"short_name" : "台中市",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "台灣",
"short_name" : "TW",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "404台灣台中市北區",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 24.1739834,
"lng" : 120.7045623
},
"southwest" : {
"lat" : 24.1422531,
"lng" : 120.6625956
}
},
"location" : {
"lat" : 24.1573171,
"lng" : 120.6832556
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 24.1739834,
"lng" : 120.7045623
},
"southwest" : {
"lat" : 24.1422531,
"lng" : 120.6625956
}
}
},
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "台中市",
"short_name" : "台中市",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "台灣",
"short_name" : "TW",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "台灣台中市",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 24.4416975,
"lng" : 121.4509513
},
"southwest" : {
"lat" : 23.9985086,
"lng" : 120.4608053
}
},
"location" : {
"lat" : 24.2332076,
"lng" : 120.9417368
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 24.4416975,
"lng" : 121.4509513
},
"southwest" : {
"lat" : 23.9985086,
"lng" : 120.4608053
}
}
},
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "台灣",
"short_name" : "TW",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "台灣",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 26.3873532,
"lng" : 122.0069051
},
"southwest" : {
"lat" : 20.5637908,
"lng" : 116.7118602
}
},
"location" : {
"lat" : 23.69781,
"lng" : 120.960515
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 25.3006668,
"lng" : 122.0069051
},
"southwest" : {
"lat" : 21.8966958,
"lng" : 120.0278016
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
第四步:之後把該網頁整個全選接著整篇複製下來,並貼到 http://json2csharp.com/ ,點Generate產生Json類別。
public class AddressComponent
{
public string long_name { get; set; }
public string short_name { get; set; }
public List types { get; set; }
}
public class Location
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Northeast
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Viewport
{
public Northeast northeast { get; set; }
public Southwest southwest { get; set; }
}
public class Northeast2
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Southwest2
{
public double lat { get; set; }
public double lng { get; set; }
}
public class Bounds
{
public Northeast2 northeast { get; set; }
public Southwest2 southwest { get; set; }
}
public class Geometry
{
public Location location { get; set; }
public string location_type { get; set; }
public Viewport viewport { get; set; }
public Bounds bounds { get; set; }
}
public class Result
{
public List address_components { get; set; }
public string formatted_address { get; set; }
public Geometry geometry { get; set; }
public List types { get; set; }
}
public class RootObject
{
public List results { get; set; }
public string status { get; set; }
}
第五步:在讀者建立好的專案底下加入一個類別檔,之後就可以把第四步產生出來的JSON類別程式碼全部貼進去可以使用它了。
第六步:利用網路擷取功能抓取第一步的URL取得該網頁資訊,小編這邊範例是使用WebClient。
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
string json_data = "http://maps.googleapis.com/maps/api/geocode/json?latlng=24.1500261,120.6839536&sensor=true";
wc.DownloadStringAsync(new Uri(json_data));
wc.DownloadStringCompleted += (sender2, e2) =>
{
};
第七步:宣告一個RootObject,並將擷取到的JSON Data反序列化轉成物件。
RootObject userInfo = JsonConvert.DeserializeObject(e2.Result);
MessageBox.Show(userInfo.results[0].formatted_address + "\n");
你沒看錯,只要七個步驟就可以了完成反向操作了。