不bb,上代码。复制粘贴可用
<template>
<div class="view-home">
{{ latitude }} --- {{ longitude}}
</div>
</template>
<script lang="ts" setup>
import { onMounted, ref} from "vue";
const latitude = ref(null);
const longitude = ref(null);
const error = ref("");
onMounted(() => {
getLocation();
})
const getLocation = () => {
if (!navigator.geolocation) {
error.value = "当前浏览器不支持获取经纬度!";
return;
}
navigator.geolocation.getCurrentPosition(
(position) => {
latitude.value = position.coords.latitude;
longitude.value = position.coords.longitude;
},
(err) => {
error.value = "Error: " + err.message;
}
);
};
</script>