地理围栏- Google 提供的地图无法读取 属性 未定义的应用
Geofence- Google Maps giving cannot read property apply of undefined
我已经阅读了关于这个主题的所有 post 并从 (How to delete all the shape after draw) and here
我创建了这个 CodePen to demonstrate the issue. Please open your developer console to have a better insight.。
var runMaps = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: position.coords.latitude,
lng: position.coords.longitude
},
zoom: 18
});
var all_overlays = [];
var selectedShape;
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.MARKER,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
//google.maps.drawing.OverlayType.MARKER,
google.maps.drawing.OverlayType.CIRCLE,
google.maps.drawing.OverlayType.POLYGON,
google.maps.drawing.OverlayType.RECTANGLE
]
},
markerOptions: {
icon: 'images/beachflag.png'
},
circleOptions: {
fillColor: '#ffff00',
fillOpacity: 0.2,
strokeWeight: 3,
clickable: false,
editable: true,
zIndex: 1
},
polygonOptions: {
clickable: true,
draggable: true,
editable: true,
fillColor: '#ffff00',
fillOpacity: 1,
},
rectangleOptions: {
clickable: true,
draggable: true,
editable: true,
fillColor: '#ffff00',
fillOpacity: 0.5,
}
});
function clearSelection() {
if (selectedShape) {
selectedShape.setEditable(false);
selectedShape = null;
}
}
function setSelection(shape) {
clearSelection();
selectedShape = shape;
shape.setEditable(true);
google.maps.event.addListener(selectedShape.getPath(), 'insert_at', getPolygonCoords(shape));
google.maps.event.addListener(selectedShape.getPath(), 'set_at', getPolygonCoords(shape));
}
function deleteSelectedShape() {
if (selectedShape) {
selectedShape.setMap(null);
}
}
function deleteAllShape() {
for (var i = 0; i < all_overlays.length; i++) {
all_overlays[i].overlay.setMap(null);
}
all_overlays = [];
}
function CenterControl(controlDiv, map) {
// Set CSS for the control border.
var controlUI = document.createElement('div');
controlUI.style.backgroundColor = '#fff';
controlUI.style.border = '2px solid #fff';
controlUI.style.borderRadius = '3px';
controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
controlUI.style.cursor = 'pointer';
controlUI.style.marginBottom = '22px';
controlUI.style.textAlign = 'center';
controlUI.title = 'Select to delete the shape';
controlDiv.appendChild(controlUI);
// Set CSS for the control interior.
var controlText = document.createElement('div');
controlText.style.color = 'rgb(25,25,25)';
controlText.style.fontFamily = 'Roboto,Arial,sans-serif';
controlText.style.fontSize = '16px';
controlText.style.lineHeight = '38px';
controlText.style.paddingLeft = '5px';
controlText.style.paddingRight = '5px';
controlText.innerHTML = 'Delete Selected Area';
controlUI.appendChild(controlText);
// Setup the click event listeners: simply set the map to Chicago.
controlUI.addEventListener('click', function() {
deleteSelectedShape();
});
}
drawingManager.setMap(map);
var getPolygonCoords = function(newShape) {
console.log("We are one");
var len = newShape.getPath().getLength();
for (var i = 0; i < len; i++) {
console.log(newShape.getPath().getAt(i).toUrlValue(6));
}
};
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
all_overlays.push(event);
if (event.type !== google.maps.drawing.OverlayType.MARKER) {
drawingManager.setDrawingMode(null);
//Write code to select the newly selected object.
var newShape = event.overlay;
newShape.type = event.type;
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
});
setSelection(newShape);
}
});
var centerControlDiv = document.createElement('div');
var centerControl = new CenterControl(centerControlDiv, map);
centerControlDiv.index = 1;
map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(centerControlDiv);
});
}
};
问题
绘制 多边形 后,当我尝试使用边缘上的点对其进行编辑时, 我收到一条错误消息,提示无法读取 属性 apply of undefined.
问题是什么?我该如何解决?对于示例 codepen given,它工作得很好,那为什么不适合我呢?
为了演示这个问题,我在上面给出了一个代码笔。
我需要坐标列表来保存在后端,即将到来,但有一个损坏的 UI。
感谢帮助。
如果有人遇到类似问题。请注意,insert_at
和 set_at
函数必须在 polygon complete 中调用。请查看我的 CodePen 以获得更完整和全面的示例。
我已经阅读了关于这个主题的所有 post 并从 (How to delete all the shape after draw) and here
我创建了这个 CodePen to demonstrate the issue. Please open your developer console to have a better insight.。
var runMaps = function() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: position.coords.latitude,
lng: position.coords.longitude
},
zoom: 18
});
var all_overlays = [];
var selectedShape;
var drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.MARKER,
drawingControl: true,
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
//google.maps.drawing.OverlayType.MARKER,
google.maps.drawing.OverlayType.CIRCLE,
google.maps.drawing.OverlayType.POLYGON,
google.maps.drawing.OverlayType.RECTANGLE
]
},
markerOptions: {
icon: 'images/beachflag.png'
},
circleOptions: {
fillColor: '#ffff00',
fillOpacity: 0.2,
strokeWeight: 3,
clickable: false,
editable: true,
zIndex: 1
},
polygonOptions: {
clickable: true,
draggable: true,
editable: true,
fillColor: '#ffff00',
fillOpacity: 1,
},
rectangleOptions: {
clickable: true,
draggable: true,
editable: true,
fillColor: '#ffff00',
fillOpacity: 0.5,
}
});
function clearSelection() {
if (selectedShape) {
selectedShape.setEditable(false);
selectedShape = null;
}
}
function setSelection(shape) {
clearSelection();
selectedShape = shape;
shape.setEditable(true);
google.maps.event.addListener(selectedShape.getPath(), 'insert_at', getPolygonCoords(shape));
google.maps.event.addListener(selectedShape.getPath(), 'set_at', getPolygonCoords(shape));
}
function deleteSelectedShape() {
if (selectedShape) {
selectedShape.setMap(null);
}
}
function deleteAllShape() {
for (var i = 0; i < all_overlays.length; i++) {
all_overlays[i].overlay.setMap(null);
}
all_overlays = [];
}
function CenterControl(controlDiv, map) {
// Set CSS for the control border.
var controlUI = document.createElement('div');
controlUI.style.backgroundColor = '#fff';
controlUI.style.border = '2px solid #fff';
controlUI.style.borderRadius = '3px';
controlUI.style.boxShadow = '0 2px 6px rgba(0,0,0,.3)';
controlUI.style.cursor = 'pointer';
controlUI.style.marginBottom = '22px';
controlUI.style.textAlign = 'center';
controlUI.title = 'Select to delete the shape';
controlDiv.appendChild(controlUI);
// Set CSS for the control interior.
var controlText = document.createElement('div');
controlText.style.color = 'rgb(25,25,25)';
controlText.style.fontFamily = 'Roboto,Arial,sans-serif';
controlText.style.fontSize = '16px';
controlText.style.lineHeight = '38px';
controlText.style.paddingLeft = '5px';
controlText.style.paddingRight = '5px';
controlText.innerHTML = 'Delete Selected Area';
controlUI.appendChild(controlText);
// Setup the click event listeners: simply set the map to Chicago.
controlUI.addEventListener('click', function() {
deleteSelectedShape();
});
}
drawingManager.setMap(map);
var getPolygonCoords = function(newShape) {
console.log("We are one");
var len = newShape.getPath().getLength();
for (var i = 0; i < len; i++) {
console.log(newShape.getPath().getAt(i).toUrlValue(6));
}
};
google.maps.event.addListener(drawingManager, 'overlaycomplete', function(event) {
all_overlays.push(event);
if (event.type !== google.maps.drawing.OverlayType.MARKER) {
drawingManager.setDrawingMode(null);
//Write code to select the newly selected object.
var newShape = event.overlay;
newShape.type = event.type;
google.maps.event.addListener(newShape, 'click', function() {
setSelection(newShape);
});
setSelection(newShape);
}
});
var centerControlDiv = document.createElement('div');
var centerControl = new CenterControl(centerControlDiv, map);
centerControlDiv.index = 1;
map.controls[google.maps.ControlPosition.BOTTOM_CENTER].push(centerControlDiv);
});
}
};
问题
绘制 多边形 后,当我尝试使用边缘上的点对其进行编辑时, 我收到一条错误消息,提示无法读取 属性 apply of undefined.
问题是什么?我该如何解决?对于示例 codepen given,它工作得很好,那为什么不适合我呢?
为了演示这个问题,我在上面给出了一个代码笔。
我需要坐标列表来保存在后端,即将到来,但有一个损坏的 UI。
感谢帮助。
如果有人遇到类似问题。请注意,insert_at
和 set_at
函数必须在 polygon complete 中调用。请查看我的 CodePen 以获得更完整和全面的示例。