微信小程序位置

以下是一个微信小程序中使用 wx.getLocation、wx.openLocation 和 wx.chooseLocation 的案例,包含 wxml、js 和 css 文件:

wxml 文件:

html
Copy code
<view class="container">
  <view class="header">
    <button bindtap="getLocation">获取位置</button>
    <button bindtap="openLocation">打开地图</button>
    <button bindtap="chooseLocation">选择位置</button>
  </view>
  <view class="map-container">
    <map id="map" latitude="{{latitude}}" longitude="{{longitude}}" markers="{{markers}}" bindmarkertap="markertap"></map>
  </view>
  <view class="location-info">
    <view class="item">
      <text>当前位置:</text>
      <text>{{locationInfo}}</text>
    </view>
    <view class="item">
      <text>选择位置:</text>
      <text>{{selectedLocation.name}}</text>
    </view>
  </view>
</view>
js 文件:

js
Copy code
Page({
  data: {
    latitude: null, // 当前位置的纬度
    longitude: null, // 当前位置的经度
    markers: [], // 地图标记
    selectedLocation: {}, // 选择的位置信息
    locationInfo: '暂无'
  },

  // 点击获取位置按钮触发事件
  getLocation: function () {
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude,
          markers: [{
            id: 1,
            latitude: res.latitude,
            longitude: res.longitude,
            iconPath: '/images/location.png',
            width: 30,
            height: 30
          }],
          locationInfo: res.address || '暂无'
        });
        wx.showToast({
          title: '获取位置成功'
        });
      },
      fail: (err) => {
        wx.showToast({
          icon: 'none',
          title: '获取位置失败'
        });
      }
    });
  },

  // 点击打开地图按钮触发事件
  openLocation: function () {
    if (this.data.latitude && this.data.longitude) {
      wx.openLocation({
        latitude: this.data.latitude,
        longitude: this.data.longitude,
        name: '当前位置',
        address: this.data.locationInfo || ''
      });
    } else {
      wx.showToast({
        icon: 'none',
        title: '当前位置信息为空'
      });
    }
  },

  // 点击选择位置按钮触发事件
  chooseLocation: function () {
    wx.chooseLocation({
      success: (res) => {
        this.setData({
          selectedLocation: {
            latitude: res.latitude,
            longitude: res.longitude,
            name: res.name,
            address: res.address
          },
          markers: [{
            id: 1,
            latitude: res.latitude,
            longitude: res.longitude,
            iconPath: '/images/location.png',
            width: 30,
            height: 30
          }]
        });
        wx.showToast({
          title: '选择位置成功'
        });
      },
      fail: (err) => {
        wx.showToast({
          icon: 'none',
          title: '选择位置失败'
        });
      }
    });
  },

  // 点击地图标记触发事件
  markertap: function (event) {
    const markerId = event.detail.markerId;
    if (





继续

ChatGPT
kotlin
Copy code
markerId === 1) {
  this.openLocation();
}
}
});

css
Copy code

**css 文件:**

```css
.container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.header {
  display: flex;
  justify-content: space-around;
  width: 100%;
}

.map-container {
  margin: 20px 0;
  width: 80%;
  height: 400px;
}

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

.item {
  margin: 10px 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值