Taro.Map地图使用

import React, { useState, useEffect, useRef } from 'react';
import './index.scss';
import { View, Map } from '@tarojs/components';
import { Button } from '@nutui/nutui-react-taro';
import Taro from '@tarojs/taro';
const Index = () => {
  const mapRef = useRef(null);
  // 标记点
  const markers = [
    {
      id: 0,
      latitude: 30.572269,
      longitude: 104.066541,
      title: '成都',
      width: 20,
      height: 20
    }]
  // 路线
  const polyline = [
    {
      points: [
        { latitude: 30.572269, longitude: 104.066541 },
        { latitude: 30.572379, longitude: 104.066745 },
        { latitude: 30.572489, longitude: 104.067242 }
      ],
      color: '#FF0000',
      width: 5,
      dottedLine: false
    }
  ];
  // 显示用户当前位置---失败
  // 在app.config.js文件和pages同级配置permission
  // permission: {
  //   'scope.userLocation': {
  //     desc: '你的位置信息将用于小程序位置接口的效果展示'
  //   }
  // }
  // 在 handleShowLocation 函数中请求用户位置信息,并传递给 Map 组件
  const handleShowLocation = async () => {
    try {
      const res = await Taro.getLocation();
      if (mapRef.current) {
        mapRef.current.moveToLocation({
          longitude: res.longitude,
          latitude: res.latitude
        });
      }
    } catch (error) {
      console.error('获取用户位置失败:', error);
    }
  }
  // 可以在这里处理获取到的经纬度信息,比如显示在页面上或发送给后端
  const handleTap = (e) => {
    console.log('用户点击地图坐标:', e.detail.longitude, e.detail.latitude);
  };
  return (
    <View className='map'>
      <Map
        ref={mapRef}                                // 显示用户当前位置
        longitude={104.066541}                      //中心经度
        latitude={30.572269}                        //中心纬度
        scale={16}                                  //缩放
        markers={markers}                           //标记点
        polyline={polyline}                         //路线
        onTap={handleTap}                           //用户选择位置
        style={{ width: '100%', height: '300px' }}
      />
      <Button onClick={handleShowLocation}>显示当前位置</Button>
    </View>
  );
};

export default Index;
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Taro 公众号中使用高德地图,你可以通过以下步骤进行: 1.进入高德开放平台,注册并创建自己的应用。 2.获取高德地图 JS API 的 key,将其添加到 Taro 项目的配置文件中。 3.在 Taro 页面中引入高德地图的 JS API 文件,并在页面中添加地图容器。 4.使用高德地图提供的 API 实现地图的各种功能,如标记、搜索、路线规划等。 以下是一个简单的例子,演示如何在 Taro 公众号中使用高德地图: ```jsx import Taro, { Component } from '@tarojs/taro' import { View } from '@tarojs/components' class MapPage extends Component { config = { navigationBarTitleText: '地图' } componentDidMount() { // 引入高德地图 JS API 文件 const script = document.createElement('script') script.type = 'text/javascript' script.async = true script.src = `https://webapi.amap.com/maps?v=1.4.15&key=YOUR_API_KEY` script.onerror = () => { reject(new Error(`加载高德地图API失败`)) } document.head.appendChild(script) // 创建地图容器 const mapContainer = document.createElement('div') mapContainer.style.width = '100%' mapContainer.style.height = '300px' document.body.appendChild(mapContainer) // 初始化地图 window.AMap && window.AMap.plugin('AMap.Map', function() { const map = new window.AMap.Map(mapContainer, { center: [116.397428, 39.90923], zoom: 13 }) }) } render() { return ( <View> <View>这是一个地图页面</View> </View> ) } } export default MapPage ``` 注意,上述代码中的 `YOUR_API_KEY` 需要替换成你自己的高德地图 JS API 的 key。另外,为了避免在 Taro使用 `window` 对象时出现问题,可以在 `componentDidMount` 生命周期中使用 `window.AMap` 来调用高德地图 API。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值