[英]get xml element value using url xml
I am trying to get city data from the xml file. 我正在尝试从xml文件获取城市数据。
Below one is a url of XML response from which I am getting a latitude and longitude value from the XML response/file. 下面是一个XML响应的url,从中我从XML响应/文件中获取纬度和经度值。
http://maps.googleapis.com/maps/api/geocode/xml?address=Kenya&sensor=true http://maps.googleapis.com/maps/api/geocode/xml?address=肯尼亚&sensor = true
and this is my JavaScript code:- 这是我的JavaScript代码:
<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. 与此,我试图通过lat和lang值在上面的url中传递Region名称。
http://services.gisgraphy.com/geoloc/search?lat=-0.0235590&lng=37.9061930&radius=100000 . http://services.gisgraphy.com/geoloc/search?lat=-0.0235590&lng=37.9061930&radius=100000 。
With this url I am trying to get asciiName. 使用此网址,我正在尝试获取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. 在ajax
调用中,您缺少一些变量,例如cntry_code
和dataType
。 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.