如何使用GPS跟踪jQuery移动应用程序的位置?

时间:2013-03-24 19:47:45

标签: javascript jquery html5 google-maps

我正在开发一个jQuery移动应用程序。我需要跟踪用户的位置(城市地址)。

var x=document.getElementById("demo");
function getLocation()
{
    if (navigator.geolocation)
    {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
    else {
        x.innerHTML="Geolocation is not supported by this browser.";
    }
}

function showPosition(position)
{
    x.innerHTML="Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude;
}

这仅给出纬度和经度。我不需要lat或long或地图。我只需要获得城市地址。我该怎么做才能得到这个?

1 个答案:

答案 0 :(得分:0)

使用Reverse Geocoding尝试,返回JSON。使用以下网址:

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false

获取如下所述的JSON:http://api.jquery.com/jQuery.getJSON/