2014-01-16 20 views
1

我已經從谷歌的教程中複製了確切的代碼並添加了我的API密鑰所以從代碼的角度看不出任何錯誤。但是,我的瀏覽器不允許地理定位,因此在那裏肯定有一些錯誤。誰能告訴我我錯過了什麼。這裏有一些屏幕截圖,我可以幫助理解我的問題。默認情況下,允許此頁Google地圖api用法地理定位我不知道最新錯誤?

我的位置設置

My location setting allowed by default to this page

我的谷歌計劃顯示,所有谷歌地圖和地理定位服務在

My Google Project showing that all google maps and geolocation services are On

錯誤對話框給我,當我加載頁面

Error dialog giving me when i load the page

在單擊該管理設置還我檢查我打開位置所有

On clicking that manage settings also i checked that i turned ON location for all

我真的不知道什麼是錯?這裏如果任何使用的代碼 - 從

https://developers.google.com/maps/documentation/javascript/examples/map-geolocation 

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Geolocation</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <style> 
     html, body, #map-canvas { 
     height: 100%; 
     margin: 0px; 
     padding: 0px 
     } 
    </style> 
    <!-- 
    Include the maps javascript with sensor=true because this code is using a 
    sensor (a GPS locator) to determine the user's location. 
    See: https://developers.google.com/maps/documentation/javascript/tutorial#Loading_the_Maps_API 
    --> 
    <script src="https://maps.googleapis.com/maps/api/js?key={My_API_KEY}&v=3.exp&sensor=true"></script> 

    <script> 
// Note: This example requires that you consent to location sharing when 
// prompted by your browser. If you see a blank space instead of the map, this 
// is probably because you have denied permission for location sharing. 

var map; 

function initialize() { 
    var mapOptions = { 
    zoom: 6 
    }; 
    map = new google.maps.Map(document.getElementById('map-canvas'), 
     mapOptions); 

    // Try HTML5 geolocation 
    if(navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
     var pos = new google.maps.LatLng(position.coords.latitude, 
             position.coords.longitude); 

     var infowindow = new google.maps.InfoWindow({ 
     map: map, 
     position: pos, 
     content: 'Location found using HTML5.' 
     }); 

     map.setCenter(pos); 
    }, function() { 
     handleNoGeolocation(true); 
    }); 
    } else { 
    // Browser doesn't support Geolocation 
    handleNoGeolocation(false); 
    } 
} 

function handleNoGeolocation(errorFlag) { 
    if (errorFlag) { 
    var content = 'Error: The Geolocation service failed.'; 
    } else { 
    var content = 'Error: Your browser doesn\'t support geolocation.'; 
    } 

    var options = { 
    map: map, 
    position: new google.maps.LatLng(60, 105), 
    content: content 
    }; 

    var infowindow = new google.maps.InfoWindow(options); 
    map.setCenter(options.position); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 

    </script> 
    </head> 
    <body> 
    <div id="map-canvas"></div> 
    </body> 
</html> 

請注意,這是在Mozilla Firefox正常工作,但不是在鉻

回答

2

出於安全原因的Chrome禁用GeoLocation中爲來自與文件系統資源file:\\方案。如果你在IIS中託管你的頁面,你應該很好。

+0

好的,酷的Din知道,非常感謝! – DeadMan