精選文章

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

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

2019年9月25日 星期三

C# Google Maps API取得資料

問題:C# Google Maps API取得資料
把地址轉成JSON格式
  1. public GoogleGeoCodeResponse ConvertAddressToLatLng(string addr)
  2. {
  3. string result = string.Empty;
  4. string googlemapkey = "你的API金鑰";
  5. string MapUrl = String.Format("https://maps.googleapis.com/maps/api/geocode/json?");
  6. string url = MapUrl + "address={0}&key={1}";
  7. url = string.Format(url, addr, googlemapkey);
  8.  
  9. HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
  10. using (var response = request.GetResponse())
  11. using (StreamReader sr = new StreamReader(response.GetResponseStream()))
  12. {
  13.  
  14. result = sr.ReadToEnd();
  15. }
  16.  
  17. return JsonConvert.DeserializeObject(result);
  18. }

取得的JSON格式(20190925)
  1. {
  2. "results" : [
  3. {
  4. "address_components" : [
  5. {
  6. "long_name" : "122號",
  7. "short_name" : "122號",
  8. "types" : [ "street_number" ]
  9. },
  10. {
  11. "long_name" : "Section 1, Chongqing South Road",
  12. "short_name" : "Section 1, Chongqing South Road",
  13. "types" : [ "route" ]
  14. },
  15. {
  16. "long_name" : "Bo'ai Special Zone",
  17. "short_name" : "Bo'ai Special Zone",
  18. "types" : [ "neighborhood", "political" ]
  19. },
  20. {
  21. "long_name" : "Zhongzheng District",
  22. "short_name" : "Zhongzheng District",
  23. "types" : [ "administrative_area_level_3", "political" ]
  24. },
  25. {
  26. "long_name" : "Taipei City",
  27. "short_name" : "Taipei City",
  28. "types" : [ "administrative_area_level_1", "political" ]
  29. },
  30. {
  31. "long_name" : "Taiwan",
  32. "short_name" : "TW",
  33. "types" : [ "country", "political" ]
  34. },
  35. {
  36. "long_name" : "100",
  37. "short_name" : "100",
  38. "types" : [ "postal_code" ]
  39. }
  40. ],
  41. "formatted_address" : "No. 122號, Section 1, Chongqing South Road, Zhongzheng District, Taipei City, Taiwan 100",
  42. "geometry" : {
  43. "location" : {
  44. "lat" : 25.0400826,
  45. "lng" : 121.5119547
  46. },
  47. "location_type" : "ROOFTOP",
  48. "viewport" : {
  49. "northeast" : {
  50. "lat" : 25.0414315802915,
  51. "lng" : 121.5133036802915
  52. },
  53. "southwest" : {
  54. "lat" : 25.03873361970849,
  55. "lng" : 121.5106057197085
  56. }
  57. }
  58. },
  59. "place_id" : "ChIJJXNJjQqpQjQR-UfWtMFthwI",
  60. "plus_code" : {
  61. "compound_code" : "2GR6+2Q Zhongzheng District, 台北市 Taiwan",
  62. "global_code" : "7QQ32GR6+2Q"
  63. },
  64. "types" : [ "establishment", "point_of_interest", "tourist_attraction" ]
  65. }
  66. ],
  67. "status" : "OK"
  68. }

設定的結構
部份有調整過,可以自行設計
  1. public class GoogleGeoCodeResponse
  2. {
  3.  
  4. public string status { get; set; }
  5. public string error_message { get; set; }
  6. public results[] results { get; set; }
  7. }
  8. public class results
  9. {
  10. public string formatted_address { get; set; }
  11. public geometry geometry { get; set; }
  12. public string[] types { get; set; }
  13. public address_component[] address_components { get; set; }
  14. }
  15. public class geometry
  16. {
  17. public string location_type { get; set; }
  18. public location location { get; set; }
  19. }
  20.  
  21. public class location
  22. {
  23. public string status { get; set; }
  24. public string error_message { get; set; }
  25. public string lat { get; set; }
  26. public string lng { get; set; }
  27. }
  28. public class address_component
  29. {
  30. public string long_name { get; set; }
  31. public string short_name { get; set; }
  32. public string[] types { get; set; }
  33. }

處理地址轉成json後,輸出經緯度資料
  1. public location GetLatLngByAddr(string addr)
  2. {
  3. location _result = new location();
  4. GoogleGeoCodeResponse _mapdata = new GoogleGeoCodeResponse();
  5. _mapdata = ConvertAddressToLatLng(addr);
  6. if (_mapdata.status == "OK")
  7. {
  8. _result.lat = _mapdata.results[0].geometry.location.lat;
  9. _result.lng = _mapdata.results[0].geometry.location.lng;
  10. }
  11. else
  12. {
  13. _result.error_message = _mapdata.error_message;
  14. }
  15. _result.status = _mapdata.status;
  16. return _result;
  17. }

沒有留言: