Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义的地图类 #4

Open
topcss opened this issue Dec 4, 2018 · 0 comments
Open

自定义的地图类 #4

topcss opened this issue Dec 4, 2018 · 0 comments

Comments

@topcss
Copy link
Owner

topcss commented Dec 4, 2018

<!DOCTYPE html>
<html>

<head>
    <title>Draw and Modify Features</title>
    <link rel="stylesheet" href="http://localhost:42000/assets/js/map/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
    <script src="http://localhost:42000/assets/js/map/ol.js"></script>
</head>

<body>
    <div id="map" class="map"></div>
    
    <script>
        var raster = new ol.layer.Tile({
            source: new ol.source.OSM()
        });

        var map = new ol.Map({
            layers: [raster],
            target: 'map',
            view: new ol.View({
                center: [-11000000, 4600000],
                zoom: 4
            })
        });
        

        var MapController = function (map, callback) {

            var drawkSettings = {
                fill: new ol.style.Fill({
                    color: 'rgba(255, 255, 255, 0.2)'
                }),
                stroke: new ol.style.Stroke({
                    color: '#ffcc33',
                    width: 2
                }),
                image: new ol.style.Circle({
                    radius: 7,
                    fill: new ol.style.Fill({
                        color: '#ffcc33'
                    })
                })
            }

            var features = new ol.Collection();
            var featureOverlay = new ol.layer.Vector({
                source: new ol.source.Vector({ features: features }),
                style: new ol.style.Style(drawkSettings)
            });
            featureOverlay.setMap(map);

            // propties

            // 创建修改交互控件
            var modify = new ol.interaction.Modify({
                features: features,
                change: changed,

                deleteCondition: function (event) {
                    changed();

                    return ol.events.condition.shiftKeyOnly(event) &&
                        ol.events.condition.singleClick(event);
                }
            });

            function changed() {
                // 修改之后触发
                if (callback) callback();
            }

            // 创建绘制交互控件
            var draw = new ol.interaction.Draw({ features: features, type: 'Point', change: changed });

            // methods
            this.startModify = function () {
                this.stopModify();

                map.addInteraction(modify);
            }
            this.startDraw = function (type) {
                this.stopDraw();

                draw = new ol.interaction.Draw({
                    features: features,
                    type: type || 'Point'
                });
                map.addInteraction(draw);
            }
            this.stopModify = function () {
                map.removeInteraction(modify);
            }
            this.stopDraw = function () {
                map.removeInteraction(draw);
            }
            this.addPoint = function (x, y) {
                features.push(new ol.Feature(new ol.geom.Point([x, y])));

                changed();
            }
            this.getPoints = function () {
                return features;
            }
        }


        // code 
        var myMapController = new MapController(map,
            function () {
                console.log(myMapController.getPoints().getArray()[0].getGeometry().getCoordinates());
            });
        myMapController.addPoint(0, 0);

    </script>
</body>

</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant