2011-10-28 165 views
6

我想在我的電腦上測試一些地理位置代碼,但我甚至無法運行這些示例。 雖然他們從文檔網站完美運行,當我嘗試打開從我的電腦HTML文件我得到一個空白頁,所有我做的是試圖檢測我的位置......本地測試谷歌地圖V3地理位置?

下面是代碼:

<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="UTF-8"> 
    <link href="/apis/maps/documentation/javascript/examples/default.css" 
     rel="stylesheet" type="text/css"> 
    <!-- 
    Include the maps javascript with sensor=true because this code is using a 
    sensor (a GPS locator) to determine the user's location. 
    See: http://code.google.com/apis/maps/documentation/javascript/basics.html#SpecifyingSensor 
    --> 
    <script type="text/javascript" 
     src="http://maps.googleapis.com/maps/api/js?sensor=true"></script> 

    <script type="text/javascript"> 
     var map; 

     function initialize() { 
     var myOptions = { 
      zoom: 6, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     map = new google.maps.Map(document.getElementById('map_canvas'), 
      myOptions); 

     // 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> 

請幫助,你能找出我做錯了什麼嗎?

回答

13

您需要在樣式表的鏈接更改爲絕對鏈接:

<link href="http://code.google.com//apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css">