navigator.Geolocation GetCurrentpositon不适用于Chrome和Firefox [英] navigator.Geolocation GetCurrentpositon isn't working on Chrome and Firefox

查看:835
本文介绍了navigator.Geolocation GetCurrentpositon不适用于Chrome和Firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在线学习本教程,教您如何使用HTML5的新内置功能和谷歌地图api来获取当前位置(地理位置)。

 ˚<!DOCTYPE html> 
< html>

< head>
< meta charset =UTF-8>
< title> Navigator< / title>
< / head>
< script src =js / jquery.js>< / script>
< script src =http://maps.google.com/maps/api/js?sensor=true>< / script>

< script>

x = navigator.geolocation;

x.getCurrentPosition(success,failure);

函数成功(位置){
var mylat = position.coords.latitude;
var mylong = position.coords.longitude;
$('#lat')。htm(mylat);
$('#long')。html(mylong);


函数失败(){
$('#lat')。html(< h3>无坐标!< / h3>) ;
}
< / script>

< body>
<! - 地图占位符 - >
< div id =map>

< / div>

< div id =lat>< / div>
< div id =long>< / div>
< / body>



< / html>

我试着在google和firefox上运行这段代码。
谷歌阻止我看到我目前的位置。我收到失败消息没有坐标可用!
至于firefox,它的效果非常好!



Chrome在这里遇到了什么问题?一位朋友建议我应该尝试使用我的IP地址获取我的位置。该怎么办?并且Chrome浏览器有没有办法阻止我的当前位置?

解决方案

我使用此代码适用于IE / Chrome / FF移动浏览器几乎所有

  if(navigator.geolocation){
var latitude = null;
var longitude = null;
navigator.geolocation.getCurrentPosition(function(position){
latitude = position.coords.latitude;
longitude = position.coords.longitude;
});

}其他{
alert(浏览器不支持地理定位API);
};


I am following this tutorial online that teaches you how to use the new built in features of HTML5 and google map api to get your current location (Geolocation).

˚<!DOCTYPE html>
<html>

<head>
<meta charset="UTF-8">
<title>Navigator</title>
</head>
<script src="js/jquery.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=true"></script>

<script>

x = navigator.geolocation;

x.getCurrentPosition(success, failure);

function success(position) {
    var mylat = position.coords.latitude;
    var mylong = position.coords.longitude;
    $('#lat').htm(mylat);
    $('#long').html(mylong);
}

function failure() {
    $('#lat').html("<h3> No co-ordinates available!</h3>");
}
</script>

<body>
<!--map placeholder -->
<div id="map">

</div>

<div id="lat"></div>
<div id="long"></div>
</body>



</html>

I tried to run this code on both google and firefox. Google is blocking me from seeing my current position. I get the fail message "No co-ordinates available!" As for firefox, it works so great!

What is the issue here with Chrome? A friend suggested that i should try getting my location using my IP address. What to do? And is there a way around Chrome blocking my current location?

解决方案

I use this code works in IE/Chrome/FF mobile browsers almost everything

    if (navigator.geolocation) {
    var latitude = null;
    var longitude = null;
    navigator.geolocation.getCurrentPosition(function (position) {
        latitude = position.coords.latitude;
        longitude = position.coords.longitude;
   });  

 } else {
    alert("Geolocation API is not supported in your browser");
 };

这篇关于navigator.Geolocation GetCurrentpositon不适用于Chrome和Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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