章誉馨 發表於 2019-5-7 20:37:00

vue开发微信公众号--地图

<p>在最近开发的微信公众号中,要实现一个打卡功能:</p>
<p><img src="https://img2018.cnblogs.com/blog/1304208/201905/1304208-20190507144021537-1769126251.png"></p>
<p>由于个人感觉微信SDK里面的地图不太好用,所以使用了腾讯地图。</p>
<h2>在项目中引入腾讯地图</h2>
<p>1,需要登录腾讯地图网站,注册一个账户,获得一个key。</p>
<p>2,然后找到项目根目录下面的index.html,引入需要使用到的js</p>
<div class="cnblogs_code">
<pre>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;meta name="viewport" content="width=device-width,initial-scale=1.0"&gt;
   <span style="background-color: rgba(255, 0, 0, 1)"> &lt;script type="text/javascript" src="https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js"&gt;&lt;/script&gt;</span>
&lt;/head&gt;
&lt;body&gt;
    &lt;div id="app"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
</div>
<p>3,项目根目录--&gt; src --&gt;service文件夹下新建parking.js(文件名称和路径可以自定义,只需要在使用时能找到就可以了)</p>
<p>parking.js(文件里的key填自己申请的就可以了)</p>
<div class="cnblogs_code">
<pre>export <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> TMap() {
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">new</span> Promise(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(resolve, reject) {
    window.init </span>= <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {
      resolve(qq)
    }
    </span><span style="color: rgba(0, 0, 255, 1)">var</span> script = document.createElement('script'<span style="color: rgba(0, 0, 0, 1)">)
    script.type </span>= 'text/javascript'<span style="color: rgba(0, 0, 0, 1)">
    script.src </span>= 'https://map.qq.com/api/js?v=2.exp&amp;callback=init&amp;key=S<span style="background-color: rgba(0, 0, 0, 1)">I5BZ-RTZRQ-2YD52-GAIRP-Z2CBK-7SFI</span>C'<span style="color: rgba(0, 0, 0, 1)">
    script.onerror </span>=<span style="color: rgba(0, 0, 0, 1)"> reject
    document.head.appendChild(script)
})
}</span></pre>
</div>
<h2>使用地图</h2>
<p>1,引入parking.js</p>
<p><img src="https://img2018.cnblogs.com/blog/1304208/201905/1304208-20190507174211252-627791824.png"></p>
<p>2,展示地图的容器</p>
<p><img src="https://img2018.cnblogs.com/blog/1304208/201905/1304208-20190507174301349-1753184777.png"></p>
<p>3,获取当前位置,并展示以当前位置为中心的地图(注释位置依旧填写自己注册的key)</p>
<p>先贴代码</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">getMyLocation() {
            </span><span style="color: rgba(0, 0, 255, 1)">var</span> geolocation = <span style="color: rgba(0, 0, 255, 1)">new</span> qq.maps.Geolocation("S<span style="background-color: rgba(0, 0, 0, 1)">I5BZ-RTZRQ-2YD52-GAIRP-Z2CBK-7SFI</span>C", "打卡"<span style="color: rgba(0, 0, 0, 1)">);</span>
            geolocation.getIpLocation(<span style="color: rgba(0, 0, 255, 1)">this</span>.showPosition, <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.showErr);
      },
      showPosition(position) {
          console.warn(position);
          </span><span style="color: rgba(0, 0, 255, 1)">this</span>.latitude =<span style="color: rgba(0, 0, 0, 1)"> position.lat;
          </span><span style="color: rgba(0, 0, 255, 1)">this</span>.longitude =<span style="color: rgba(0, 0, 0, 1)"> position.lng;
          </span><span style="color: rgba(0, 0, 255, 1)">this</span>.city =<span style="color: rgba(0, 0, 0, 1)"> position.city;
          </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.mapTX();
      },
      showErr() {
            Toast({
                message: </span>'定位失败,请稍后重试!'<span style="color: rgba(0, 0, 0, 1)">,
                position:</span>'center'<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)"> this.$router.back(-1);</span>
            <span style="color: rgba(0, 0, 255, 1)">this</span>.getMyLocation();<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)">      },
      mapTX() {
            let that </span>= <span style="color: rgba(0, 0, 255, 1)">this</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)"> 根据地理位置坐标,展示地图</span>
            TMap().then(qq =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
                </span><span style="color: rgba(0, 0, 255, 1)">var</span> map = <span style="color: rgba(0, 0, 255, 1)">new</span> qq.maps.Map(document.getElementById('container'<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)">这里经纬度代表进入地图显示的中心区域</span>
                  center: <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> qq.maps.LatLng(that.latitude,that.longitude),
                  zoom: </span>15<span style="color: rgba(0, 0, 0, 1)">
                });
                </span><span style="color: rgba(0, 0, 255, 1)">var</span> marker = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> qq.maps.Marker({
                  map : map,
                  position : </span><span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> qq.maps.LatLng(that.latitude,that.longitude),
                });

                </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, 255, 1)">var</span> getAdd = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> qq.maps.Geocoder({
                        complete : </span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(result){
                            console.log(result);
                            that.address </span>=<span style="color: rgba(0, 0, 0, 1)"> result.detail.address;
                        }
                  });
                </span><span style="color: rgba(0, 0, 255, 1)">var</span> latLng = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> qq.maps.LatLng(that.latitude, that.longitude);
                getAdd.getAddress(latLng);

                </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">绑定单击事件添加参数</span>
                qq.maps.event.addListener(map, 'click', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(event) {</span><span style="color: rgba(0, 0, 0, 1)">})
            })
      },</span></pre>
</div>
<p><img src="https://img2018.cnblogs.com/blog/1304208/201905/1304208-20190507180223358-1978866661.png"></p>
<p>在showPosition(position)这个方法里面就会有位置信息,包括经纬度和地址。</p>
<p><img src="https://img2018.cnblogs.com/blog/1304208/201905/1304208-20190507174819400-849168083.png"></p>
<p>然后根据这里的经纬度就可以调用mapTX()来绘制地图了,但是因为我们需要展示当前定位的位置,而上面打印的并不精确,所以还需要根据获取到当前经纬度对应的详细地址:</p>
<p><img src="https://img2018.cnblogs.com/blog/1304208/201905/1304208-20190507175054432-112470367.png"></p>
<p>只要获取到了经纬度和详细地址,我们就可以进行打卡了。</p>
<h2>点击事件</h2>
<p>&nbsp;在地图中是可以添加点击事件的,但是由于这里不需要,所以上面没有写。关于点击事件,主要就是获取点击位置的经纬度就可以继续后续的操作。</p>
<p><img src="https://img2018.cnblogs.com/blog/1304208/201905/1304208-20190507175531895-1469248594.png"></p>
<p>打印上面代码中点击事件的event</p>
<p><img src="https://img2018.cnblogs.com/blog/1304208/201905/1304208-20190507175647008-419491835.png"></p>
<p>也就是,我们是可以通过event.latLng.getLat()和event.latLng.getLng()获取到经纬度的,如果还需要获取点击的位置,再调用上面getAdd.getAddress(latLng)方法,传入点击获取的经纬度就可以了。</p>
<p>&nbsp;<img src="https://img2018.cnblogs.com/blog/1304208/201905/1304208-20190507180121686-327441375.png"></p><br><br>
来源:https://www.cnblogs.com/yuyujuan/p/10825482.html
頁: [1]
查看完整版本: vue开发微信公众号--地图