【问题标题】:Google Map Center set user current position谷歌地图中心设置用户当前位置
【发布时间】:2015-02-04 18:59:28
【问题描述】:

我正在尝试使标记移动并获得 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>

【问题讨论】:

    标签: google-maps geolocation


    【解决方案1】:

    这就够了……

        navigator.geolocation.getCurrentPosition(function(position) {
          var initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
          map.setCenter(initialLocation);
        }, function() {
          //handle no geolocation
        });
    

    只要确保在地图创建后运行它即可。

    顺便说一下,这是来自 Google 地图文档。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 2020-02-29
      • 1970-01-01
      • 2016-01-16
      • 2017-03-06
      相关资源
      最近更新 更多