2010-04-01 4 views
2

나는 xml을 반환하는 Google지도 링크가 있습니다. 그 XML에서 특정 태그 값을 원한다. 어떤 사람이 저에게 어떻게 제안 할 수 있습니까? 링크 :Google api에서 xml 데이터를 구문 분석

http://maps.google.com/maps/api/geocode/xml?address=1270 Broadway Ste 803, New York, NY 10001, USA&sensor=false 

답변

4

다음은 사전의 좌표를 반환하는 함수입니다. 참고 : 이것은 새로운 V3 API와 함께 있습니다. API 키가 필요하지 않습니다. 센서가 있습니다.

// Figure out the geocoordinates for the location 
private Dictionary<string, string> GeoCode(string location) 
{ 
    // Initialize the dictionary with empty values. 
    Dictionary<string, string> dic = new Dictionary<string, string>(); 
    dic.Add("latitude", ""); 
    dic.Add("longitude", ""); 

    try 
    { 
     string url = "http://maps.google.com/maps/api/geocode/xml?address=" + System.Web.HttpUtility.UrlEncode(location) + "&sensor=false"; 
     XmlDocument doc = new XmlDocument(); 
     doc.Load(url); 
     XmlNodeList nodes = doc.SelectNodes("//location"); 

     // We're assuming there's only one location node in the xml. 
     foreach (XmlNode node in nodes) 
     { 
      dic["latitude"] = node.SelectSingleNode("lat").InnerText; 
      dic["longitude"] = node.SelectSingleNode("lng").InnerText; 
     } 
    } 
    catch (Exception) 
    { 
     // If anything goes wrong, we want to get empty values back. 
     dic["latitude"] = ""; 
     dic["longitude"] = ""; 
    } 

    return dic; 
} 
1

은 사용하려는 언어에 따라 다릅니다. 파이썬으로 개발한다면, 파이썬의 minidom parser을 사용할 수 있습니다.

이이 파이썬에서 구문 분석 할 수있는 방법의 예 :

>>> import urllib 
>>> from xml.dom import minidom 
>>> URL="http://maps.google.com/maps/api/geocode/xml?address=1270%20Broadway%20Ste%20803,%20New%20York,%20NY%2010001,%20USA&sensor=false" 
>>> loc = dom.getElementsByTagName('location')[0] 
>>> lat = loc.getElementsByTagName('lat')[0].firstChild.data 
>>> lng = loc.getElementsByTagName('lng')[0].firstChild.data 
>>> print lat, lng 
40.7486930 -73.9877870 
>>> 
+0

내가 asp.net에서 나를 도울 수 있다는 것을 잊어서 죄송합니다. asp.net을 사용하고 있습니다. – pankaj

+0

아무 생각도하지 않습니다. tbh ... google을 시도해보십시오. 적어도 하나의 예를 들어 보겠습니다. http : //www.daniweb.com/forums/thread30083.html 연령대별로 창을 사용하지 않았습니다 ... – rytis

0

당신은 * nix에서 스크립트에 있고 당신은 상관없이 계층 구조에서 현 위치, 특정 태그의 값을보고 싶다면,

$ s="http://maps.google.com/maps/api/geocode/xml?address=1270 Broadway Ste 803, New York, NY 10001, USA&sensor=false" 
$ wget -O- -q "$s" | awk '/<lat>|<lng>/' # getting </lat> tags and <lng> tags 
0

json 서비스를 사용하고 XML을 구문 분석하지 않으려면 어떻게해야합니까? this을 시도하면 무슨 뜻인지 이해할 수 있습니다.