2012-01-20 38 views
8

Tôi đang sử dụng dịch vụ web từ bộ mã hóa địa lý để nhận được lat và dài từ địa chỉ, thành phố và tiểu bang. Khi tôi sử dụng ví dụ dưới đây, tôi không nhận được lat và dài. Tôi có làm gì sai ở đây không.Nhận Kinh độ Vĩ độ từ một địa chỉ C#

địa chỉ yêu cầu: 1125 đường E. thứ 102, Los Angeles, CA

Đây là mã của tôi:

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); 
} 

Các lat và dài trở lại với 0. Tôi cố gắng để thêm mã zip quá nhưng điều đó không hiệu quả. Có bất kỳ dịch vụ web nào tốt hơn có thể cung cấp cho tôi kết quả cập nhật hay không.

Xin cảm ơn trước.

+0

[Google maps] (http://code.google.com/apis/maps/faq.html#geocoder_exists) thực hiện điều này –

+1

Đây không thực sự là câu hỏi C#. Đây là câu hỏi về dịch vụ web. –

+0

Vui lòng xem lại câu hỏi của bạn. Đây là về một dịch vụ web cụ thể, không phải là C#. – Strillo

Trả lời

4

Tôi thường sử dụng số Bing Maps Rest APIs. Bạn có thể địa lý mã sử dụng các yêu cầu như sau:

http://dev.virtualearth.net/REST/v1/Locations/CA/adminDistrict/postalCode/locality/addressLine?includeNeighborhood=includeNeighborhood&key=BingMapsKey 

Bạn có thể nhìn thấy nó được sử dụng trong một codeproject article I wrote on reactive extensions.

Ví dụ, địa chỉ của bạn:

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

Cho các phản ứng sau:

{ 
    "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" 
} 

Hoặc ... cho dữ liệu XML, thêm 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 

Mà cho:

<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> 

Vì vậy, các lat/dài cho địa chỉ cho bạn là (33,9, -118,2)

1

Google Maps có một API dịch vụ web mà trả lat/dài, và hoạt động tốt cho các nước lớn. Xem http://maps.google.com/maps/geo?q=1%205th%20ave,new%20york,ny,us để biết ví dụ. Các định dạng đầu ra khác nhau được hỗ trợ bao gồm CSV, http://maps.google.com/maps/geo?q=1%205th%20ave,new%20york,ny,us&output=csv, JSON, v.v.

Tôi không quen với geocoder.us, nhưng bản đồ của Google chắc chắn hoạt động tốt cho địa chỉ ở Hoa Kỳ.

Sửa 1: Xem http://code.google.com/apis/maps/faq.html#whatcountries cho một danh sách các nước hỗ trợ

Chỉnh sửa 2: Đối phù hợp hơn, đây là một mẫu Google Maps API gọi cho địa chỉ của bạn trong câu hỏi: http://maps.google.com/maps/geo?q=1125%20E.%20102nd%20Street,%20Los%20Angeles,%20CA

+1

các quốc gia chính là gì? – AnthonyBlake

+0

@AnthonyBlake Các nước có kích thước, mức độ lệch tùy ý nhất định hoặc mức độ lệch, tôi chắc chắn là vậy. –

+0

Các quốc gia được thêm làm liên kết trong câu trả lời –

0

tôi đã sử dụng Yahoo API trong quá khứ với thành công. Điều này link cho bạn thấy cách sử dụng nó với C#

Hy vọng điều đó sẽ hữu ích.

Các vấn đề liên quan