如何使用C#查找纬度和经度

30

我有一个使用C#编写的 WCF 服务。

在服务调用中,客户端发送城市名称。我想将城市名称转换为经纬度并存储在人口统计信息下的数据库中。

我计划使用Google API来实现上述功能。

我已经从Google获取了一个API密钥,它是“服务账户”类型。

我该如何使用哪些API来获取经纬度?

我是否需要安装某个SDK或者只需要使用REST服务就可以了?


1
所有回答都涉及Google地图;如果您愿意使用必应地图,请参见https://learn.microsoft.com/en-us/windows/uwp/maps-and-location/geocoding - B. Clay Shannon-B. Crow Raven
5个回答

67
你可以尝试使用NuGet包GoogleMaps.LocationServices,或者只需拆分其源代码。它使用了谷歌的REST API来获取给定地址的经纬度以及相反操作,无需API密钥。

你可以像这样使用:

public static void Main()
{
    var address = "Stavanger, Norway";

    var locationService = new GoogleLocationService();
    var point = locationService.GetLatLongFromAddress(address);

    var latitude = point.Latitude;
    var longitude = point.Longitude;

    // Save lat/long values to DB...
}

1
与上面的答案相同,这也适用于限制。它只是一个围绕REST API的C#包装器。请参见源代码 - khellang
1
这个解决方案为我节省了数小时的XML解析时间。+1,这是一个优雅的解决方案!! - TheOptimusPrimus
@khellang,代码很好,是个不错的例子。但我遇到了问题,我的应用程序无法工作,提示缺少关键性的文件 .snk ,错误信息为“Error 1 Assembly generation failed -- Referenced assembly 'GoogleMaps.LocationServices' does not have a strong name xxxxxxxPlugins\CSC PluginsMAPA”。谢谢你的代码! - KingRider
1
让我的生活变得更轻松了。 - MD TAREQ HASSAN
1
“不需要 API 密钥” 但它告诉我没有授权访问。 - MwBakker
显示剩余4条评论

21
如果您想使用Google Maps API,请查看它们的REST API,您不需要安装Google Maps API,只需像发送请求一样即可。
http://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false

你将会得到一个响应的XML。

如果需要响应的JSON:

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Estância+Sergipe,&key=**YOUR_API_KEY**

更多信息请查看

https://developers.google.com/maps/documentation/geocoding/index#GeocodingRequests


令人惊叹并非常有帮助。 - 3 rules

7
你可以在特定的URL中传递地址,然后你会得到纬度和经度的返回值dt(数据表)。
string url = "http://maps.google.com/maps/api/geocode/xml?address=" + address+ "&sensor=false";
WebRequest request = WebRequest.Create(url);

using (WebResponse response = (HttpWebResponse)request.GetResponse())
{
    DataTable dtCoordinates = new DataTable();

    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
    {
        DataSet dsResult = new DataSet();
        dsResult.ReadXml(reader);
        dtCoordinates.Columns.AddRange(new DataColumn[4] { new DataColumn("Id", typeof(int)),
                    new DataColumn("Address", typeof(string)),
                    new DataColumn("Latitude",typeof(string)),
                    new DataColumn("Longitude",typeof(string)) });
        foreach (DataRow row in dsResult.Tables["result"].Rows)
        {
            string geometry_id = dsResult.Tables["geometry"].Select("result_id = " + row["result_id"].ToString())[0]["geometry_id"].ToString();
            DataRow location = dsResult.Tables["location"].Select("geometry_id = " + geometry_id)[0];
            dtCoordinates.Rows.Add(row["result_id"], row["formatted_address"], location["lat"], location["lng"]);
        }
    }
    return dtCoordinates;
}

我使用了这个,但我需要<location_type>ROOFTOP</location_type>,但在dsresult中没有找到location_type? - LuckyS
好的,我会检查。 - Manish sharma

6
     /*Ready to use code :  simple copy paste GetLatLong*/
    public class AddressComponent
    {
        public string long_name { get; set; }
        public string short_name { get; set; }
        public List<string> types { 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 Bounds
    {
        public Northeast northeast { get; set; }
        public Southwest southwest { get; set; }
    }

    public class Location
    {
        public double lat { get; set; }
        public double lng { 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 Viewport
    {
        public Northeast2 northeast { get; set; }
        public Southwest2 southwest { get; set; }
    }

    public class Geometry
    {
        public Bounds bounds { get; set; }
        public Location location { get; set; }
        public string location_type { get; set; }
        public Viewport viewport { get; set; }
    }

    public class Result
    {
        public List<AddressComponent> address_components { get; set; }
        public string formatted_address { get; set; }
        public Geometry geometry { get; set; }
        public string place_id { get; set; }
        public List<string> types { get; set; }
    }

    public class RootObject
    {
        public List<Result> results { get; set; }
        public string status { get; set; }
    }


    public static RootObject GetLatLongByAddress(string address)
    {
        var root = new RootObject();

        var url =
            string.Format(
                "http://maps.googleapis.com/maps/api/geocode/json?address={0}&sensor=true_or_false", address);
        var req = (HttpWebRequest)WebRequest.Create(url);

        var res = (HttpWebResponse)req.GetResponse();

        using (var streamreader=new StreamReader(res.GetResponseStream()))
        {
            var result = streamreader.ReadToEnd();

            if (!string.IsNullOrWhiteSpace(result))
            {
                root = JsonConvert.DeserializeObject<RootObject>(result);
            }
        }
        return root;


    }


          /* Call This*/

var destination_latLong = GetLatLongByAddress(um.RouteDestination);

var lattitude =Convert.ToString( destination_latLong.results[0].geometry.location.lat, CultureInfo.InvariantCulture);

 var longitude=Convert.ToString( destination_latLong.results[0].geometry.location.lng, CultureInfo.InvariantCulture);

在我的情况下,谷歌地图给出了地址的结果,但是这种方法给出了纬度/经度的空值结果,可能是什么问题? - Divyang Desai
不,我想从地址字符串中获取经纬度。 - Divyang Desai

0

除了谷歌,OpenStreetMap的Nominatim APIs是一个可选方案。

与谷歌不同,只要您不过度使用(每秒钟超过1个请求),它们就没有每日限制。以下代码适用于Blazor Server。

    [Inject] IJSRuntime _js { get; set; }

    public async Task<Coordinates> GetCoordinates(string city) {
        Coordinates coordinates = new Coordinates();
        string query= String.Format("https://nominatim.openstreetmap.org/search.php?q={0}&format=jsonv2", city);
        string result = GetRequest(query); //returns a stringified array of js objects
        IJSObjectReference objArray= await _js.InvokeAsync<IJSObjectReference>("JSON.parse", result); //parse the string to a js array
        IJSObjectReference obj = await objArray.InvokeAsync<IJSObjectReference>("shift"); //take the first element in the array
        string jsonResult = await _js.InvokeAsync<string>("JSON.stringify", obj);
        dynamic dynamicResult = JObject.Parse(jsonResult);
        coordinates.Latitude= dynamicResult.lat;
        coordinates.Longitude= dynamicResult.lon;
        return coordinates;

    }

    public string GetRequest(string url) {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        request.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36";
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        using (Stream stream = response.GetResponseStream())
        using (StreamReader reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
        }
    }

    public class Coordinates {
        public double Longitude { get; set; }
        public double Latitude { get; set; }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接