Taro地图组件和小程序定位

在 Taro 中使用腾讯地图

1.首先在项目配置文件 project.config.json 中添加权限:

js 复制代码
{
  "permission": {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  }
}

2.在 app.config.ts 中配置:

ts 复制代码
export default {
  // ...其他配置
  requiredPrivateInfos: [
    "getLocation",
    "chooseLocation"
  ],
  permission: {
    "scope.userLocation": {
      "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
  }
}

3.使用例子

ts 复制代码
import Taro from '@tarojs/taro'

// 基础使用示例
const MapExample: React.FC = () => {
 // 获取当前位置
 const getLocation = async () => {
   try {
     const res = await Taro.getLocation({
       type: 'gcj02' //返回可以用于wx.openLocation的经纬度
     })
     console.log('当前位置:', res.latitude, res.longitude);
     return res;
   } catch (err) {
     console.error('获取位置失败:', err);
   }
 }

 // 打开地图选择位置
 const chooseLocation = async () => {
   try {
     const res = await Taro.chooseLocation({
       latitude: 39.9,  // 默认纬度
       longitude: 116.4 // 默认经度
     })
     console.log('选择的位置:', res);
   } catch (err) {
     console.error('选择位置失败:', err);
   }
 }

 return (
   <View>
     <Map
       id="map"
       longitude={116.4}
       latitude={39.9}
       scale={16}
       markers={[{
         id: 1,
         latitude: 39.9,
         longitude: 116.4,
         name: '位置'
       }]}
       onTap={() => {console.log('map tap')}}
       style={{ width: '100%', height: '300px' }}
     />
     <Button onClick={getLocation}>获取当前位置</Button>
     <Button onClick={chooseLocation}>选择位置</Button>
   </View>
 )
}

4.常用的地图属性

ts 复制代码
<Map
  id="map"
  longitude={116.4}
  latitude={39.9}
  scale={16}
  markers={markers}
  polyline={polyline} // 路线
  circles={circles}   // 圆形
  showLocation       // 显示当前位置
  enableZoom        // 允许缩放
  enableScroll      // 允许拖动
  enableRotate      // 允许旋转
  style={{ width: '100%', height: '300px' }}
  onTap={handleMapTap}
  onMarkerTap={handleMarkerTap}
  onRegionChange={handleRegionChange}
/>

marks示例

ts 复制代码
[{id: 1,
    latitude: 28.236142,
    longitude: 112.983482,
    iconPath: require('.././../assets/location.png'), // 标记点图标
    width: 30,
    height: 30,
    callout: {
      content: '开福区总店',
      color: '#000',
      fontSize: 12,
      borderRadius: 5,
      bgColor: '#fff',
      padding: 5,
      display: 'ALWAYS',
      borderWidth: 1, // 模拟边框宽度
      borderColor: '#000', // 模拟边框颜色
    },
    label: {
      content: '标记点1',
      color: '#000',
      fontSize: 12,
      anchorX: 0,
      anchorY: -40 // 调整垂直偏移,使其看起来像在标记点上方
    }
  }}]

小程序定位相关api

ts 复制代码
//获取当前位置
const res = await Taro.getLocation({
        type: 'gcj02' //返回可以用于wx.openLocation的经纬度
      })
//去选择位置
 const res = await Taro.chooseLocation({
        latitude: 28.236142,  // 默认纬度
        longitude: 112.983482 // 默认经度
      })
 //定位去某个位置
 Taro.openLocation({
    latitude,
    longitude,
    name, // 位置名称
    address, // 位置地址
    scale: 18 // 地图缩放级别
  });
相关推荐
kyriewen6 小时前
Anthropic 估值逼近万亿美元,Claude Sonnet 5 + Claude Science 一天两连发
前端·ai编程·claude
小徐_23337 小时前
Wot UI 2.2.0 发布:Button 新增 subtle,VideoPreview 预览体验继续增强
前端·微信小程序·uni-app
天蓝色的鱼鱼9 小时前
关于 CSS 你可能不知道的属性,但关键时刻很有用
前端·css
泯泷10 小时前
第 2 篇:设计第一套字节码:Opcode、Instruction 与 Constant Pool
前端·javascript·安全
妙码生花10 小时前
从 PHP 到 AI + Golang,程序员自救转型手记(十五):优化细节、网络请求封装
前端·后端·ai编程
泯泷10 小时前
第 1 篇:从 1 + 2 开始:亲手写出第一台 JSVM
前端·javascript·安全
团团崽_七分甜10 小时前
Spring Boot 核心知识点总结
前端
lichenyang45311 小时前
从一个按钮开始,理解 ASCF 框架到底在做什么
前端
古夕11 小时前
第三方 SSO 接入实践:redirect_uri 编码、回调一致性与跨项目联调
前端·vue.js