ol3 geometryFunction interaction.Draw添加GPS点

时间:2017-05-09 13:48:24

标签: openlayers openlayers-3

在我正在开发的项目中,我们使用ol.interaction.Draw功能来绘制几何体。我们用几何函数将GPS点添加到几何体(线串或多边形)。这是通过设置布尔值来添加此GPS点来实现的。这个布尔值是由HTML中的一些按钮设置的。

Al运行良好,但草图未更新,直到我们再次将光标移动到地图上。有没有办法触发geometryFunction而不将光标移动到地图上?

// Interaction
        $scope.interactions.draw = new ol.interaction.Draw({
            source: $scope.vector.getSource(),
            type: (function () {
                var type = 'Point';
                if ($scope.layer.TypeName == 'LineStyle')
                    type = 'LineString';
                if ($scope.layer.TypeName == 'PolygonStyle')
                    type = 'Polygon';
                return type;
            })(),
            geometryFunction: function (coordinates, geometry) {

                if (!_.isUndefined(geometry)) {
                    // Is move to GPS position selected?
                    if ($scope.moveToGpsPosition_) {
                        // GPS position
                        var pos = _geolocation.geolocation.getPosition();
                        // Line
                        if ($scope.layer.TypeName == 'LineStyle')
                            coordinates.splice(coordinates.length - 1, 0, pos);
                        // Polygon
                        if ($scope.layer.TypeName == 'PolygonStyle')
                            coordinates[0].splice(coordinates.length - 2, 0, pos);
                        // Stop move to GPS position
                        $scope.moveToGpsPosition_ = false;
                    }
                    geometry.setCoordinates(coordinates);
                } else {

                    // Detect geometry type
                    if ($scope.layer.TypeName == 'PointStyle')
                        geometry = new ol.geom.Point(coordinates);
                    if ($scope.layer.TypeName == 'LineStyle')
                        geometry = new ol.geom.LineString(coordinates);
                    if ($scope.layer.TypeName == 'PolygonStyle')
                        geometry = new ol.geom.Polygon(coordinates);
                }
                return geometry;
            }
        });

1 个答案:

答案 0 :(得分:0)

我很难复制您的问题,但我怀疑您可以通过在geometry.setCoordinates(coordinates);行之后添加以下其中一项来解决问题:

map.updateSize(); 要么 map.render();