简体   繁体   English

如何在gmap中使用经度和纬度获取地址?

[英]How to get the address using Latitude and Longitude with gmap?

I have here a Windows Form and when I type the latitude and longitude it wont show the complete address in the richtextbox. 我在这里有一个Windows窗体,当我输入纬度和经度时,它不会在richtextbox中显示完整的地址。 Heres my code for the search button: 这是我的搜索按钮代码:

double lat = Convert.ToDouble(textBox8.Text);
double longt = Convert.ToDouble(textBox6.Text);
map.Position = new PointLatLng(lat, longt);
map.MinZoom = 5;
map.MaxZoom = 100;
map.Zoom = 10;

PointLatLng point = new PointLatLng(lat, longt);
GMapMarker marker = new GMarkerGoogle(point, GMarkerGoogleType.blue_dot);

GMapOverlay markers = new GMapOverlay("markers");
markers.Markers.Add(marker);
map.Overlays.Add(markers);

and heres my code for the form load: 这是我的表单加载代码:

GMaps.Instance.Mode = AccessMode.ServerAndCache;
map.CacheLocation = @"cache";
map.DragButton = MouseButtons.Left;
map.ShowCenter = false;
map.DragButton = MouseButtons.Left;
map.MapProvider = GMapProviders.GoogleMap;

You need to reverse-geocode your coordinates. 您需要对坐标进行反向地理编码。 First, you'll need to add these classes to deserialize the reverse-geocoding response. 首先,您需要添加这些类以反序列化反向地理编码响应。 (The Result class is in the GMap.NET.MapProviders namespace.) (Result类位于GMap.NET.MapProviders命名空间中。)

public class ReverseGeocodeResult
{
    public PlusCode plus_code { get; set; }
    public List<Result> results { get; set; }
    public string status { get; set; }
}

public class PlusCode
{
    public string compound_code { get; set; }
    public string global_code { get; set; }
}

Then you can reverse-geocode the coordinates with an HttpClient, but you'll need a Google API key with the geocoding API enabled first. 然后,您可以使用HttpClient对坐标进行反向地理编码,但是您需要先启用地理编码API的Google API密钥。 Get one here if you don't have one. 如果您没有一本,请在这里买一本。

using (var c = new HttpClient())
{
    try
    {
        var result = await c.GetAsync("https://maps.googleapis.com/maps/api/geocode/json?latlng=" 
            + lat.ToString() + "," + longt.ToString() + "&key=YOUR_API_KEY");
        string content = await result.Content.ReadAsStringAsync();
        ReverseGeocodeResult results = JsonConvert.DeserializeObject<ReverseGeocodeResult>(content);
        if (results != null && results.results.Count > 0)
            richTextBox1.Text = results.results[0].formatted_address;
    }
    catch (Exception exc)
    {
        richTextBox1.Text = exc.ToString();
    }
}

Here are the imports to add to the top of your form class: 这是要添加到表单类顶部的导入:

using GMap.NET;
using GMap.NET.MapProviders;
using GMap.NET.WindowsForms;
using GMap.NET.WindowsForms.Markers;
using Newtonsoft.Json;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM