Javascript 查找当前位置的纬度和经度
我使用此代码查找当前位置的纬度和经度Javascript 查找当前位置的纬度和经度,javascript,jquery,html,firefox,mozilla,Javascript,Jquery,Html,Firefox,Mozilla,我使用此代码查找当前位置的纬度和经度 $(document).ready(function() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(successFunction, errorFunction); } else { alert('It seems like Geolocation, which is required
$(document).ready(function() {
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
}
else
{
alert('It seems like Geolocation, which is required for this page, is not enabled in your browser.');
}
});
function successFunction(position)
{
var lat = position.coords.latitude;
var long = position.coords.longitude;
alert('Your latitude is :'+lat+' and longitude is '+long);
}
function errorFunction(position)
{
alert('Error!');
}
它在Chrome中运行良好,但在Mozilla中却不行。也没有警报错误消息。试试这个我希望这能帮助您:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function initGeolocation()
{
if( navigator.geolocation )
{
// Call getCurrentPosition with success and failure callbacks
navigator.geolocation.getCurrentPosition( success, fail );
}
else
{
alert("Sorry, your browser does not support geolocation services.");
}
}
function success(position)
{
document.getElementById('long').value = position.coords.longitude;
document.getElementById('lat').value = position.coords.latitude
}
function fail()
{
// Could not obtain location
}
</script>
</head>
<body onLoad="initGeolocation();">
<FORM NAME="rd" METHOD="POST" ACTION="index.html">
<INPUT TYPE="text" NAME="long" ID="long" VALUE="">
<INPUT TYPE="text" NAME="lat" ID="lat" VALUE="">
</body>
</html>
函数initGeolocation()
{
if(navigator.geolocation)
{
//使用成功和失败回调调用getCurrentPosition
navigator.geolocation.getCurrentPosition(成功,失败);
}
其他的
{
警报(“对不起,您的浏览器不支持地理定位服务。”);
}
}
功能成功(职位)
{
document.getElementById('long')。value=position.coords.longitude;
document.getElementById('lat')。value=position.coords.latitude
}
函数失败()
{
//无法获取位置
}
您可以尝试以下代码:-
<!DOCTYPE html>
<html>
<body>
<p>Click the button to get your coordinates.</p>
<button onclick="getLocation()">Try It</button>
<p id="demo"></p>
<script>
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;
}
</script>
</body>
</html>
单击按钮获取您的坐标
试试看
var x=document.getElementById(“演示”);
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showPosition);
}否则{
x、 innerHTML=“此浏览器不支持地理位置。”;
}
}
功能显示位置(位置){
x、 innerHTML=“纬度:”+position.coords.Latitude+
“
经度:”+position.coords.Longitude;
}
通过使用下面的jQuery代码,您可以在不使用任何API键的情况下找到当前位置的纬度和经度。让我们试试看
检查您的Firefox开发者控制台(Ctrl+Shift+I,如果安装了Firebug,则检查F12)是否存在脚本错误,或者您的代码是否需要获得使用该功能的权限
托管在addons.mozilla.org上的任何使用地理位置数据的加载项必须在执行此操作之前明确请求mdn Fopun的许可
,它在Chrome和mozilla中对我都有效。问题非常小,您定义了变量名long
checkvar long=
its读取此
$(document).ready(function(){
// get users lat/long
var getPosition = {
enableHighAccuracy: false,
timeout: 9000,
maximumAge: 0
};
function success(gotPosition) {
var uLat = gotPosition.coords.latitude;
var uLon = gotPosition.coords.longitude;
console.log(`${uLat}`, `${uLon}`);
};
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
};
navigator.geolocation.getCurrentPosition(success, error, getPosition);
});