Google maps 谷歌地图中心设置用户当前位置

Google maps 谷歌地图中心设置用户当前位置,google-maps,geolocation,Google Maps,Geolocation,我正在努力使标记移动并获得lat lng。我的代码工作正常。但现在我想将我的地图中心设置为我的当前位置。我正在尝试使用地理位置代码,但当我添加getCurrentPosition时,地图不会显示 我的移动标记地图代码是 var marker, map; function initialize() { var myLatlng = new google.maps.LatLng(53.871963457471786,10.689697265625);

我正在努力使标记移动并获得lat lng。我的代码工作正常。但现在我想将我的地图中心设置为我的当前位置。我正在尝试使用地理位置代码,但当我添加getCurrentPosition时,地图不会显示

我的移动标记地图代码是

 var marker, map;

      function initialize() {
        var myLatlng = new google.maps.LatLng(53.871963457471786,10.689697265625);      

                var mapOptions = {
          zoom: 4,
          center: myLatlng,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        } 
        map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);

        marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: 'Hello World!'
        });

      }
      $(function() {
        initialize();

        google.maps.event.addListener(map, 'click', function(event) {    
            var lat=event.latLng.lat();
            var lng=event.latLng.lng();
            $('#latlng').val(lat+', '+lng); 
          marker.animateTo(event.latLng);
        });
      }); 

   <input type="text" id="latlng" size="40" /><br />
    <div id="map_canvas"></div>
var标记,map;
函数初始化(){
var myLatlng=new google.maps.LatLng(53.871963457471786,10.689697265625);
变量映射选项={
缩放:4,
中心:myLatlng,
mapTypeId:google.maps.mapTypeId.ROADMAP
} 
map=new google.maps.map(document.getElementById('map_canvas'),mapOptions);
marker=新的google.maps.marker({
职位:myLatlng,
地图:地图,
标题:“你好,世界!”
});
}
$(函数(){
初始化();
google.maps.event.addListener(映射,'click',函数(事件){
var lat=event.latLng.lat();
var lng=event.latLng.lng();
$('#latlng').val(lat+,'+lng);
marker.animateTo(event.latLng);
});
}); 


这应该足够了

    navigator.geolocation.getCurrentPosition(function(position) {
      var initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      map.setCenter(initialLocation);
    }, function() {
      //handle no geolocation
    });
只需确保在创建地图后运行此操作

顺便说一下,这是来自谷歌地图文档