如何在两个顶点之间的折线上添加点?

时间:2019-05-28 14:50:05

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

目标是在每个顶点都具有一个带有自定义标记的可编辑折线。

如我所见,Map API V3不允许这样做。所以我做了这样的可编辑折线:

_context.FridgeTemperture.Where(
        x =>
        x.UpdatedDate >= DateTime.Today
        && x.UpdatedDate < DateTime.Today.AddDays(1)
        && x.Fridgetemp > 26
    ).Count();
let polyline = new self.google.maps.Polyline({
    strokeColor: '#000000',
    strokeOpacity: 0.3,
    strokeWeight: 3,
    editable: false,
    path: path,
    map: self.map
});

polyline.binder = new MVCArrayBinder(polyline.getPath());

let markers = [];
for (let i = 0; i < path.length; i++) {
    let marker = this.getBasicMarker(path[i]);
    marker.bindTo('position', polyline.binder, i.toString());
    markers.push(marker);
}
getBasicMarker(position){
    return new this.google.maps.Marker({
        position: position,
        map: this.map,
        icon: {
            url: "https://maps.gstatic.com/intl/en_us/mapfiles/markers2/measle.png",
            size: new google.maps.Size(7, 7),
            anchor: new google.maps.Point(3.5, 3.5)
        },
        draggable: true,
        visible: false
    });
}

最终折线现在看起来像这样:http://sandbox.rubera.ru/img/2019-05-28_17-04-25.jpg。我可以拖动任何现有的点。拖动点时折线会更新。

现在我必须在两个现有顶点之间添加新顶点。

这就是我被困住的地方。

可能还有另一种解决方案,如何解决任务?

1 个答案:

答案 0 :(得分:0)

如果有人面临类似的任务,这里是我对我的代码的一些修改。那终于帮助我解决了这个问题。

MVCArrayBinder.prototype = new google.maps.MVCObject();
MVCArrayBinder.prototype.get = function (key) {
    if (!isNaN(parseInt(key))) {
        return this.array_.getAt(parseInt(key));
    } else {
        this.array_.get(key);
    }
};

MVCArrayBinder.prototype.set = function (key, val) {
    if (!isNaN(parseInt(key))) {
        this.array_.setAt(parseInt(key), val);
    } else {
        this.array_.set(key, val);
    }
};

MVCArrayBinder.prototype.insertAt = function (key, val) {
    this.array_.insertAt(key, val);
};
google.maps.event.addListener(this.activePoly, 'click', function (e) {
    let path = $this.activePoly.getPath(),
        inserted = false;

    // find line segment
    for (let i = 0; i < path.getLength() - 1, !inserted; i++) {
        let tempPoly = new google.maps.Polyline({
            path: [path.getAt(i), path.getAt(i + 1)]
        });

        if (google.maps.geometry.poly.isLocationOnEdge(e.latLng, tempPoly, 10e-2)) {
            $this.activeRoute.binder.insertAt(i + 1, e.latLng);
            inserted = true;

            let marker = $this.getBasicMarker(e.latLng);
            marker.setVisible(true);

            // Add new marker to array
            $this.activeRoute.markers.splice(i + 1, 0, marker);

            // Have to rebind all markers
            $this.activeRoute.markers.forEach((marker, index) => {
                marker.bindTo('position', $this.activeRoute.binder, index.toString());
            });
        }
    }
});

我的activeRoute是具有以下结构的对象:

{polyline: polyline, markers: markers, binder: polyline.binder}