58 lines
1.6 KiB
TypeScript
58 lines
1.6 KiB
TypeScript
export type ThirdPartyMapProvider = 'amap' | 'baidu' | 'tencent'
|
|
|
|
export interface ThirdPartyMapSearchTarget {
|
|
keyword: string
|
|
region: string
|
|
}
|
|
|
|
export interface ThirdPartyMapProviderOption {
|
|
provider: ThirdPartyMapProvider
|
|
label: string
|
|
}
|
|
|
|
export const THIRD_PARTY_MAP_PROVIDERS: ThirdPartyMapProviderOption[] = [
|
|
{ provider: 'amap', label: '高德地图' },
|
|
{ provider: 'baidu', label: '百度地图' },
|
|
{ provider: 'tencent', label: '腾讯地图' }
|
|
]
|
|
|
|
const encode = (value: string) => encodeURIComponent(value)
|
|
|
|
export const buildThirdPartyMapSearchUri = (
|
|
provider: ThirdPartyMapProvider,
|
|
target: ThirdPartyMapSearchTarget
|
|
) => {
|
|
const keyword = target.keyword.trim()
|
|
const region = target.region.trim()
|
|
|
|
switch (provider) {
|
|
case 'amap':
|
|
return `https://uri.amap.com/search?keyword=${encode(keyword)}&city=${encode(region)}&src=museum-guide&callnative=1`
|
|
case 'baidu':
|
|
return `https://api.map.baidu.com/place/search?query=${encode(keyword)}®ion=${encode(region)}&output=html&src=museum-guide`
|
|
case 'tencent':
|
|
return `https://apis.map.qq.com/uri/v1/search?keyword=${encode(keyword)}®ion=${encode(region)}&referer=museum-guide`
|
|
default: {
|
|
const exhaustive: never = provider
|
|
return exhaustive
|
|
}
|
|
}
|
|
}
|
|
|
|
export const openThirdPartyMapSearch = (
|
|
provider: ThirdPartyMapProvider,
|
|
target: ThirdPartyMapSearchTarget
|
|
) => {
|
|
const uri = buildThirdPartyMapSearchUri(provider, target)
|
|
|
|
if (typeof window !== 'undefined' && window.location) {
|
|
window.location.href = uri
|
|
return
|
|
}
|
|
|
|
uni.showToast({
|
|
title: '请在 H5 浏览器中打开地图',
|
|
icon: 'none'
|
|
})
|
|
}
|