实现地图功能 利用微信内置的微信地图

本文详细介绍如何在微信小程序中实现地图位置选择功能,通过调用wx.chooseLocation接口,轻松获取并显示用户选择的位置信息,包括经纬度和详细地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:


查看微信开发文档:https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.chooseLocation.html

其实不难,查看官方文档,一目了然

直接上代码:

map.wxml:


 <button bindtap="mapViewTap" style="margin:10px">查看地图</button>
 <button bindtap="chooseMapViewTap" style="margin:10px">选择位置</button>

 <view>
 <view>纬度:{{location.latitude}}</view>
 <view>精度:{{location.longitude}}</view>
 <view>地址:{{location.address}}{{location.name}}</view>
 </view>

map.js:

Page({

  /**
   * 页面的初始数据
   */
  data: {
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
  //利用getLocation获取当前位置的经纬度坐标
  mapViewTap: function () {
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        console.log(res)
        wx.openLocation({
          latitude: res.latitude,
          longitude: res.longitude,
          scale: 28
        })
      }
    })
  },
 //wx.chooseLocation(OBJECT) 打开地图选择位置
  chooseMapViewTap:function(e){
    var that=this;
    wx.chooseLocation({
      success: function(res) {
        console.log(res);
        that.setData({
          location:{
            latitude:res.latitude,
            longitude: res.longitude,
            name:res.name,
            address:res.address
          }
        })
      },
    })

  },
})

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值