This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: baidu map demo * chore: format code --------- Co-authored-by: likui628 <[email protected]>
- Loading branch information
1 parent
95e63dc
commit 03ca604
Showing
7 changed files
with
186 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
<script lang="ts" setup> | ||
import { Baidu } from '@vben/demo' | ||
</script> | ||
<template> | ||
<div>Demo待补充</div> | ||
<Baidu /> | ||
</template> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<script lang="ts" setup> | ||
import { ref, unref, onMounted } from 'vue' | ||
import { useDebounceFn } from '@vben/utils' | ||
import { useScript } from '@vben/hooks' | ||
defineProps({ | ||
width: { | ||
type: String, | ||
default: '100%', | ||
}, | ||
height: { | ||
type: String, | ||
default: 'calc(100vh - 90px)', | ||
}, | ||
}) | ||
const emit = defineEmits(['change-point', 'point-changed']) | ||
const wrapRef = ref<HTMLDivElement | null>(null) | ||
const map = ref() | ||
const BMap = ref() | ||
const geo = ref() | ||
const BAI_DU_MAP_URL = | ||
'https://api.map.baidu.com/api?v=3.0&ak=qiDNjcA9vYIY7HhzebKkWue4lpSR5z9M&callback=initialize' | ||
const { load } = useScript({ src: BAI_DU_MAP_URL }) | ||
const initBaiduMap = async () => { | ||
await load() | ||
setTimeout(() => { | ||
const wrapEl = unref(wrapRef) | ||
if (!wrapEl) return | ||
BMap.value = (window as any).BMap | ||
map.value = new BMap.value.Map(wrapEl) | ||
geo.value = new BMap.value.Geocoder() | ||
const point = new BMap.value.Point(120.373487, 31.508123) | ||
map.value.centerAndZoom(point, 15) | ||
map.value.enableScrollWheelZoom(true) | ||
map.value.addEventListener('click', function (e) { | ||
map.value.clearOverlays() | ||
const pt = e.point | ||
const marker = new BMap.value.Marker(new BMap.value.Point(pt.lng, pt.lat)) | ||
map.value.addOverlay(marker) | ||
latlng2text(pt) | ||
emit('change-point', e.point) | ||
}) | ||
}, 100) | ||
} | ||
// 地理坐标转化成具体的地理位置 | ||
const latlng2text = (point) => { | ||
geo.value.getLocation(point, function (rs) { | ||
if (rs) { | ||
emit('point-changed', rs.address) | ||
} else { | ||
console.log('The address you selected did not resolve to a result') | ||
} | ||
}) | ||
} | ||
const handleInput = useDebounceFn((value: string) => { | ||
geo.value.getPoint(value, (point) => { | ||
if (point) { | ||
map.value.centerAndZoom(point, 16) | ||
} else { | ||
console.log('Geo can not find this place') | ||
} | ||
}) | ||
}, 500) | ||
onMounted(() => { | ||
initBaiduMap() | ||
}) | ||
</script> | ||
|
||
<template> | ||
<div class="bmap-input"> | ||
<VbenInput clearable placeholder="search" @input="handleInput"> | ||
<template #prefix> | ||
<VbenIconify | ||
:size="20" | ||
color="gray" | ||
icon="ant-design:search-outlined" | ||
/> | ||
</template> | ||
</VbenInput> | ||
</div> | ||
<div ref="wrapRef" :style="{ height, width }"></div> | ||
</template> | ||
|
||
<style lang="less" scoped> | ||
.bmap-input { | ||
margin: 15px; | ||
position: absolute; | ||
width: 320px; | ||
z-index: 10000; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { onMounted, onUnmounted, ref } from 'vue' | ||
|
||
interface ScriptOptions { | ||
src: string | ||
} | ||
|
||
export function useScript(opts: ScriptOptions) { | ||
const isLoading = ref(false) | ||
const error = ref(false) | ||
const success = ref(false) | ||
let script: HTMLScriptElement | ||
|
||
const promise = new Promise<void>((resolve, reject) => { | ||
onMounted(() => { | ||
script = document.createElement('script') | ||
script.type = 'text/javascript' | ||
script.onload = function () { | ||
isLoading.value = false | ||
success.value = true | ||
error.value = false | ||
resolve() | ||
} | ||
|
||
script.onerror = function (err) { | ||
isLoading.value = false | ||
success.value = false | ||
error.value = true | ||
reject(err) | ||
} | ||
|
||
script.src = opts.src | ||
document.head.appendChild(script) | ||
}) | ||
}) | ||
|
||
onUnmounted(() => { | ||
script && script.remove() | ||
}) | ||
|
||
return { | ||
isLoading, | ||
error, | ||
success, | ||
load: () => promise, | ||
} | ||
} |
Oops, something went wrong.