简体   繁体   English

c#:将地址转换为经度和纬度

[英]c#:converting address to longitude and latitude

i was trying to convert address into longitude and latitude using this code using geocoding api 我试图使用地理编码API使用此代码将地址转换为经度和纬度

protected void Button1_Click(object sender, EventArgs e)
{
    IGeocoder geocoder = new GoogleGeocoder() {  };
    Address[] addresses = geocoder.Geocode("#65/1 bangalore").ToArray();
    foreach (Address adrs in addresses)
    {
        Response.Write("address:"+adrs);
    }
}

the answer i got was the more detailed address not the longitude and latitude .for example for this code i got 我得到的答案是更详细的地址,而不是经度和纬度。例如,对于此代码,我得到了

address:Hari Om Trust Tapovana, 1, Bangalore, Karnataka 562123, India 

someone please tell me what modification i have to do so that i could get longitude and latitude? 有人请告诉我我必须进行哪些修改才能获得经度和纬度?

the code for googlegeocoder is googlegeocoder的代码是

 #region Assembly Geocoding.dll, v3.1.0.0
    // C:\Users\Admin\Documents\Visual Studio 2012\WebSites\learn1\Bin\Geocoding.dll
    #endregion

    using Geocoding;
    using System;
    using System.Collections.Generic;
    using System.Net;
    using System.Threading;
    using System.Threading.Tasks;

    namespace Geocoding.Google
    {
        public class GoogleGeocoder : IGeocoder, IAsyncGeocoder
        {
            public GoogleGeocoder();

            public string ApiKey { get; set; }
            public Bounds BoundsBias { get; set; }
            public BusinessKey BusinessKey { get; set; }
            public string Language { get; set; }
            public WebProxy Proxy { get; set; }
            public string RegionBias { get; set; }
            public string ServiceUrl { get; }

            public IEnumerable<GoogleAddress> Geocode(string address);
            public Task<IEnumerable<GoogleAddress>> GeocodeAsync(string address);
            public Task<IEnumerable<GoogleAddress>> GeocodeAsync(string address, CancellationToken cancellationToken);
            public IEnumerable<GoogleAddress> ReverseGeocode(Location location);
            public IEnumerable<GoogleAddress> ReverseGeocode(double latitude, double longitude);
            public Task<IEnumerable<GoogleAddress>> ReverseGeocodeAsync(double latitude, double longitude);
            public Task<IEnumerable<GoogleAddress>> ReverseGeocodeAsync(double latitude, double longitude, CancellationToken cancellationToken);

            protected class RequestState
            {
                public readonly CancellationToken? cancellationToken;
                public readonly HttpWebRequest request;

                public RequestState(HttpWebRequest request, CancellationToken? cancellationToken);
            }
        }
    }

Ahh... quite simple in Fact: 啊...其实很简单:

protected void Button1_Click(object sender, EventArgs e)
{
    IGeocoder geocoder = new GoogleGeocoder() {  };
    Address[] addresses = geocoder.Geocode("#65/1 bangalore").ToArray();
    foreach (Address adrs in addresses)
    {
        Response.Write("address:"+adrs.Coordinates);
    }
}

Cheers 干杯

EDIT: The reason you're only seeing a street address is because of Address.ToString() being called by default in your loop. 编辑:您只看到街道地址的原因是因为默认情况下在循环中调用了Address.ToString()。

Refer to : Google has a geocoding API which seems to work pretty well for most of the locations that they have Google Maps data for. 请参阅:Google有一个地理编码API,对于他们拥有Google Maps数据的大多数位置而言,它似乎都可以很好地工作。

http://googlemapsapi.blogspot.com/2006/06/geocoding-at-last.html http://googlemapsapi.blogspot.com/2006/06/geocoding-at-last.html

They provide online geocoding (via JavaScript): 他们提供在线地理编码(通过JavaScript):

http://code.google.com/apis/maps/documentation/services.html#Geocoding http://code.google.com/apis/maps/documentation/services.html#Geocoding

Or backend geocoding (via an HTTP request): 或后端地理编码(通过HTTP请求):

http://code.google.com/apis/maps/documentation/services.html#Geocoding_Direct http://code.google.com/apis/maps/documentation/services.html#Geocoding_Direct

The data is usually the same used by Google Maps itself. 数据通常与Google Maps本身使用的数据相同。 (note that there are some exceptions to this, such as the UK or Israel, where the data is from a different source and of slightly reduced quality) (请注意,有一些例外情况,例如英国或以色列,它们的数据来自其他来源且质量略有下降)

Here is good and simple example: 这是一个简单的好例子:

Adress to Lat- long generation 面向长久一代

Thanks 谢谢

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

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