【问题标题】:getting null in "selectId(infoId)" in gmap-v3-geofence.js在 gmap-v3-geofence.js 中的“selectId(infoId)”中获取 null
【发布时间】:2019-09-30 11:22:14
【问题描述】:
Geofence.prototype.setInfo = function(polygon, infoId) 
{
    selectId(polygon).value = getCoordinatesString(polygon);
}

gmap-v3-geofence.js:208 Uncaught TypeError: Cannot set property 'value' of null at Geofence.setInfo (gmap-v3-geofence.js:208) at 在 geofencing.js:17 处绘制多边形(geofencing.js:27)

需要一些认真的帮助。我正在使用来自 Github 的以下代码。 Click Here

【问题讨论】:

  • 什么时候出现这个错误?
  • 在页面加载时,实际上我想要如果我在选定区域内点击,它会给我点击区域的纬度,经度。
  • 您能发布您的完整代码或 jsfiddle 吗?据我所见,演示工作没有问题sujancse.github.io/geofence
  • 感谢@evan。我已更改代码,使用谷歌多边形地图并对其进行更改以满足我的要求。
  • 对!所以我知道你现在自己解决了这个问题?如果是这样,为了社区的缘故,请随意回答您自己的问题。如果您仍然遇到此问题,请分享您修改后的代码,以便我们提供帮助。

标签: javascript google-maps geofencing


【解决方案1】:

我的问题是在谷歌地图上放一个部分,我也可以拖动和调整大小,所以我使用以下代码并得到了我需要的结果:

<script>
// This example creates a simple polygon representing the Bermuda Triangle.
  // When the user clicks on the polygon an info window opens, showing
  // information about the polygon's coordinates.
  var map;
  var infoWindow;
  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      zoom: 7,
      center: {lat: 31.448809299757535, lng: -99.08335389822349},
      mapTypeId: 'terrain'
    });
    // Define the LatLng coordinates for the polygon.
    var triangleCoords = [
        {lat: 31.422875949492543, lng: -99.89672607421875},
        {lat: 31.03122750234828, lng: -98.66225048828124},
        {lat: 32.02807714324144, lng: -98.65806201171876},
        {lat: 31.735314763657264, lng: -99.22246240234375}
    ];
    // Construct the polygon.
    var bermudaTriangle = new google.maps.Polygon({
      paths: triangleCoords,
      strokeColor: '#FF0000',
      strokeOpacity: 0.8,
      strokeWeight: 3,
      fillColor: '#FF0000',
      fillOpacity: 0.35,
      editable: true,
      draggable: true,
    });
    bermudaTriangle.setMap(map);
    // Add a listener for the click event.
    bermudaTriangle.addListener('click', showArrays);
    infoWindow = new google.maps.InfoWindow;
  }
  /** @this {google.maps.Polygon} */
  function showArrays(event) {
    // Since this polygon has only one path, we can call getPath() to return the
    // MVCArray of LatLngs.
    var vertices = this.getPath();
    var contentString = '<b><strong>Selected polygon</strong></b><br>' +
        'Clicked location: <br>' + event.latLng.lat() + ',' + event.latLng.lng() +
        '<br>';
    // Iterate over the vertices.
    for (var i =0; i < vertices.getLength(); i++) {
      var xy = vertices.getAt(i);
      contentString += '<br>' + 'Coordinate ' + i + ':<br>' + xy.lat() + ',' +
          xy.lng();
    }
    // Replace the info window's content and position.
    infoWindow.setContent(contentString);
    infoWindow.setPosition(event.latLng);
    infoWindow.open(map);
  }

在此之前,我使用的是已弃用的 geofence-v3-map.js。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-14
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    相关资源
    最近更新 更多