使用ajax从xml网址获取纬度和经度 [英] get the latitude and longitude from the xml url using ajax

查看:94
本文介绍了使用ajax从xml网址获取纬度和经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的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);
}

我正在尝试传递URL城市名称和国家/地区代码并获取xml文件. 尝试jsfiddle:- http://jsfiddle.net/GbDFD/ 我尝试从此xml文件中获取第一个latlng元素值.

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.

我尝试在ajax 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.

如何使用javascript工作.

how can it work using javascript.

谢谢.

推荐答案

您可以使用jsonp,为此您需要在URL中包括format=json

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"

这是工作代码:

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

演示-> http://jsfiddle.net/mohammadAdil/GbDFD/1/

这篇关于使用ajax从xml网址获取纬度和经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆