无法创建属性' fillColor'在字符串''

时间:2018-02-20 10:23:14

标签: javascript google-maps google-maps-api-3

我使用谷歌地图绘制多边形,并具有添加多边形对象的功能

这是多边形的监听器

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();
});

这是添加Polygon的功能。

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&#39 ;;    }

我有这个

  

无法创建属性' fillColor'在字符串''       在新的Polygon(Place.js:65)       在addPolygonPlace(Index.js:1895)

我如何解决?

1 个答案:

答案 0 :(得分:0)

"use strict";

var colors = 'string';
colors.fillColor = 'abc';
  

未捕获的TypeError:无法在字符串'string'

上创建属性'fillColor'
"use strict";

var colors = {};
colors.fillColor = 'abc';
  

没有错误

如果需要任何评论:您无法在string类型的变量上创建object property