您可以在JavaScript中使用Geolocation API ...来获取当前位置使用此:
navigator.geolocation.getCurrentPosition(function(position) {
do_something(position.coords.latitude, position.coords.longitude);
});
Run Code Online (Sandbox Code Playgroud)
do_something()可能是更新地图或只显示当前的经度和纬度.很简单的例子
浏览器支持(来源):
navigator.geolocation.getCurrentPosition(
onSuccess,
onError, {
enableHighAccuracy: true,
timeout: 20000,
maximumAge: 120000
});
function onSuccess(position) {
//the following are available to use
//position.coords.latitude
//position.coords.longitude
// position.coords.altitude
// position.coords.accuracy
// position.coords.altitudeAccuracy
// position.coords.heading
// position.coords.speed
}
Run Code Online (Sandbox Code Playgroud)
警告:这将在浏览器中弹出一个如下所示的权限对话框(Safari):
