美文网首页
四章-48-使用GPX数据渲染要素

四章-48-使用GPX数据渲染要素

作者: 彩云飘过 | 来源:发表于2020-04-07 18:49 被阅读0次

本文基于腾讯课堂老胡的课《跟我学Openlayers--基础实例详解》做的学习笔记,使用的openlayers 5.3.x api。

源码 见 1048.html ,对应的 官网示例 https://openlayers.org/en/latest/examples/gpx.html

image.png image.png
<!DOCTYPE html>
<html>

<head>
   <title>GPX</title>
   <link rel="stylesheet" href="../include/ol.css" type="text/css" />
   <link rel="stylesheet" href="../include/bootstrap.css" type="text/css" />
   <script src="../include/jquery.js"></script>
   <script src="../include/ol.js"></script>
   <script src="../include/bootstrap.min.js"></script>
</head>
<style>

</style>

<body>
   <div id="info">&nbsp;</div>

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

   <script>
       // 必应瓦片地图
       var raster = new ol.layer.Tile({
           source: new ol.source.BingMaps({
               imagerySet: 'Aerial',
               key: 'As1HiMj1PvLPlqc_gtM7AqZfBL8ZL3VrjaS3zIb22Uvb9WKhuJObROC-qUpa81U5'
           })
       });

       // 样式
       var style = {
           'Point': new ol.style.Style({
               image: new ol.style.Circle({
                   fill: new ol.style.Fill({
                       color: 'rgba(255,255,0,0.4)'
                   }),
                   radius: 5,
                   stroke: new ol.style.Stroke({
                       color: '#ff0',
                       width: 1
                   })
               })
           }),
           'LineString': new ol.style.Style({
               stroke: new ol.style.Stroke({
                   color: '#f00',
                   width: 3
               })
           }),
           'MultiLineString': new ol.style.Style({
               stroke: new ol.style.Stroke({
                   color: '#0f0',
                   width: 3
               })
           })
       };

       // 矢量图层
       var vector = new ol.layer.Vector({
           source: new ol.source.Vector({
               // GPX数据路径
               url: '../data/fells_loop.gpx',
               // 指定数据格式为GPX
               format: new ol.format.GPX()
           }),
           // 根据几何类型获取相应样式
           style: function (feature) {
               return style[feature.getGeometry().getType()];
           }
       });

       var map = new ol.Map({
           layers: [raster, vector],
           target: document.getElementById('map'),
           view: new ol.View({
               center: [-7916041.528716288, 5228379.045749711],
               zoom: 12
           })
       });


       var info = $("#info");
       info.tooltip({
           animation: false,
           trigger: "manual"
       });
       // 显示要素信息
       var displayFeatureInfo = function (pixel) {
           var features = [];
           // 根据像素来获取要素并把它添加到features数组中
           map.forEachFeatureAtPixel(pixel, function (feature) {
               features.push(feature);
           });
           // 如果有要素则显示要素信息,并设置鼠标指针样式
           if (features.length > 0) {
               var info = [];
               var i, ii;
               for (i = 0, ii = features.length; i < ii; ++i) {
                   info.push(features[i].get('desc'));
               }
               document.getElementById('info').innerHTML = info.join(', ') || '(unknown)';
               map.getTarget().style.cursor = 'pointer';
           } else {
               document.getElementById('info').innerHTML = '&nbsp; ';
               map.getTarget().style.cursor = '';
           }
       };

       // 当鼠标指针移到点要素上时显示要素信息
       map.on('pointermove', function (evt) {
           if (evt.dragging) {
               return;
           }
           var pixel = map.getEventPixel(evt.originalEvent);
           displayFeatureInfo(pixel);
       });

       // 鼠标单击时显示要素信息
       map.on('click', function (evt) {
           displayFeatureInfo(evt.pixel);
       });
   </script>
</body>

</html>

相关文章

网友评论

      本文标题:四章-48-使用GPX数据渲染要素

      本文链接:https://www.haomeiwen.com/subject/vlzkuhtx.html