我使用谷歌地图绘制多边形并具有添加多边形对象的功能
这是多边形的侦听器
drawingManager = new google.maps.drawing.DrawingManager({
drawingMode: google.maps.drawing.OverlayType.RECTANGLE,
drawingControlOptions: {
drawingModes: [
google.maps.drawing.OverlayType.RECTANGLE,
google.maps.drawing.OverlayType.POLYGON
]
}
});
drawingManager.addListener('polygoncomplete', function(polygon) {
currentShape = addPolygonPlace('unsaved', polygon.getPath().getArray(), '');
polygon.setMap(null);
drawingManager.setMap();
});
这是添加多边形的功能。
function addPolygonPlace(id, paths, colors) {
var place = new Polygon(id, paths, colors);
mapWrapper.addPlace(place);
place.addEventListener('mouseover', function() {
placeInfobox.update(place);
placeInfobox.show();
});
place.addEventListener('mouseout', function() {
placeInfobox.hide();
});
place.addEventListener('click', function() {
if (selectedShape) {
selectedShape.setEditable(false);
google.maps.event.trigger(selectedShape, 'edit_end');
}
place.setEditable(true);
selectedShape = place;
});
google.maps.event.addListener(place, 'edit_end', function() {
if (place.id === 'unsaved') {
return;
}
var placeToEdit = getPlaceByDescription(place.id);
var path = place.googleEntity.getPath().getArray();
updatePlace(place.id);
});
return place;
}
从这一行也可以看出 var place = new Polygon(id, paths, colors);
我使用了带有属性的 Polygon 对象,这是它的代码。
var Polygon = (function() {
'use strict';
function Polygon(id, paths, colors) {
this._visible = true;
this.id = id;
this.paths = paths;
// this.infoboxDescription = infoboxDescription;
if (colors === undefined || colors === null) {
colors = {
fillColor: '#006400',
strokeColor: '#646464'
};
}
if (colors.fillColor === undefined || colors.fillColor === null) {
colors.fillColor = '#006400';
}
if (colors.strokeColor === undefined || colors.strokeColor === null) {
colors.strokeColor = '#646464';
}
this.googleEntity = new google.maps.Polygon({
strokeColor: colors.strokeColor,
fillColor: colors.fillColor,
paths: paths
});
}
Object.defineProperty(Polygon.prototype, 'visible', {
get: function() {
return this._visible || (this._visible = false);
},
enumerable: true,
configurable: true
});
Polygon.prototype.remove = function() {
this.googleEntity.setMap(null);
}
Polygon.prototype.addEventListener = function(eventName, callback) {
this.googleEntity.addListener(eventName, callback.bind(this));
}
Polygon.prototype.setEditable = function(editable) {
this.googleEntity.setEditable(editable);
}
return Polygon
})()
但我有错误。在这一行
if (colors.fillColor === undefined || colors.fillColor === null) { colors.fillColor = '#006400'; }
我有这个
无法在 addPolygonPlace (Index.js:1895) 处的新多边形 (Place.js:65) 处的字符串 '' 上创建属性 'fillColor'
我该如何解决?
"use strict";
var colors = 'string';
colors.fillColor = 'abc';
未捕获的类型错误:无法在字符串 'string' 上创建属性 'fillColor'
"use strict";
var colors = {};
colors.fillColor = 'abc';
没有错误
如果这需要任何评论:您不能在类型为 的变量上创建对象属性string
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句