get the latitude and longitude from the xml url using ajax

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

Here is my jQuery ajax :-

function getlg(){     var cntry_code = 'IN';     var reg = 'Rajkot';     var xml;     $.ajax(     {         url: "http://services.gisgraphy.com//geocoding/geocode?address="+reg+"&country="+cntry_code+"",         async: false,         dataType:'xml',         success: function(data)         {             xml=data;         }     });     var lat = $(xml).find('lat:eq(0)').text();     alert(lat);     var lng = $(xml).find('lng:eq(0)').text();       alert(lng); }

I am trying to pass in URL city name and country code and get xml file. try jsfiddle :-http://jsfiddle.net/GbDFD/ From this xml file i am try to get first lat and lng element value.

This is working url.

I am try to pass the city name and country code in ajax url its work but not return me latitude and longitude value.

how can it work using javascript.

thanks.

回答1:

You can use jsonp , For that you need to include format=json in your url

"http://services.gisgraphy.com//geocoding/geocode?address="+reg+"&country="+cntry_code+"&format=json"

Here is the working Code :

$.ajax({     url: "http://services.gisgraphy.com//geocoding/geocode?address=" + reg + "&country=" + cntry_code + "&format=json",     async: false,     dataType: 'jsonp',     success: function (data) {            var lat = data.result[0].lat;            console.log(lat);            var lng = data.result[0].lng;            console.log(lng);        } });

Demo --> http://jsfiddle.net/mohammadAdil/GbDFD/1/



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!