2012-04-24 114 views
2

我一直在尋找網絡上的解決方案,我仍然無法找到任何解決方案,iv'd一直在谷歌自己的libery找到這個。谷歌地圖JavaScript API v3的GPS座標爲中心

我想提取座標,從GPS作爲我的谷歌地圖的中心使用,所以當你在FX。在Kongs nytorv的哥本哈根,你會以這個位置爲中心。

我有一些選項,我這裏的地圖:

var myOptions = { 
     center: new google.maps.LatLng(55.674, 12.403), 
     zoom: 10, 
     disableDefaultUI: true, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     mapTypeControl: false, 
     mapTypeControlOptions: { 
     style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR, 
     position: google.maps.ControlPosition.BOTTOM_CENTER 
     }, 
     panControl: true, 
     panControlOptions: { 
      position: google.maps.ControlPosition.TOP_RIGHT 
     }, 
     zoomControl: true, 
     zoomControlOptions: { 
      style: google.maps.ZoomControlStyle.LARGE, 
      position: google.maps.ControlPosition.LEFT_CENTER 
     }, 
     scaleControl: false, 
     scaleControlOptions: { 
      position: google.maps.ControlPosition.BOTTOM_CENTER 
     }, 
     streetViewControl: true, 
     streetViewControlOptions: { 
      position: google.maps.ControlPosition.BOTTOM_CENTER 
     } 
    }; 

現在我有一個靜態的位置爲中心。
但我不能找到從哪裏

我一直在試圖解決這個問題,拉動信息,以便我們都得到解決的問題,如果可能的

回答

3

下面是谷歌文檔的例子

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

這裏是代碼,如果這個環節被打破:

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Google Maps JavaScript API v3 Example: Map Geolocation</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="UTF-8"> 
    <link href="/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: https://developers.google.com/apis/maps/documentation/javascript/basics#SpecifyingSensor 
    --> 
    <script type="text/javascript" 
     src="https://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> 
+0

不能在本地主機上測試,因爲我一直在發送錯誤。 只是添加我有最新版本的鉻,所以我支持HTML5 – 2012-04-25 09:37:23

+0

Thx我發現它是我的瀏覽器阻止傳感器,所以我打開訪問,現在它工作謝謝 – 2012-04-25 09:58:08