谷歌地图浮动销在中心和返回位置

google maps float pin at center and return location

我希望将图钉固定在Google地图的中心,同时用户可以移动地图并进行缩放。关键是销的当前GPS位置(当然仍在中心)以值的形式递归返回。我发现了这个

Google Map API V2-当用户滚动地图时,如何在屏幕中央保持标记?

但是我这不返回当前的中心坐标。我一直在寻找带有代码的演示实现,但找不到任何东西,将非常感谢您的帮助。


在daftlogic.com上的此示例中,修改为使用标记而不是十字准线,将中心坐标放在页面上的HTML元素中。

要点:

  • 标记绑定到地图的中心
  • \\'center_changed \\'用于更新页面上显示的坐标。
  • 工作片段:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    function initialize() {
      var crosshairShape = {
        coords: [0, 0, 0, 0],
        type: 'rect'
      };
      var latlng = new google.maps.LatLng(54.62279178711505, -5.895538330078125);
      var myOptions = {
        zoom: 12,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.SATELLITE,
        mapTypeControlOptions: {
          style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
        }
      };
      var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      var marker = new google.maps.Marker({
        map: map,
      });
      document.getElementById('coordinates').innerHTML ="center coordinates:" + map.getCenter().toUrlValue(6)
      google.maps.event.addListener(map, 'center_changed', function() {
        document.getElementById('coordinates').innerHTML ="center coordinates:" + map.getCenter().toUrlValue(6);
      });
      marker.bindTo('position', map, 'center');
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    1
    2
    3
    4
    5
    6
    7
    8
    html,
    body,
    #map_canvas {
      height: 500px;
      width: 500px;
      margin: 0px;
      padding: 0px
    }
    1
    <script src="https://maps.googleapis.com/maps/api/js">

    工作jsfiddle