【openlayers 4 示例】

<!DOCTYPE html>
<html>
    <head>
        <title>zxk示例</title>
        <link rel="stylesheet" href="ol.css" type="text/css">
        <script src='https://cdn.jsdelivr.net/npm/openlayers@4.6.5/dist/ol-debug.js'></script>
        <script src='https://cdn.jsdelivr.net/npm/ol-extent@2.0.1/dist/ol-extent.js'></script>
        <link rel='stylesheet' href='https://cdn.jsdelivr.net/npm/ol-extent@2.0.1/dist/ol-extent.css'>
        <style>
            .ole-js-popup-content {
                width: 200px;
            }
        </style>
    </head>
    <body>
        <div id="map" class="map"></div>
        <script>

            //让图标可以移动
            function makeMarkerMove(feature) {
                var modify = new ol.interaction.Modify({
                    features: new ol.Collection([feature])
                });
                feature.on('change', function() {
                    //移动事件。
                    console.log('Feature Moved To:' + this.getGeometry().getCoordinates());
                }, feature);

                map.addInteraction(modify);
            }

            //创建图标
            function createMarker(map, name, x, y, icon, txt) {

                //创建空的矢量容器
                var vectorSource = new ol.source.Vector({});

                var iconFeature = new ol.Feature({
                    geometry: new ol.geom.Point([x, y],"XY"),
                    name: name,
                });
                //将图标特性添加进矢量中
                vectorSource.addFeature(iconFeature);

                //创建图标样式
                var iconStyle = [new ol.style.Style({
                    image: new ol.style.Icon({
                        opacity: 0.75,
                        src: icon,
                        //"https://openlayers.org/en/latest/examples/data/icon.png"
                    }),

                }), new ol.style.Style({
                    text: new ol.style.Text({
                        text: txt,
                        //"hahahahaha",
                        offsetX: 25,
                        //offsetY: -25,
                        font: '18px roboto,sans-serif',
                        textAlign: 'left',
                        stroke: new ol.style.Stroke({
                            color: '#fff',
                            lineCap: 'butt',
                            width: 4
                        }),
                        fill: new ol.style.Fill({
                            color: '#000'
                        })
                    })
                })];

                //创建图标层
                var vectorLayer = new ol.layer.Vector({
                    source: vectorSource,
                    style: iconStyle
                });

                map.addLayer(vectorLayer);
                return iconFeature;
            }
            ;//地图尺寸
            var extent = [0, 0, 1024, 968];
            var projection = new ol.proj.Projection({
                code: 'xkcd-image',
                units: 'pixels',
                extent: extent
            });

            //创建地图	
            var map = new ol.Map({
                layers: [new ol.layer.Image({
                    //设置地图图片
                    source: new ol.source.ImageStatic({
                        url: 'http://picapi.zhituad.com/photo/388/88/11FED.jpg',
                        projection: projection,
                        imageExtent: extent
                    })
                })],
                target: 'map',
                //设置缩放比例和等级。
                view: new ol.View({
                    projection: projection,
                    center: ol.extent.getCenter(extent),
                    zoom: 1,
                    maxZoom: 10
                }),
                controls: ol.control.defaults({
                    attribution: false,

                })

            });

            console.log("地图中心", map.getView().getCenter())

            var marker1 = createMarker(map, "卧室空调A", 450, 500, "https://openlayers.org/en/latest/examples/data/icon.png", "卧室空调");
            var marker2 = createMarker(map, "客厅空调B", 400, 400, "https://openlayers.org/en/latest/examples/data/icon.png", "客厅空调");

            //让图标可以拖动
            makeMarkerMove(marker1);
            //markMarkerMove(marker2);

            var popover = null;
            map.on('click', function(evt) {
                console.log("当前经纬度", evt.coordinate);
                var feature = map.forEachFeatureAtPixel(evt.pixel, function(feature) {
                    return feature;
                });

                if (feature && feature.getProperties().name) {
                    //获取marker的坐标。
                    var coordinates = feature.getGeometry().getCoordinates();
                    window.f = feature;
                    //获取marker的所有属性。
                    var attr = feature.getProperties().name;

                    //如果没有弹出层html,则创建。
                    if (!popover) {
                        popover = new ole.Popover({
                            // showCloser: false,
                            //showMarkFeature: true,
                            // showMinimize: true,
                            //autoPan: true,
                            offset: [0, -50],
                        });
                        map.addOverlay(popover);
                    }

                    //设置弹出html
                    var content = '<ul>' + '<li>name:' + attr + '</li>' + '</ul>';
                    popover.show(coordinates, content);
                } else {
                    if (popover)
                        popover.hide();
                }
            });
        </script>
    </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值