微信小程序——高德地图展示点、线、圆、多边形

在这里插入图片描述
在这里插入图片描述

  • 点、线、面、圆形的显示
  • 获取周边、天气等

在这里插入图片描述

<view class="map_box">
  <view bindtap="change">点击</view>
  <map class="w100 min100" id="navi_map" longitude="116.451028" latitude="39.949643" scale="12" markers="{{markers}}" polyline="{{polyline}}" circles="{{circles}}" polygons="{{polygons}}" ></map>
</view>
// miniprogram/pages/gao_de/gao_de.js
var amapFile = require('../../libs/amap-wx.js');//如:..­/..­/libs/amap-wx.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    markers: [{ //点
      iconPath: "../../assets/imgs/wx.png",
      latitude: 39.989643,
      longitude: 116.481028,
      width: 20,
      height: 20
    },{
      iconPath: "../../assets/imgs/wx.png",
      latitude: 39.90816,
      longitude: 116.434446,
      width: 20,
      height: 20
    }], 
    polyline: [], // 线
    circles: [{   // 圆(圆心和半径)
      latitude: 39.949643,
      longitude: 116.451028,
      fillColor: "#FFA07A33", // 圆形填充颜色
      strokeColor: "#FFA07A",
      strokeWidth: 2,
      radius: 2000
    }],
    polygons:[{ // 区域(拐点坐标集合)
      points: [
        { latitude: 39.989643, longitude: 116.481028 },
        { latitude: 39.90816, longitude: 116.434446},
        { latitude: 39.889643, longitude: 116.451028 },
        { latitude: 39.919643, longitude: 116.491028 },
      ],
      fillColor: '#FFA07A33',
      strokeColor: '#FFA07A',
      strokeWidth: 2
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var myAmapFun = new amapFile.AMapWX({key:'a0ee84c178d16e6045790c612eb51ab9'});
    // 获取周边
    myAmapFun.getPoiAround({
      success: function(data){
        console.log(data)
        //成功回调
      },
      fail: function(info){
        //失败回调
        console.log(info)
      }
    })
    //获取自己所在地址的定位
    myAmapFun.getRegeo({
      success: function(data){
        //成功回调
        console.log(data)
      },
      fail: function(info){
        //失败回调
        console.log(info)
      }
    })

    //获取定位地点天气内容
    myAmapFun.getWeather({
      success: function(data){
        console.log(data,'123')
        //成功回调
      },
      fail: function(info){
        //失败回调
        console.log(info)
      }
    })

    //获取两点之间行车路线
    myAmapFun.getDrivingRoute({
      origin: '116.481028,39.989643',
      destination: '116.434446,39.90816',
      success: function(data){
        var points = [];
        if(data.paths && data.paths[0] && data.paths[0].steps){
          var steps = data.paths[0].steps;
          for(var i = 0; i < steps.length; i++){
            var poLen = steps[i].polyline.split(';');
            for(var j = 0;j < poLen.length; j++){
              points.push({
                longitude: parseFloat(poLen[j].split(',')[0]),
                latitude: parseFloat(poLen[j].split(',')[1])
              })
            } 
          }
        }
        that.setData({
          polyline: [{
            points: points,
            color: "#0091ff",
            width: 6
          }]
        });
        if(data.paths[0] && data.paths[0].distance){
          that.setData({
            distance: data.paths[0].distance + '米'
          });
        }
        if(data.taxi_cost){
          that.setData({
            cost: '打车约' + parseInt(data.taxi_cost) + '元'
          });
        }
          
      },
      fail: function(info){

      }
    })

  }, 
  change(){
    let o = this.data.polygons[0]
    o.points = [ { latitude: 39.989643, longitude: 116.481028 },
      { latitude: 39.90816, longitude: 116.434446},
      { latitude: 39.889643, longitude: 116.451028 },]
    this.setData({
      ['polygons[0]']: o,
    })
  }
})

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值