2016-03-05 62 views

回答

0

您可以通过等待位置的回调位置发送到您的API与XMLHttpRequest做到这一点。

下面是一个例子:

if (navigator.geolocation){ 
    navigator.geolocation.getCurrentPosition(sendLocation); 
} else { 
    alert("Geolocation is not supported by this browser."); 
} 

function sendLocation(position){ 
    var latlng = {lat: position.coords.latitude, lng: position.coords.longitude}; 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
     alert('Success'); 
    } 
    }; 
    xhttp.open("POST", "uploadurl", true); 
    xhttp.send(JSON.stringify(latlng)); 
} 
+0

太棒了!非常感谢!只需要一次后续操作,如果我在用户完成发布后将用户重定向到另一个URL,我将使用什么操作? – frekn0

+0

在我的例子中,只需使用'window.location ='url''语句来改变页面而不是alert('success')''函数。 – bvx89

相关问题