C# 从地址c获取纬度经度#

C# 从地址c获取纬度经度#,c#,asp.net,xml,rest,C#,Asp.net,Xml,Rest,我正在使用geocoder的Web服务从地址、城市和州获取lat和long。当我使用下面的例子时,我没有得到lat和long。我做错什么了吗 请求地址:加利福尼亚州洛杉矶102街东1125号 这是我的密码: private void GetLatLongFromAddress(string street, string city, string state) { string geocoderUri = string.Format(@"http://rpc.geocoder.us/ser

我正在使用geocoder的Web服务从地址、城市和州获取lat和long。当我使用下面的例子时,我没有得到lat和long。我做错什么了吗

请求地址:加利福尼亚州洛杉矶102街东1125号

这是我的密码:

private void GetLatLongFromAddress(string street, string city, string state)
{
    string geocoderUri = string.Format(@"http://rpc.geocoder.us/service/rest?address={0},{1},{2}", street, city, state);
    XmlDocument geocoderXmlDoc = new XmlDocument();
    geocoderXmlDoc.Load(geocoderUri);
    XmlNamespaceManager nsMgr = new XmlNamespaceManager(geocoderXmlDoc.NameTable);
    nsMgr.AddNamespace("geo", @"http://www.w3.org/2003/01/geo/wgs84_pos#");
    string sLong = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"//geo:long", nsMgr).InnerText;
    string sLat = geocoderXmlDoc.DocumentElement.SelectSingleNode(@"//geo:lat", nsMgr).InnerText;

    Latitude = Double.Parse(sLat);
    Longitude = Double.Parse(sLong);
}
lat和long返回0。我也尝试添加邮政编码,但没有成功。有没有更好的Web服务可以提供最新的结果

提前谢谢。

我经常使用。您可以使用以下请求进行地理编码:

http://dev.virtualearth.net/REST/v1/Locations/CA/adminDistrict/postalCode/locality/addressLine?includeNeighborhood=includeNeighborhood&key=BingMapsKey
你可以看到它在使用中的一个例子

例如,您的地址:

http://dev.virtualearth.net/REST/v1/Locations/US/1125%20E.%20102nd%20Street,%20Los%20Angeles,%20CA?key=Ai9-KNy6Al-r_ueyLuLXFYB_GlPl-c-_iYtu16byW86qBx9uGbsdJpwvrP4ZUdgD
答复如下:

{
   "authenticationResultCode":"ValidCredentials",
   "brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png",
   "copyright":"Copyright © 2012 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.",
   "resourceSets":[
      {
         "estimatedTotal":1,
         "resources":[
            {
               "__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1",
               "bbox":[
                  33.940492293415652,
                  -118.26180800227225,
                  33.948217728557005,
                  -118.24939194889963
               ],
               "name":"1125 E 102ND St, Los Angeles, CA 90002",
               "point":{
                  "type":"Point",
                  "coordinates":[
                     33.944355010986328,
                     -118.25559997558594
                  ]
               },
               "address":{
                  "addressLine":"1125 E 102ND St",
                  "adminDistrict":"CA",
                  "adminDistrict2":"Los Angeles Co.",
                  "countryRegion":"United States",
                  "formattedAddress":"1125 E 102ND St, Los Angeles, CA 90002",
                  "locality":"Los Angeles",
                  "postalCode":"90002"
               },
               "confidence":"High",
               "entityType":"Address",
               "geocodePoints":[
                  {
                     "type":"Point",
                     "coordinates":[
                        33.944355010986328,
                        -118.25559997558594
                     ],
                     "calculationMethod":"Parcel",
                     "usageTypes":[
                        "Display"
                     ]
                  },
                  {
                     "type":"Point",
                     "coordinates":[
                        33.944118499755859,
                        -118.25559997558594
                     ],
                     "calculationMethod":"Interpolation",
                     "usageTypes":[
                        "Route"
                     ]
                  }
               ],
               "matchCodes":[
                  "Good"
               ]
            }
         ]
      }
   ],
   "statusCode":200,
   "statusDescription":"OK",
   "traceId":"81518ba504a3494bb0b62bdb6aa4b291|LTSM002104|02.00.83.500|LTSMSNVM001473, LTSMSNVM001463, LTSMSNVM001452, LTSMSNVM001851, LTSMSNVM001458, LTSMSNVM001462"
}
或者。。。对于XML数据,添加
o=XML

http://dev.virtualearth.net/REST/v1/Locations/US/1125%20E.%20102nd%20Street,%20Los%20Angeles,%20CA?o=xml&key=Ai9-KNy6Al-r_ueyLuLXFYB_GlPl-c-_iYtu16byW86qBx9uGbsdJpwvrP4ZUdgD
其中:

<Response>
  <Copyright>Copyright ? 2012 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright>
  <BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri>
  <StatusCode>200</StatusCode>
  <StatusDescription>OK</StatusDescription>
  <AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
  <TraceId>b9af8a6b058b455ca9c368b9e32142fe|LTSM002102|02.00.83.500|LTSMSNVM002001, LTSMSNVM001464, LTSMSNVM001451, LTSMSNVM001452, LTSMSNVM001457, LTSMSNVM002052, LTSMSNVM001461</TraceId>
  <ResourceSets>
    <ResourceSet>
      <EstimatedTotal>1</EstimatedTotal>
      <Resources>
        <Location>
          <Name>1125 E 102ND St, Los Angeles, CA 90002</Name>
          <Point>
            <Latitude>33.944355010986328</Latitude>
            <Longitude>-118.25559997558594</Longitude>
          </Point>
          <BoundingBox>
            <SouthLatitude>33.940492293415652</SouthLatitude>
            <WestLongitude>-118.26180800227225</WestLongitude>
            <NorthLatitude>33.948217728557005</NorthLatitude>
            <EastLongitude>-118.24939194889963</EastLongitude>
          </BoundingBox>
          <EntityType>Address</EntityType>
          <Address>
            <AddressLine>1125 E 102ND St</AddressLine>
            <AdminDistrict>CA</AdminDistrict>
            <AdminDistrict2>Los Angeles Co.</AdminDistrict2>
            <CountryRegion>United States</CountryRegion>
            <FormattedAddress>1125 E 102ND St, Los Angeles, CA 90002</FormattedAddress>
            <Locality>Los Angeles</Locality>
            <PostalCode>90002</PostalCode>
          </Address>
          <Confidence>High</Confidence>
          <MatchCode>Good</MatchCode>
          <GeocodePoint>
            <Latitude>33.944355010986328</Latitude>
            <Longitude>-118.25559997558594</Longitude>
            <CalculationMethod>Parcel</CalculationMethod>
            <UsageType>Display</UsageType>
          </GeocodePoint>
          <GeocodePoint>
            <Latitude>33.944118499755859</Latitude>
            <Longitude>-118.25559997558594</Longitude>
            <CalculationMethod>Interpolation</CalculationMethod>
            <UsageType>Route</UsageType>
          </GeocodePoint>
        </Location>
      </Resources>
    </ResourceSet>
  </ResourceSets>
</Response>

版权?2012年微软及其供应商。版权所有。未经微软公司明确书面许可,不得访问本API,不得以任何方式使用、复制或传播本API的内容和任何结果。
http://dev.virtualearth.net/Branding/logo_powered_by.png
200
好啊
有效存款
b9af8a6b058b455ca9c368b9e32142fe | LTSM002102 | 02.00.83.500 | LTSMSNVM002001、LTSMSNVM001464、LTSMSNVM001451、LTSMSNVM001452、LTSMSNVM001457、LTSMSNVM002052、LTSMSNVM001461
1.
加利福尼亚州洛杉矶市东102街1125号,邮编90002
33.944355010986328
-118.25559997558594
33.940492293415652
-118.26180800227225
33.948217728557005
-118.24939194889963
地址
东102街1125号
加利福尼亚州
洛杉矶公司。
美国
加利福尼亚州洛杉矶市东102街1125号,邮编90002
洛杉矶
90002
高
好
33.944355010986328
-118.25559997558594
包裹
展示
33.944118499755859
-118.25559997558594
插值
路线

因此,给定地址的lat/long为(33.9,-118.2)

谷歌地图有一个返回lat/long的web服务API,适用于主要国家。有关示例,请参见。支持不同的输出格式,包括CSV、JSON等

我对geocoder.us不太熟悉,但谷歌地图在美国地址上确实很管用

编辑1: 有关支持的国家/地区的列表,请参阅

编辑2: 为了增强相关性,这里有一个针对您的地址的示例Google Maps API调用:

按地址获取纬度和经度-Microsoft BingMapsRESTToolkit 微软有一个官方软件可以帮助你轻松地使用它。为此,您需要安装BingMapsRESTToolkit

在这里,您有兴趣通过地址获取位置。您可以通过将
Query
属性设置为指定的地址,或将
address
属性设置为
address
SimpleAddress
的实例来指定地址

示例:

var request = new GeocodeRequest();
request.BingMapsKey = "Your Map Key";

request.Query = "The Postal address";

//OR
//request.Address = new SimpleAddress()
//{
//    CountryRegion = "The Country",
//    AddressLine = "The official street line of an address relative to the area",
//    AdminDistrict = "The subdivision name in the country or region for an address",
//    Locality = "The locality, such as the city or neighborhood", 
//    PostalCode = "The postal code",
//};

var result = await request.Execute();
if (result.StatusCode == 200)
{
    var toolkitLocation = (result?.ResourceSets?.FirstOrDefault())
        ?.Resources?.FirstOrDefault() as BingMapsRESTToolkit.Location;
    var latitude = toolkitLocation.Point.Coordinates[0];
    var longitude = toolkitLocation.Point.Coordinates[1];

    // Use latitude and longitude
}
更多信息:

var request = new GeocodeRequest();
request.BingMapsKey = "Your Map Key";

request.Query = "The Postal address";

//OR
//request.Address = new SimpleAddress()
//{
//    CountryRegion = "The Country",
//    AddressLine = "The official street line of an address relative to the area",
//    AdminDistrict = "The subdivision name in the country or region for an address",
//    Locality = "The locality, such as the city or neighborhood", 
//    PostalCode = "The postal code",
//};

var result = await request.Execute();
if (result.StatusCode == 200)
{
    var toolkitLocation = (result?.ResourceSets?.FirstOrDefault())
        ?.Resources?.FirstOrDefault() as BingMapsRESTToolkit.Location;
    var latitude = toolkitLocation.Point.Coordinates[0];
    var longitude = toolkitLocation.Point.Coordinates[1];

    // Use latitude and longitude
}

这不是一个真正的问题。这是一个关于Web服务的问题。请检查您的问题。这是关于一个特定的web服务,而不是c#。我试过你的代码GetLatLongFromAddress(“1125 E.102nd Street”,“Los Angeles”,“CA”);它返回的纬度为33.944015,纬度为-118.255818。如何调用方法?此函数对我有效,返回正确的值。也许问题在于你的物体的纬度和经度?当你跨过它时,长跑和长跑的价值是什么?@AnthonyBlake,一个特定的、任意大小或发展水平的国家,我确信。作为回答,国家被添加为链接