openlayers基础使用和一些简单交互

引入文件

<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>

创建dom节点

<div id="map" class="map"></div>

定义样式

必须要定义好长和宽 才能正常使用

<style>
      .map {
        height: 400px;
        width: 100%;
      }
    </style>

初始化

var map = new ol.Map({
                        controls: ol.control.defaults({
                            attribution: false,
                            rotate: false,
                            zoom: false
                        }),
                        target: 'container',
                        layers: [
                            new ol.layer.Tile({
                                // source: new ol.source.OSM()  这个使用openstreemap 国内可用,但是无法将国内显示改为英文
                                source: new ol.source.TileImage({url: 'http://www.google.cn/maps/vt/pb=!1m4!1m3!1i{z}!2i{x}!3i{y}!2m3!1e0!2sm!3i380072576!3m8!2szh-EN!3scn!5e1105!12m4!1e68!2m2!1sset!2sRoadmap!4e0!5m1!1e0'}) //谷歌地图支持中国地区显示英文和中文,其他国家显示英文和当地国家语言
                            })
                        ],
                        view: new ol.View({
                            center: ol.proj.fromLonLat(centerPoint),
                            zoom: 10
                        })
                    });

地图上画一个点

 var vectorsource = new ol.source.Vector({});   //创建一个vector
var geometrypt = new ol.geom.Point(ol.proj.fromLonLat([item.lon, item.lat])); //点的坐标 
var feature = new ol.Feature({
        geometry: geometrypt,
        name: item.yy_name,
        yy_tel: item.yy_tel,
        remarkContent: item.remark,
        id: item.id,
        status: item.status
}); //创建一个feture
vectorsource.addFeature(feature); //将feture添加到 vector中

var layer = new ol.layer.Vector({
        source: vectorsource,
        style: new ol.style.Style({
                            image: new ol.style.Icon({
                                src: '../../image/icon2.png'
                            }),
                            text: new ol.style.Text({
                                font: '12px Calibri,sans-serif',
                                text: item.yy_name,
                                fill: new ol.style.Fill({
                                    color: '#DC143C'
                                }),
                                stroke: new ol.style.Stroke({
                                    color: '#fff',
                                    width: 3
                                })
                            })
                        })
}); // 创建一个layer

map.addLayer(layer); //添加layer到地图中

点击一个点

//设置点击事件
                    var selectClick = new ol.interaction.Select({
                        condition: ol.events.condition.singleClick,
                        style: function (feature) {
                            // console.log(feature);

                            var coordinate = feature.get('geometry').flatCoordinates;
                            var id = feature.get('id');
                            var yy_tel = feature.get('yy_tel');
                            var remarkContent = feature.get('remarkContent');

                            content.innerHTML = "<div>Appointment phone:"   yy_tel  
                                "</div><div>Remarks:"   (remarkContent ? remarkContent :
                                    'No remarks')  
                                "<a href=\'javascript:;\' class=\'map_box_btn\' onclick=\'app.openyuyue("  
                                id   ")\'>details</a></div>";
                            overlay.setPosition(coordinate);
                            return pointStyle[id];
                        }
                    });
                    map.addInteraction(selectClick);//激活一个点击事件

使用openwrite发布

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值