格调小黄 發表於 2019-7-21 19:45:00

uni-app 之地图 map

<p style="text-align: center"><strong><span style="font-family: 宋体; font-size: 18pt">uni-app 之地图 map</span></strong></p>
<p>  怎么说呢,,,我们公司啊(忒好啊)是房地产公司,所以我现在写的是一个关于买房卖房一类的APP,用的这个uni-app ,这种app少不了的就是房产经纪人啊,置业部的店铺啊,店铺啊就不用说了,位置被~~~~是吧</p>
<p>  看了一下uni-app 的地图给大家实例一下</p>
<p>  &lt;map&gt;&nbsp; 说一说属性问题&nbsp;&nbsp;</p>
<p>    <span style="color: rgba(255, 0, 0, 1)">longitude</span>:经度&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: rgba(255, 0, 0, 1)"> &nbsp;latitude</span>:纬度&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: rgba(255, 0, 0, 1)">&nbsp;scale</span>:缩放级别,也就是说放大缩下的程度,取值范围是5-18,默认的是16 ,他就是呢值数越大,放大程度越大,看的越细,看村庄的那种</p>
<p>    <span style="color: rgba(255, 0, 0, 1)">markers</span>:标记点&nbsp; &nbsp;就是说你在地图上标记出来的东西&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: rgba(255, 0, 0, 1)">&nbsp;polyline</span>:路线&nbsp; &nbsp;可以写两个标记点&nbsp; &nbsp;然后用路线将他们连接起来&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: rgba(255, 0, 0, 1)">circle</span>:圆&nbsp; &nbsp; 就是说在地图上画个圈?</p>
<p>    <span style="color: rgba(255, 0, 0, 1)">controls</span>:控件&nbsp; 他就是说那&nbsp; 在地图上显示一个控件,但是这个控件并不随着地图移动    <span style="color: rgba(255, 0, 0, 1)">include-points</span>:缩放视野以包含所有的坐标点 这就是说你坐标点很多的时候,缩小时可以看到全部</p>
<p>    <span style="color: rgba(255, 0, 0, 1)">show-location</span>:显示带有方向的当前定位点</p>
<p>  大概粗略说一下&nbsp; 我用代码跟大家解释,这个希望大家先看一遍uni-app的地图组件,一下很浅显的我就不跟大家说了</p>
<p>  先给大家看个最基本的,根据经纬度 显示这个地图</p>
<div class="cnblogs_Highlighter">
<pre class="brush:html;gutter:true;">    &lt;map style="width:100%;height:85vh;":latitude="latitude":longitude="longitude" &gt;&lt;/map&gt;
</pre>
</div>
<p>  js部分:</p>
<div class="cnblogs_code">
<pre>export <span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> {
  data(){
</span><span style="color: rgba(0, 0, 255, 1)">    return</span><span style="color: rgba(0, 0, 0, 1)">{
         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 经纪人id</span>
         agentId:<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">,
         title: </span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">map</span><span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(0, 0, 0, 1)">, <span style="color: rgba(0, 128, 0, 1)">//地图标题</span>
         latitude: </span><span style="color: rgba(128, 0, 128, 1)">40.013305</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">纬度</span>
         longitude: <span style="color: rgba(128, 0, 128, 1)">118.685713</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">经度
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">scale:5,</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最小数,缩放范围最大,可见程度最大
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">scale:18,</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">最大数,缩放范围最小,可见程度最小</span>
<span style="color: rgba(0, 0, 0, 1)">    }
  }
}</span></pre>
</div>
<p>  看一下图是这样的&nbsp; &nbsp; 这样显示的就是经纬度对应的地图了&nbsp; 这个是默认的缩放16&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 然后给大家看一下缩放程度的照片 我上边有些的&nbsp; &nbsp;绿色的效果&nbsp; &nbsp; 首先展示的是5的&nbsp; &nbsp; &nbsp; &nbsp;其次是18的&nbsp;</p>
<p>&nbsp;这张是默认的16&nbsp; &nbsp;<img src="https://img2018.cnblogs.com/blog/1664944/201907/1664944-20190721201133606-483359895.png">这张缩放程度为5 中国的那种<img src="https://img2018.cnblogs.com/blog/1664944/201907/1664944-20190721201454952-1710406282.png">&nbsp; 这张是18 我们庄的那种<img src="https://img2018.cnblogs.com/blog/1664944/201907/1664944-20190721201526531-1566527300.png"></p>
<p>下一步: 标记点&nbsp; markers&nbsp; 顾名思义&nbsp; 就是在地图上标记了一个类似位置的标记点&nbsp; &nbsp; &nbsp;用代码解释属性</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">{</span><span style="color: rgba(0, 0, 0, 1)">
  latitude: </span><span style="color: rgba(128, 0, 128, 1)">40.013305</span><span style="color: rgba(0, 0, 0, 1)">,
  longitude: </span><span style="color: rgba(128, 0, 128, 1)">118.685713</span><span style="color: rgba(0, 0, 0, 1)">,
  marker: [{
     id:</span><span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">,
     latitude: </span><span style="color: rgba(128, 0, 128, 1)">40.013305</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">纬度</span>
     longitude: <span style="color: rgba(128, 0, 128, 1)">118.685713</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">经度</span>
     iconPath: <span style="color: rgba(128, 0, 0, 1)">''</span>,    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">显示的图标      </span>
     rotate:<span style="color: rgba(128, 0, 128, 1)">0</span>,   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 旋转度数</span>
     width:<span style="color: rgba(128, 0, 128, 1)">20</span>,   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">宽</span>
     height:<span style="color: rgba(128, 0, 128, 1)">20</span>,   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">高</span>
   title:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">你在哪了</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">标注点名</span>
   alpha:<span style="color: rgba(128, 0, 128, 1)">0.5</span>   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">透明度</span>
   label:{<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">为标记点旁边增加标签   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">因背景颜色H5不支持</span>
   content:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">唐山迁安</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">文本</span>
    color:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">red</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">文本颜色
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   fontSize:24,</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">文字大小
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    x:5,</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">label的坐标,原点是 marker 对应的经纬度
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    y:1,</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">label的坐标,原点是 marker 对应的经纬度
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    borderWidth:12,</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">边框宽度
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    borderColor:'pink',</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">边框颜色   
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   borderRadius:20,</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">边框圆角                        
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   bgColor:'black',</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">背景色
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   padding:5,</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">文本边缘留白
       </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">    textAlign:'right'</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">文本对齐方式。</span>
<span style="color: rgba(0, 0, 0, 1)">   },
   callout:{</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">自定义标记点上方的气泡窗口 点击有效</span>
     content:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">幸福花园店A组</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">文本</span>
     color:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">#ffffff</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">文字颜色</span>
     fontSize:<span style="color: rgba(128, 0, 128, 1)">14</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">文本大小</span>
     borderRadius:<span style="color: rgba(128, 0, 128, 1)">2</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">边框圆角</span>
   bgColor:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">#00c16f</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">背景颜色</span>
     display:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">ALWAYS</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">常显</span>
<span style="color: rgba(0, 0, 0, 1)">   },
   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> anchor:{</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">经纬度在标注图标的锚点,默认底边中点
   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   x:0,    原点为给出的经纬度
   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">   y:0,
   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> }</span>
<span style="color: rgba(0, 0, 0, 1)">               
   }],
   scale:</span><span style="color: rgba(128, 0, 128, 1)">16</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">地图缩放程度</span>
}      <span style="color: rgba(153, 51, 102, 1)">(我将每一项都给大家标记出来注释,平台的差异我就不解释了,大家可以去官网看一下)</span></pre>
</div>
<p>  </p>
<p>  先看一张比较普通的,就是只有 标记点的那种&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;再看一下就是有各种标记的那种&nbsp; &nbsp;</p>
<p><img src="https://img2018.cnblogs.com/blog/1664944/201907/1664944-20190722100220554-1501971554.png">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<img src="https://img2018.cnblogs.com/blog/1664944/201907/1664944-20190722100933205-1402753282.png"></p>
<p>接下来把剩下的都给大家展示一下&nbsp; &nbsp; 路线 圆 还有控件&nbsp; &nbsp;这几个是一体的所以一并显示                    </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">这是接着上面data里面写的   重复太多我就不在写了 补充了没有的
  controls:[{</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在地图上显示控件,控件不随着地图移动</span>
     id:<span style="color: rgba(128, 0, 128, 1)">1</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">控件id</span>
     iconPath:<span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">../../static/images/myself.jpg</span><span style="color: rgba(128, 0, 0, 1)">'</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">显示的图标    </span>
     position:{<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">控件在地图的位置</span>
        left:<span style="color: rgba(128, 0, 128, 1)">15</span><span style="color: rgba(0, 0, 0, 1)">,
        top:</span><span style="color: rgba(128, 0, 128, 1)">15</span><span style="color: rgba(0, 0, 0, 1)">,
        width:</span><span style="color: rgba(128, 0, 128, 1)">50</span><span style="color: rgba(0, 0, 0, 1)">,
        height:</span><span style="color: rgba(128, 0, 128, 1)">50</span><span style="color: rgba(0, 0, 0, 1)">
  },   
  }],
  circles:[{</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">在地图上显示圆</span>
     latitude: <span style="color: rgba(128, 0, 128, 1)">40.013</span><span style="color: rgba(0, 0, 0, 1)">,
     longitude: </span><span style="color: rgba(128, 0, 128, 1)">118.685</span><span style="color: rgba(0, 0, 0, 1)">,
     fillColor:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">#999999</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">填充颜色</span>
   color:<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">#0016ca</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">描边的颜色</span>
     radius:<span style="color: rgba(128, 0, 128, 1)">20</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">半径</span>
   strokeWidth:<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">描边的宽度</span>
<span style="color: rgba(0, 0, 0, 1)">   }],
  polyline:[{</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">指定一系列坐标点,从数组第一项连线至最后一项</span>
<span style="color: rgba(0, 0, 0, 1)">    points:[
         {latitude: </span><span style="color: rgba(128, 0, 128, 1)">40.013305</span>,longitude: <span style="color: rgba(128, 0, 128, 1)">118.685713</span><span style="color: rgba(0, 0, 0, 1)">},
         {latitude: </span><span style="color: rgba(128, 0, 128, 1)">40.013</span>,longitude: <span style="color: rgba(128, 0, 128, 1)">118.685</span><span style="color: rgba(0, 0, 0, 1)">},
    ],
    color:</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">#0000AA</span><span style="color: rgba(128, 0, 0, 1)">"</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">线的颜色</span>
    width:<span style="color: rgba(128, 0, 128, 1)">2</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">线的宽度</span>
    dottedLine:<span style="color: rgba(0, 0, 255, 1)">true</span>,<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">是否虚线</span>
    arrowLine:<span style="color: rgba(0, 0, 255, 1)">true</span>,    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">带箭头的线 开发者工具暂不支持该属性</span>
  }],</pre>
</div>
<p>&nbsp;</p>
<p>  效果图如下:</p>
<p><img src="https://img2018.cnblogs.com/blog/1664944/201907/1664944-20190722105411591-1484360857.png">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;后来我引用了一个比较好理解的控件<img src="https://img2018.cnblogs.com/blog/1664944/201907/1664944-20190729084057453-2112040713.png"></p>
<p>注意:我们公司设计的当时说想做成那种地图上面带有div的&nbsp; &nbsp;就是说地图上面可以显示门店位置。门店名称的那种,我当时用定位在网页上实现的,但是一到手机上运行的时候,发现显示不出来,后来我仔细看了一下,那个map组件享有最高的优先级,</p>
<p>你用定位之类,根本就实现不了,后来我发现了一个叫&lt;cover-image&gt; 和 &lt;cover-view &gt;&nbsp; 他是可以覆盖在原生组件上的文本视图&nbsp; &nbsp;但是也有注意点它不支持的css:</p>
<ul>
<li>position: fixed</li>
<li>opacity</li>
<li>overflow</li>
<li>padding</li>
<li>linebreak</li>
<li>white-space</li>
</ul>
<p>另外还有:&nbsp; 在APP端它不支持嵌套其他组件,也不支持本身组件的嵌套,app现在&lt;cover-view&gt;只可以不该原生组件video和map, 一定要注意</p>
<p>目前呢 ~~~~总结这么多,以后要是有什么心得的话我会继续补充,也欢迎大家帮我补充哈~~~谢谢大家~~~</p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/gongliying/p/11222421.html
頁: [1]
查看完整版本: uni-app 之地图 map