1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > C# 根据地址调用 Google Map 服务得到经纬度

C# 根据地址调用 Google Map 服务得到经纬度

时间:2023-12-13 18:27:48

相关推荐

C# 根据地址调用 Google Map 服务得到经纬度

直接贴代码了:

using System;using System.Collections.Generic;using System.IO;using System.Linq;using ;using System.Text;using System.Web.Script.Serialization;namespace MvcUI.Extensions{public static class LocalisationUtils{/// <summary>/// 根据地址调用 Google 服务得到经纬度/// </summary>/// <param name="postcode"></param>/// <returns></returns>public static List<string> GeocodeGoogle(string postcode){var longlat = new List<string>();var req = (HttpWebRequest)WebRequest.Create(string.Format("/uds/GlocalSearch?q={0}&v=1.0", postcode));using (var resp = req.GetResponse())using (var respStream = resp.GetResponseStream())using (var reader = new StreamReader(respStream)){var response = reader.ReadToEnd();var serializer = new JavaScriptSerializer();var deserialized = (Dictionary<string, object>)serializer.DeserializeObject(response);var responseData = (Dictionary<string, object>)deserialized["responseData"];var results = (object[])responseData["results"];try{var resultsData = (Dictionary<string, object>)results[0];longlat.Add(resultsData["lat"].ToString());longlat.Add(resultsData["lng"].ToString());longlat.Add(resultsData["title"].ToString());}catch (Exception){longlat.Add("0");longlat.Add("0");longlat.Add("No Result");}return longlat;}}}}

谢谢浏览!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。