首页 > 技术文章 > openlayers添加地图标记

zhoushangwu 2018-08-09 14:54 原文

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8" />
    <title></title>
    <link rel="stylesheet" href="lib/OpenLayers/ol.css" type="text/css">

    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="lib/OpenLayers/ol.js"></script>
    <script src="olStyle/Light.js"></script>
    <style>
        html,
        body {
            width: 100%;
            height: 100%;
            margin: 0;
        }

        .map {
            width: 100%;
            height: 100%;
            background: #f6f6f4;
        }
    </style>
</head>

<body>
    <div id="map" class="map" data-id="11"></div>
    <script type="text/javascript">  
        var map;

        $(function () {

            InitMap();

            AddPoint();

        })

        var layer;

        //地图初始化
        function InitMap() {
            //初始化地图
            layer = new ol.layer.Vector({
                source:new ol.source.Vector()
            })

            map = new ol.Map({
                layers: [layer],
                target: 'map',
                view: new ol.View({
                    center: ol.proj.fromLonLat([120.277, 36.330]),
                    minZoom: 1,
                    zoom: 16
                })
            });
        }

        /*增加坐标点**********************************************************************************/

        //增加坐标点
        function AddPoint() {
            //设置位置坐标
            //经纬度转坐标
            var point = ol.proj.fromLonLat([120.277, 36.330]);

            //如果类型是矢量标注则添加矢量标签,否则添加覆盖标签
            addVectorLabel("", point);
        }

        //添加矢量标签
        function addVectorLabel(title, coordinate) {
            //初始化一个新的点要素
            var newFeature = new ol.Feature({
                geometry: new ol.geom.Point(coordinate),
                name: title
            });
            //设置点的样式
            newFeature.setStyle(createLabelStyle(newFeature));

            //清楚坐标点
            layer.getSource().clear();

            //将当前要素添加到矢量数据源中
            layer.getSource().addFeature(newFeature);
        }

        //创建样式
        function createLabelStyle(feature) {
            //返回一个样式
            return new ol.style.Style({
                //把点的样式换成ICON图标
                image: new ol.style.Icon({
                    //设置图标偏移
                    anchor: [0.5, 1],
                    //标注样式的起点位置
                    anchorOrigin: 'top-right',
                    //X方向单位:分数
                    anchorXUnits: 'fraction',
                    //Y方向单位:像素
                    anchorYUnits: 'pixels',
                    //偏移起点位置的方向
                    offsetOrigin: 'top-right',
                    //透明度
                    opacity: 0.9,
                    //图片路径
                    //src: 'images/map.png'
                    src:'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png'
                }),
                //文本样式
                text: new ol.style.Text({
                    //对齐方式
                    textAlign: 'center',
                    //文本基线
                    textBaseline: 'middle',
                    //字体样式
                    font: 'normal 14px 微软雅黑',
                    //文本内容
                    //text: feature.get('name'),
                    text: "",
                    //填充样式
                    fill: new ol.style.Fill({
                        color: '#aa3300'
                    }),
                    //笔触
                    stroke: new ol.style.Stroke({
                        color: '#ffcc33',
                        width: 2
                    })
                }),
                //层
                zIndex: 20
            });
        };


    </script>
</body>

</html>

  

推荐阅读