微信小程序--map

官方api:https://mp.weixin.qq.com/debug/wxadoc/dev/component/map.html

很多时候,都会用到地图,不仅方便,更直观,用户体验更好,
这里我们就来介绍一下地图的简单使用;
效果图如下,

这里写图片描述

1.这里使用了https://github.com/Jacky-MYD/WeChat的自定义的toast,大家可以下载研究下,
1.app.js中:

//app.js
import { ToastPannel } from './component/toastTest/toastTest'
App({
  ToastPannel,
  onLaunch: function () {

  },
  globalData: {
    userInfo: null
  }
})

2.app.json中:

{
  "pages": [
    "pages/index/index",
    "pages/logs/logs",
    "pages/toast/toast"
  ],
  "window": {
    "backgroundTextStyle": "light",
    "navigationBarBackgroundColor": "#fff",
    "navigationBarTitleText": "demo",
    "navigationBarTextStyle": "black"
  }
}

3.app.wxss中:

 /* 引用toast的样式 */
@import "./component/toastTest/toastTest.wxss"; 

.container {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 200rpx 0;
  box-sizing: border-box;
} 

4.index.js中:

//index.js
//获取应用实例
const app = getApp()
var isFirst = true;
var isFirstCircles = true;

Page({
  data: {
    scale: 13,
    //设置是否显示带
    location: true,
    //设置圆
    circles: [],
    markers: [{
      iconPath: "../images/location.png",
      id: 0,
      latitude: 38.859464,
      longitude: 121.527323,
      width: 25,
      height: 25,

    }],
  },
  regionchange(e) {
    console.log(e.type)
  },
  markertap(e) {
    console.log(e.markerId)
  },
  controltap(e) {
    console.log(e.controlId)
  },

  onLoad:function(){
    let app = getApp()
    // toast组件实例

    new app.ToastPannel();
  },


  //显示线路
  showLine: function (e) {
    var that = this;
    if (isFirst == true) {
      isFirst = false;
      that.setData({
        polyline: [{
          points: [{
            longitude: 121.527323,
            latitude: 38.859464
          }, {
            longitude: 121.550674,
            latitude: 38.877136,
          }],
          color: "#FF0000DD",
          width: 2,
          dottedLine: true
        }],
      })
    } else {
      isFirst = true;
      that.setData({
        polyline: [{
          points: [{

          }, {

          }],
          color: "#FF0000DD",
          width: 2,
          dottedLine: true
        }],
      })
    }
  },
  //显示圆
  showCircles: function (res) {
    var that = this;
    if (isFirstCircles == true) {
      isFirstCircles = false;
      //请求接口
      wx.getLocation({
        type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
        success: function (res) {
          that.setData({
            latitude: res.latitude,
            longitude: res.longitude,
            markers: [{
              id: "1",
              latitude: res.latitude,
              longitude: res.longitude,
              width: 50,
              height: 50,

              //marker 上的气泡 callout
              callout: {
                content: '自定义标记点上方的气泡窗口',
                color: '#000000',
                fontSize: 8,
                borderRadius: 2,
                bgColor: '#ff00ff',
                padding: 4,
                textAlign: 'center',
              },
              //marker 上的气泡 label
              label: {
                content: '显示在标记点旁边的标签',
                color: '#000000',
                fontSize: 8,
                borderRadius: 2,
                bgColor: '#00ff00',
                padding: 4,
                textAlign: 'center',
              },

            }],
            //  圆
            circles: [{
              latitude: res.latitude,
              longitude: res.longitude,
              color: '#FF0000DD',
              fillColor: '#7cb5ec88',
              radius: 3000,
              strokeWidth: 1
            }]
          })
        }
      })
    } else {
      isFirstCircles = true;
      that.setData({
        latitude: res.latitude,
        longitude: res.longitude,
        markers: [{

        }],
        circles: [{
          latitude: res.latitude,
          longitude: res.longitude,
          color: '#ffffff',
          fillColor: '#7cb5ec88',
          radius: 3000,
          strokeWidth: 1
        }]
      })
    }
  },
  //放大
  add: function (e) {
    var that = this;
    console.log("scale===" + this.data.scale)
    if (this.data.scale > 5) {
      that.setData({
        scale: --this.data.scale
      })
    } else {
      that.show("最大放大级别为5");
    }
  },
  //缩小
  reduce: function (e) {
    var that = this;
    console.log("scale===" + this.data.scale)
    if (this.data.scale < 18) {
      that.setData({
        scale: ++this.data.scale
      })
    } else {
      that.show("最大放大级别为18");
    }
  },

})

5.index.wxml中:

<!-- map.wxml -->
<!-- 自定义Toast -->
<import src="../../component/toastTest/toastTest.wxml"/>
<template is="toast" data="{{ ..._toast_ }}"/>

<map id="map" longitude="121.527323" latitude="38.859464" scale="{{scale}}" controls="{{controls}}" bindcontroltap="controltap" markers="{{markers}}" bindmarkertap="markertap" polyline="{{polyline}}" bindregionchange="regionchange" show-location="{{location}}"
  circles="{{circles}}" style="width: 100%; height: 300px;"></map>
<view>
  <view>
    <button class='button' bindtap="add">放大</button>
    <button class='button' bindtap="reduce">缩小</button>
  </view>
  <button bindtap="showLine">显示/隐藏路线</button>
  <button bindtap="showCircles">显示/隐藏圆</button>
</view>

6.index.wxss

.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.userinfo-avatar {
  width: 128rpx;
  height: 128rpx;
  margin: 20rpx;
  border-radius: 50%;
}

.userinfo-nickname {
  color: #aaa;
}

.usermotto {
  margin-top: 200px;
}

.button{
  width: 50vw;
  float: left;
}

7.demo的下载地址:

http://download.csdn.net/download/afanbaby/10154232

本人菜鸟一个,有什么不对的地方希望大家指出评论,大神勿喷,希望大家一起学习进步。

微信小程序 map-master 是基于微信开发平台的一款地图应用。它提供了丰富的地图展示和定位功能,方便用户查询地理位置信息,规划路线等。 在 map-master 中,用户可以通过地图展示页面查看各种地理标记,包括地点、商铺、景点等。用户可以通过缩放、拖动地图来获得更详细的信息,双击地图可以放大到更具细节的级别。地图上的标记点提供了关于该地点的详细信息,包括名称、地址、评分等。用户可以点击标记点来查看更详细的信息,也可以通过搜索功能来查找特定的地点。 在定位功能方面,map-master 使用了微信提供的位置服务接口,可以根据用户的实际位置显示当前定位,并提供导航功能。用户可以使用导航功能输入起点和终点,map-master 会根据用户选择的交通方式,提供最佳路线规划和导航指引,方便用户出行和导航。 除了基本的地图展示和定位功能外,map-master 还提供了一些实用的附加功能,比如周边搜索、实时交通状况等。用户可以通过周边搜索功能查找特定类型的地点,比如餐馆、银行、酒店等,从而更方便地满足自己的需求。实时交通状况功能可以帮助用户选择避免拥堵的路线,节省时间和精力。 总之,微信小程序 map-master 是一款功能强大且实用的地图应用程序,提供了丰富的地图展示和定位功能,为用户的出行和导航提供了很大的便利。用户可以随时随地通过微信小程序打开 map-master,获取周边信息、规划导航路线等,方便快捷。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值