浏览器中的当前位置标记(蓝色圆圈)

时间:2012-10-03 15:42:57

标签: javascript google-maps-api-3

  

可能重复:
  How to highlight a user's current location in google maps?

是否有任何方法可以使用javascript google maps api在当前位置获取蓝色圆圈标记?

2 个答案:

答案 0 :(得分:14)

您可以使用GeolocationMarker library。它是Google Maps API Utility Library

的一部分

答案 1 :(得分:7)

使用navigator.geolocation方法,并使用蓝色圆圈的自定义图标

https://developer.mozilla.org/en-US/docs/Using_geolocation

https://developers.google.com/maps/documentation/javascript/overlays#Icons

编辑:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Google Maps User Location</title>
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <style>
    html, body, #map_canvas{width:100%;height:100%;}
    </style>
  </head>
  <body onload="locate()">
    <div id="map_canvas"></div>
  </body>
  <script>
    var im = 'http://www.robotwoods.com/dev/misc/bluecircle.png';
    function locate(){
        navigator.geolocation.getCurrentPosition(initialize,fail);
    }

    function initialize(position) {
        var myLatLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        var mapOptions = {
          zoom: 4,
          center: myLatLng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(document.getElementById('map_canvas'),
                                      mapOptions);
        var userMarker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: im
        });
      }

     function fail(){
         alert('navigator.geolocation failed, may not be supported');
     }
    </script>
</html>