63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import Taro from '@tarojs/taro';
|
|
import { Button } from '@tarojs/components';
|
|
import { mapService, SearchResult, LocationInfo } from './mapService'
|
|
import { useEffect, useState } from 'react';
|
|
|
|
export default function MapPlugin() {
|
|
const key = 'AZNBZ-VCSC4-MLVUF-KBASD-6GZ6H-KBFTX'; //使用在腾讯位置服务申请的key
|
|
const referer = '八瓜一月'; //调用插件的app的名称
|
|
const [currentLocation, setCurrentLocation] = useState<LocationInfo | null>(null)
|
|
|
|
const category = '';
|
|
|
|
const chooseLocation = () => {
|
|
Taro.navigateTo({
|
|
url: 'plugin://chooseLocation/index?key=' + key + '&referer=' + referer + '&latitude=' + currentLocation?.lat + '&longitude=' + currentLocation?.lng
|
|
});
|
|
}
|
|
useEffect(() => {
|
|
initializeMapService()
|
|
}, [])
|
|
|
|
// 初始化地图服务
|
|
const initializeMapService = async () => {
|
|
try {
|
|
const success = await mapService.initSDK()
|
|
if (success) {
|
|
console.log('地图服务初始化成功')
|
|
getCurrentLocation()
|
|
} else {
|
|
console.error('地图服务初始化失败')
|
|
Taro.showToast({
|
|
title: '地图服务初始化失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
} catch (error) {
|
|
console.error('初始化地图服务异常:', error)
|
|
Taro.showToast({
|
|
title: '地图服务初始化异常',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
// 获取当前位置
|
|
const getCurrentLocation = async () => {
|
|
try {
|
|
const location = await mapService.getLocation()
|
|
if (location) {
|
|
setCurrentLocation(location)
|
|
console.log('当前位置:', location)
|
|
}
|
|
} catch (error) {
|
|
console.error('获取位置失败:', error)
|
|
Taro.showToast({
|
|
title: '获取位置失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
return (
|
|
<Button onClick={chooseLocation}>选择位置</Button>
|
|
)
|
|
} |