Google Geo Location Api无效

时间:2015-03-24 11:57:22

标签: google-maps

问题:地理定位服务不起作用,也不确定原因。

对问题的建议:我在这里读到了堆栈溢出,可能是因为我从桌面运行程序而不是服务器。但是我尝试了修复它并没有用。此外,我还没有添加一个信息窗口,如谷歌页面上显示的api,但据我所知,它是不需要的。

代码:请注意,这只是问题所在的摘录。

var myLatlng = new google.maps.LatLng(-27.4667,153.0333);
            var mapOptions = {
                zoom: 13,
                center: myLatlng
            }
            var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
            alert('Section 1: Pass');
             if(navigator.geolocation) { 
             alert('Section 2: Pass');

                //Get the location
                navigator.geolocation.getCurrentPosition(function(position) {

                    alert('Section 3: Pass');
                    //Declate pos ()
                    myLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                    mapOptions.center(pos);
                    alert('Success');


            }, function() {
                    alert('Section 3: The Geolocation service failed.');
            });
            } else {
            // Browser doesn't support Geolocation
            Alert('Section 2: Your browser doesn\'t support geolocation.');
            }

结果: 第1节:通行证 第2节:通行证 第3节:地理定位服务失败

2 个答案:

答案 0 :(得分:1)

尝试此解决方案以获取客户位置

if (navigator.geolocation) {
    alert('Your browser support this feature');
    navigator.geolocation.getCurrentPosition(showPosition);
} else {
    alert('Your browser does not support this feature');
}
function showPosition(position) {
    alert("Latitude: " + position.coords.latitude + "\nLongitude: " + position.coords.longitude); 
}

答案 1 :(得分:0)

我还没有发表评论,所以我的猜测就是答案。

您的浏览器中是否关闭了位置?我的浏览器问我是否要提交我的职位。您可以尝试以下代码:http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_geolocation

另一个猜测是让你的代码适应这个:



if (navigator.geolocation) {
  alert("My browser supports Geolocations");
  navigator.geolocation.getCurrentPosition(alertPosition);
} else {
   alert("My browser doesn't support Geolocations");
}

function alertPosition(position) {
  alert("My Position is Lat:" + position.coords.latitude +
    ", Lng: " + position.coords.longitude);
}




但是我认为这不会在代码片段中发挥作用,因为它不是你的位置,而是服务器片段正在运行的服务器的位置,并且不太可能告诉你它的位置。

相关问题