简体   繁体   中英

get xml element value using url xml

I am trying to get city data from the xml file.
Below one is a url of XML response from which I am getting a latitude and longitude value from the XML response/file.

http://maps.googleapis.com/maps/api/geocode/xml?address=Kenya&sensor=true

and this is my JavaScript code:-

   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
function load(){
        var reg = "Kenya";
                alert(reg);
                var xml;
                $.ajax({
                  url: "http://maps.googleapis.com/maps/api/geocode/xml?address="+reg+"&sensor=true",
                  async: false,
                  dataType:'xml',
                  success: function(data)
                {
                    xml=data;
                }
            });
                var lat = $(xml).find('lat:eq(0)').text();
                var lng = $(xml).find('lng:eq(0)').text();  
                var radius = "100000";
                alert(lat);
                alert(lng);
                 $.ajax({
              url: "http://services.gisgraphy.com/geoloc/search?lat="+lat+"&lng="+lng+"&radius="+radius+"&format=json",
              async: false,
              dataType:'jsonp',
              success: function(data)
            {
                var asciiname = data.result[0].asciiName;
                console.log(asciiname);
            }
        });
            }
</script>
<body onload="load()">
</body>

with this I am trying to pass Region name in above url with lat and lang value.

http://services.gisgraphy.com/geoloc/search?lat=-0.0235590&lng=37.9061930&radius=100000 .

With this url I am trying to get asciiName.
But it's not working and itss display nothing.
What I am doing wrong. Help me to solve my problem.
Thanks.

Here is your full code details. You're missing some variables like cntry_code and dataType in ajax call. If your question is solved then accept it.

<script>
    function getlg(){
            var cntry_code = 'IN';
            var reg = 'Rajkot';
            var xml;
            $.ajax({
                url: "http://services.gisgraphy.com//geocoding/geocode?address="+reg+"&country="+cntry_code+"&format=json",
                async: false,
                dataType:'jsonp',
                success: function(data){ 
                    var id = data.result[1].id;
                    console.log(id);
                    var lat = data.result[1].lat;
                    console.log(lat);
                    var lng = data.result[1].lng;  
                    console.log(lng);
                    var radius = "100000";
                    $.ajax({
                          url: "http://services.gisgraphy.com/geoloc/search?lat="+lat+"&lng="+lng+"&radius="+radius+"&format=json",
                          async: false,
                          dataType:'jsonp',
                          success: function(data)
                          {
                                for(i=0;i<data.result.length;i++)
                                {
                                    var asciiname = data.result[i].asciiName;
                                    console.log(asciiname);
                                }
                          }
                    });
                }
            });
        }
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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