前端小程序调用 getLocation 实现地图位置功能,通过 纬度:latitude 经度: longitude 获取当前位置

server/2024/9/22 23:10:29/

1、首先登录一下 腾讯的位置服务 有账号就登录没账号就注册,

  1. 点击右上角的控制台
  2. 点击左侧的应用管理 ---> 我的应用  ---->> 创建应用

1、创建应用 

 

 

 

 2、列表就会显示我们刚刚创建好的 key

 

 

 3、点击添加 key

4、按照要求填写信息  我们用的是小程序 所以选择小程序 输入自己开发者工具的 APP ID 再点击添加,,

5、列表中会显示一个 key 把这个key 复制一下 

2、打开自己的 HBuilder X 选择  manifest.json 中的web配置中 把刚才复制的 key 填写完

 

 3、看 uni-app 官网的调用  uni.getLocation的API 来配置

1、在 pages.json 中配置一下 这一行的代码

 "requiredPrivateInfos": ["getLocation"],

 

4、在需要 引入地图的页面写我们需要的地图代码

 

<template><view class="header"><view class="page-body"><view class="page-section page-section-gap"><map class="mapDt" :latitude="latitude" :longitude="longitude" :markers="covers"></map></view><view class="position"><image class="aaaaa" src="../../static/saoma/yzy.png" mode=""></image></view></view></view>
</template><script setup>
import { onMounted, ref } from 'vue';
import { onLoad } from '@dcloudio/uni-app';
import { onReady } from '@dcloudio/uni-app';
const id = ref(10); // 使用 marker 点击事件需要填写 id
const title = 'map';
const latitude = ref(39.909);
const longitude = ref(116.39742);const covers = ref([// {//   id: 101,//   latitude: 39.909,//   longitude: 116.39742,//   iconPath: '../../static/login/logo.png',//   width: 50,//   height: 50// },// {//   id: 102,//   latitude: 39.7,//   longitude: 116.39,//   iconPath: '../../static/login/logo.png',//   width: 50,//   height: 50// }
]);const formattedMarkers = covers.value.map((marker) => ({...marker// id: +8 // 使用经纬度作为 marker 的 id
}));onReady(() => {console.log('onReady');uni.getLocation({type: 'wgs84',success: function (res) {console.log('当前位置的经度:' + res.longitude);console.log('当前位置的纬度:' + res.latitude);// covers.value[0].latitude = res.latitude;// covers.value[0].longitude = res.longitude;covers.value.push({id: 101,latitude: res.latitude,longitude: res.longitude,iconPath: '../../static/login/logo.png',width: 50,height: 50});latitude.value = res.latitude;longitude.value = res.longitude;},fail: (re) => {console.log(re);}});
});
</script>
<style lang="scss">
.mapDt {width: 100%;height: 100vh;
}
.aaaaa {height: 50rpx;width: 57rpx;
}
</style>

5、效果展示

 


http://www.ppmy.cn/server/40192.html

相关文章

【qt】纯代码界面设计

界面设计目录 一.界面设计的三种方式1.使用界面设计器2.纯代码界面设计3.混合界面设计 二.纯代码进行界面设计1.代码界面设计的总思路2.创建项目3.设计草图4.添加组件指针5.初始化组件指针6.添加组件到窗口①水平布局②垂直布局③细节点 7.定义槽函数8.初始化信号槽9.实现槽函数…

Linux 二十一章

&#x1f436;博主主页&#xff1a;ᰔᩚ. 一怀明月ꦿ ❤️‍&#x1f525;专栏系列&#xff1a;线性代数&#xff0c;C初学者入门训练&#xff0c;题解C&#xff0c;C的使用文章&#xff0c;「初学」C&#xff0c;linux &#x1f525;座右铭&#xff1a;“不要等到什么都没有了…

Python Web框架Django项目开发实战:多用户内容发布系统

注意:本文的下载教程,与以下文章的思路有相同点,也有不同点,最终目标只是让读者从多维度去熟练掌握本知识点。 下载教程:Python项目开发Django实战-多用户内容发布系统-编程案例解析实例详解课程教程.pdf 一、引言 在Web应用开发中,内容发布系统是一个常见的需求。这类系…

python中flask使用简要记录

文档层级概要 一、flask简要说明 二、uwsgi配置说明 三、启动 四、结果验证 之前有做过接口&#xff0c;后来写了许多算法和数据处理&#xff0c;对于接口大多时候是通过fastapi或调别人的接口。自己写的接口倒是没有多少。在这里使用uwsgi和flask及nginx进行配置&#xf…

力扣127.单词接龙讲解

距离上一次刷题已经过去了.........嗯............我数一一下............整整十天&#xff0c;今天再来解一道算法题 由于这段时间准备简历&#xff0c;没咋写博客。。今天回来了&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&#xff01;&…

如何在matlab时间序列中X轴标注月-日

一般我们使用的时间序列都是以年为单位&#xff0c;比如下图&#xff1a; 而如果要绘制月尺度的时间变化图&#xff0c;则需要调整X轴的标注。下面代码展示了如何绘制小时尺度的降水数据。 [sname2,lon2,lat2] kml2xy(GZ_.kml); nc_bound2 [lon2,lat2]; area_ind2inpolygon(e…

网络安全之DHCP详解

DHCP&#xff1a;Dynamic Host Configration Protocol 动态主机配置协议 某一协议的数据是基于UDP封装的&#xff0c;当它想确保自己的可靠性时&#xff0c;这个协议要么选确认重传机制&#xff0c;要么选周期性传输。 DHCP是确认重传&#xff0c;【UDP|DHCP】,当DHCP分配完地…

SpringBoot拦截器中使用RedisTemplate

这几天想着把登陆拦截器的验证规则修改一下&#xff0c;验证介质由session中获取改为从redis中获取&#xff0c;结果发现redisTemplate一直为空&#xff0c; Configuration public class WebInterceptorConfig implements WebMvcConfigurer {Overridepublic void addIntercept…