简体   繁体   English

Google地图标记getPosition()无效 - “__ e3_”错误

[英]Google Maps marker getPosition() not working - '__e3_' error

I have been struggling to get the below code to work. 我一直在努力让下面的代码工作。 The aim of the code is to be able to Geolocate the person if they allow geolocation, then when the marker is placed be able to get the Lat and Lng of the marker as it is being dragged around. 代码的目的是能够在人员进行地理定位时对其进行地理定位,然后在放置标记时能够在拖动时获取标记的Lat和Lng。

Currently the map displays fine and the marker is geolocated in the correct place. 目前地图显示正常,标记在正确的位置进行地理定位。 But when you begin to drag it, no Lat and Lng is passed to updateMarkerPosition. 但是当你开始拖动它时,没有Lat和Lng传递给updateMarkerPosition。

The error which I am getting in my chrome console is: *Uncaught TypeError: Cannot read property '_ e3 ' of undefined* 我在Chrome控制台中遇到的错误是:*未捕获的TypeError:无法读取未定义的属性'_ e3 '*

Here is the script I am using, also displaying the files which I include to the page. 这是我正在使用的脚本,也显示我包含在页面中的文件。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=true"></script>




function initialize() {
  var mapOptions = {
    zoom: 8,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById('mapCanvas'),
      mapOptions);

  // Try HTML5 geolocation
  if(navigator.geolocation) {
    // Geolocation found
            navigator.geolocation.getCurrentPosition(function(position) {
              var pos = new google.maps.LatLng(position.coords.latitude,
                                               position.coords.longitude);


              var marker = new google.maps.Marker({
                position: pos,
                title: 'Your Location',
                map: map,
                draggable: true
              });

              map.setCenter(pos);

            }, function() {
                // User has cancelled and not let the browser find them using geolocation

                var pos = new google.maps.LatLng(-34.397, 150.644);
                var map = new google.maps.Map(document.getElementById('mapCanvas'), {
                  zoom: 8,
                  center: pos,
                  mapTypeId: google.maps.MapTypeId.ROADMAP
                });

                var marker = new google.maps.Marker({
                  position: pos,
                  title: 'Point A',
                  map: map,
                  draggable: true
                });

            });

  } else {
    // Browser doesn't support Geolocation
          var pos = new google.maps.LatLng(-34.397, 150.644);
          var map = new google.maps.Map(document.getElementById('mapCanvas'), {
            zoom: 8,
            center: pos,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          });

          var marker = new google.maps.Marker({
            position: pos,
            title: 'Point A',
            map: map,
            draggable: true
          });

  }



  google.maps.event.addListener(marker, 'drag', function() {
    updateMarkerPosition(marker.getPosition());
  });


}

google.maps.event.addDomListener(window, 'load', initialize);

Code for updateMarkerPosition. updateMarkerPosition的代码。 This just updated two form elements on the page which display the Lat and Lng: 这刚刚更新了页面上显示Lat和Lng的两个表单元素:

function updateMarkerPosition(pos) {
  $('#posLat').val(pos.lat());
  $('#posLong').val(pos.lng());
}

geolocation is asynchronous. 地理定位是异步的。 The marker is not defined until the callback runs. 在回调运行之前,不会定义标记。 You need to assign the listener inside the callback function (where the marker is created). 您需要在回调函数(创建标记的位置)内分配侦听器。

 // Try HTML5 geolocation
  if(navigator.geolocation) {
    // Geolocation found
            navigator.geolocation.getCurrentPosition(function(position) {
              var pos = new google.maps.LatLng(position.coords.latitude,
                                               position.coords.longitude);


              var marker = new google.maps.Marker({
                position: pos,
                title: 'Your Location',
                map: map,
                draggable: true
              });
              google.maps.event.addListener(marker, 'drag', function() {
                updateMarkerPosition(marker.getPosition());
              });

              map.setCenter(pos);

            }, function() {
                // User has cancelled and not let the browser find them using geolocation

                var pos = new google.maps.LatLng(-34.397, 150.644);
                var map = new google.maps.Map(document.getElementById('mapCanvas'), {
                  zoom: 8,
                  center: pos,
                  mapTypeId: google.maps.MapTypeId.ROADMAP
                });

                var marker = new google.maps.Marker({
                  position: pos,
                  title: 'Point A',
                  map: map,
                  draggable: true
                });
                google.maps.event.addListener(marker, 'drag', function() {
                  updateMarkerPosition(marker.getPosition());
                });

            });

  } else {
    // Browser doesn't support Geolocation
          var pos = new google.maps.LatLng(-34.397, 150.644);
          var map = new google.maps.Map(document.getElementById('mapCanvas'), {
            zoom: 8,
            center: pos,
            mapTypeId: google.maps.MapTypeId.ROADMAP
          });

          var marker = new google.maps.Marker({
            position: pos,
            title: 'Point A',
            map: map,
            draggable: true
          });
          google.maps.event.addListener(marker, 'drag', function() {
            updateMarkerPosition(marker.getPosition());
          });

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM