实现定位点的功能

master
叶志超 2022-04-08 00:22:07 +08:00
parent d24c9643ce
commit 4dc89128da
6 changed files with 68 additions and 17 deletions

8
src/api/util.js Normal file
View File

@ -0,0 +1,8 @@
export const isLnglat = lnglat => {
const { lng, lat } = lnglat
if (isNaN(lng) || lng > 180 || lng < -180) return false
if (isNaN(lat) || lat > 90 || lat < -90 ) return false
return true
}

View File

@ -1,7 +1,11 @@
<template> <template>
<Pin :position="{ lng: 116.4, lat: 39.9 }" /> <Pin :position="position" />
</template> </template>
<script setup> <script setup>
import Pin from '/@/components/Pin.vue' import Pin from '/@/components/Pin.vue'
import { usePositionStore } from '/@/stores/position'
const position = usePositionStore()
</script> </script>

View File

@ -1,24 +1,34 @@
<template> <template>
<form> <form @keyup.enter="onFlyBtnClick">
<label>经度</label> <label>经度</label>
<input type="text" id="lon-input" placeholder="请输入经度值"> <input type="number" v-model="lng" placeholder="请输入经度值">
<label>纬度</label> <label>纬度</label>
<input type="text" id="lat-input" placeholder="请输入纬度值"> <input type="number" v-model="lat" placeholder="请输入纬度值">
<input type="button" id="fly-btn" @click="onFlyBtnClick" value="飞到这个点"> <input type="button" id="fly-btn" @click="onFlyBtnClick" value="飞到这个点">
<input type="button" id="clear-btn" @click="onClearBtnClick" value="清除坐标"> <input type="button" id="clear-btn" @click="onClearBtnClick" value="清除坐标">
</form> </form>
</template> </template>
<script setup> <script setup>
import { usePointStore } from '../stores/point' import { ref } from 'vue';
import { usePointStore } from '/@/stores/point'
import { usePositionStore } from '/@/stores/position'
const lng = ref(NaN)
const lat = ref(NaN)
const position = usePositionStore()
const onFlyBtnClick = () => { const onFlyBtnClick = () => {
if ( !position.set({ lng: lng.value, lat: lat.value }) ) alert('请输入正确的经纬度信息')
} }
const point = usePointStore() const point = usePointStore()
const onClearBtnClick = () => { const onClearBtnClick = () => {
lng.value = NaN
lat.value = NaN
position.$reset()
point.$reset() point.$reset()
} }
@ -52,6 +62,18 @@ input:focus {
outline: none; outline: none;
} }
/* Chrome, Safari, Edge, Opera */
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
/* Firefox */
input[type=number] {
-moz-appearance: textfield;
}
input#fly-btn, input#clear-btn { input#fly-btn, input#clear-btn {
border-radius: 3px; border-radius: 3px;
background-color: #41ddee; background-color: #41ddee;

View File

@ -19,7 +19,7 @@
<script setup> <script setup>
import { computed } from 'vue' import { computed } from 'vue'
import { usePointStore } from '../stores/point' import { usePointStore } from '/@/stores/point'
const point = usePointStore() const point = usePointStore()
const show = computed(() => point.index) const show = computed(() => point.index)

View File

@ -5,9 +5,9 @@
</template> </template>
<script setup> <script setup>
import { VcEntity, VcGraphicsBillboard } from 'vue-cesium'
import { computed } from 'vue' import { computed } from 'vue'
import { VcEntity, VcGraphicsBillboard } from 'vue-cesium'
import { isLnglat } from '/@/api/util'
import pin from '/@/assets/pin.png' import pin from '/@/assets/pin.png'
const props = defineProps({ const props = defineProps({
@ -21,12 +21,5 @@ const props = defineProps({
} }
}) })
const show = computed(() => { const show = computed(() => isLnglat(props.position))
const { lng, lat } = props.position
if (isNaN(lng) || lng > 180 || lng < -180) return false
if (isNaN(lat) || lat > 90 || lat < -90 ) return false
return true
})
</script> </script>

24
src/stores/position.js Normal file
View File

@ -0,0 +1,24 @@
import { defineStore } from 'pinia'
import { isLnglat } from '/@/api/util'
export const usePositionStore = defineStore({
id: 'position',
state: () => ({
lng: NaN,
lat: NaN,
}),
getters: {
value: (state) => { state.lng, state.lat }
},
actions: {
set(position) {
if ( !isLnglat(position) ) return false
this.lng = position.lng
this.lat = position.lat
return true
},
},
})