OpenLayers笔记

文章展示了如何在项目中使用OL库创建地图、视图、向量层,并详细说明了单条和多条线路的表示方法。同时,文章提到了通过AMapLoader加载高德地图API,特别是Driving插件进行路径规划,并解析路径结果到OL地图上。
摘要由CSDN通过智能技术生成

项目中用到了ol,但是写法跟网上搜到的不太一样,记一下方便后面用
新建map

    <ol-map
      ref="map"
      :load-tiles-while-animating="true"
      :load-tiles-while-interacting="true"
      class="map__x"
      @click="mapClick"
      @movestart="movestart"
      @moveend="moveend"
    >

新建视图

      <ol-view
        ref="view"
        :center="view.center"
        :rotation="view.rotation"
        :zoom="view.zoom"
        :max-zoom="view.maxZoom"
        :min-zoom="view.minZoom"
        :constrain-resolution="view.constrainResolution"
      />

新建vector层(一条线)

      <ol-vector-layer>
        <ol-source-vector>
          <ol-feature ref="overlay">
            <ol-geom-line-string :coordinates="taskMap" />
            <ol-style>
              <ol-style-stroke color="#848484" :width="5" line-cap="butt"  />
            </ol-style>
          </ol-feature>
        </ol-source-vector>
      </ol-vector-layer>

其中taskMap格式[[fromLonLat([lon,lat])],[fromLonLat([lon,lat])],…]
新建vector层(多条线)

      <ol-vector-layer>
        <ol-source-vector>
          <ol-feature ref="overlay">
            <ol-geom-multi-line-string :coordinates="multipleLines"/>
            <ol-style>
              <ol-style-stroke color="#848484" :width="5" line-cap="butt"  />
            </ol-style>
          </ol-feature>
        </ol-source-vector>
      </ol-vector-layer>

其中multipleLines格式[taskMap,taskMap,…]

用到高德导航文档
index里需要引入用到的插件(这里是Driving)

import AMapLoader from '@amap/amap-jsapi-loader';
            AMapLoader.load({
              plugins: ['AMap.Driving'],
            }).then((AMap) => {
              let driving=new AMap.Driving({
              //策略,可选有很多,选的最快的
                policy: AMap.DrivingPolicy.LEAST_TIME
              })
              //siteList里是需要连起来的节点列表
              for(let i=0;i<siteList.length-1;i++){
                driving.search(siteList[i], siteList[i+1], function (status, result) {
                  // 未出错时,result即是对应的路线规划方案
                  if(status=='complete'){
                    for(const step of result.routes[0].steps){
                      //pos 格式[12955415.215279303, 4846312.905273205],就是fromLonLat处理经纬度后的格式,openlayers要的是这种格式
                      //[lng,lat]
                      //需求不用很精细,只拿了每一步起点和终点的值,也可以拆里面的step细节出来效果就会比较精准
                      that.taskMap.push(step['end_location'].pos)
                    }
                  }
                })
              }
            });

效果
在这里插入图片描述
然后被打回去重新设计UI了(

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值