精選文章

bcp大量匯出與匯入資料庫資料方法

--匯出-- bcp "select * from [資料庫名稱].[dbo].[資料表名稱]" queryout 匯出檔案名稱.txt -w -U "使用者帳號" -P "使用者密碼" " ...

2019年9月25日 星期三

C# Google Maps API取得資料

問題:C# Google Maps API取得資料
把地址轉成JSON格式
    
public GoogleGeoCodeResponse ConvertAddressToLatLng(string addr)
        {
            string result = string.Empty;
            string googlemapkey = "你的API金鑰";         
            string MapUrl = String.Format("https://maps.googleapis.com/maps/api/geocode/json?");
            
            string url = MapUrl + "address={0}&key={1}";
            url = string.Format(url, addr, googlemapkey);

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            using (var response = request.GetResponse())
            using (StreamReader sr = new StreamReader(response.GetResponseStream()))
            {

                result = sr.ReadToEnd();
            }

            return JsonConvert.DeserializeObject(result);
        }

取得的JSON格式(20190925)
 
{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "122號",
               "short_name" : "122號",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Section 1, Chongqing South Road",
               "short_name" : "Section 1, Chongqing South Road",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Bo'ai Special Zone",
               "short_name" : "Bo'ai Special Zone",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Zhongzheng District",
               "short_name" : "Zhongzheng District",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Taipei City",
               "short_name" : "Taipei City",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Taiwan",
               "short_name" : "TW",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "100",
               "short_name" : "100",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "No. 122號, Section 1, Chongqing South Road, Zhongzheng District, Taipei City, Taiwan 100",
         "geometry" : {
            "location" : {
               "lat" : 25.0400826,
               "lng" : 121.5119547
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 25.0414315802915,
                  "lng" : 121.5133036802915
               },
               "southwest" : {
                  "lat" : 25.03873361970849,
                  "lng" : 121.5106057197085
               }
            }
         },
         "place_id" : "ChIJJXNJjQqpQjQR-UfWtMFthwI",
         "plus_code" : {
            "compound_code" : "2GR6+2Q Zhongzheng District, 台北市 Taiwan",
            "global_code" : "7QQ32GR6+2Q"
         },
         "types" : [ "establishment", "point_of_interest", "tourist_attraction" ]
      }
   ],
   "status" : "OK"
}

設定的結構
部份有調整過,可以自行設計
 
public class GoogleGeoCodeResponse
        {

            public string status { get; set; }
            public string error_message { get; set; }
            public results[] results { get; set; }
        }
        public class results
        {
            public string formatted_address { get; set; }
            public geometry geometry { get; set; }
            public string[] types { get; set; }
            public address_component[] address_components { get; set; }
        }
        public class geometry
        {
            public string location_type { get; set; }
            public location location { get; set; }
        }

        public class location
        {
            public string status { get; set; }
            public string error_message { get; set; }
            public string lat { get; set; }
            public string lng { get; set; }
        }
        public class address_component
        {
            public string long_name { get; set; }
            public string short_name { get; set; }
            public string[] types { get; set; }
        }

處理地址轉成json後,輸出經緯度資料
 
public location GetLatLngByAddr(string addr)
        {
            location _result = new location();
            GoogleGeoCodeResponse _mapdata = new GoogleGeoCodeResponse();
            _mapdata = ConvertAddressToLatLng(addr);
            if (_mapdata.status == "OK")
            {
                    _result.lat = _mapdata.results[0].geometry.location.lat;
                    _result.lng = _mapdata.results[0].geometry.location.lng;
            }
            else
            {
                _result.error_message = _mapdata.error_message;
            }
            _result.status = _mapdata.status;
            return _result;
        }

沒有留言: