|
显示定位失败是为什么,已经把位置的权限给打开了,代码是有哪里写错了吗
- <template>
- <safe-area class="page">
- <text>当前位置: {{ currentPosition }}</text> <!-- 显示当前位置 -->
- <view id="mapContainer" style="width: 100%; height: 100%;"></view>
- </safe-area>
- </template>
- <script>
- export default {
- name: 'map1',
- data() {
- return {
- map: null, // 用于存储地图实例
- aMapLBS: null, // 用于存储定位插件实例
- currentPosition: '' // 用于存储当前位置的经纬度
- };
- },
- mounted() {
- this.initMap();
- },
- methods: {
- initMap() {
- var aMap = api.require('aMap');
- this.setupPrivacy(aMap); // 封装隐私协议调用
- // 确保地图容器的宽高设置正确
- var mapContainer = document.getElementById('mapContainer');
- mapContainer.style.width = '100%';
- mapContainer.style.height = '100%';
- // 通过 open 方法打开地图
- this.map = aMap.open({
- rect: {
- x: 0,
- y: 0,
- w: '100%',
- h: '100%'
- },
- showUserLocation: true,
- zoomLevel: 10,
- center: {
- lon: 116.40,
- lat: 39.90
- },
- fixedOn: api.frameName
- }, (ret, err) => {
- if (ret && ret.status) {
- console.log('地图加载成功');
- this.initLocation(); // 地图加载成功后初始化定位
- } else {
- console.error('地图加载失败', err);
- }
- });
- },
- setupPrivacy(aMap) {
- aMap.updateMapViewPrivacy({
- privacyAgree: 'didAgree',
- privacyShow: 'didShow',
- containStatus: 'didContain'
- });
- aMap.updateSearchPrivacy({
- privacyAgree: 'didAgree',
- privacyShow: 'didShow',
- containStatus: 'didContain'
- });
- },
- initLocation() {
- var aMapLBS = api.require('aMapLBS');
- this.aMapLBS = aMapLBS; // 保存实例
- // 设置定位类隐私权限
- aMapLBS.updateLocationPrivacy({
- privacyAgree: 'didAgree',
- privacyShow: 'didShow',
- containStatus: 'didContain'
- });
- // 配置定位信息
- aMapLBS.configManager({
- accuracy: 'best', // 设置最高定位精度
- filter: 1
- }, (ret, err) => {
- console.log("高德坐标配置定位信息" + JSON.stringify(ret));
- if (ret.status) {
- aMapLBS.singleLocation({
- timeout: 10
- }, (ret, err) => {
- console.log("高德坐标单次定位" + JSON.stringify(ret));
- if (ret.status) {
- this.successFun(ret);
- } else {
- this.errorFun(err);
- }
- });
- } else {
- console.error('定位配置失败', err);
- }
- });
- },
- successFun(ret) {
- // 定位成功的处理逻辑
- this.currentPosition = `纬度: ${ret.lat}, 经度: ${ret.lon}`;
- if (this.map) {
- this.map.setCenter({ lat: ret.lat, lon: ret.lon });
- }
- },
- errorFun(err) {
- // 定位失败的处理逻辑
- api.toast({
- msg: '定位失败',
- location: "middle"
- });
- this.currentPosition = '无法获取当前位置,请检查定位权限和网络连接。';
- }
- }
- }
- </script>
- <style>
- .page {
- height: 100%;
- background: #fff;
- }
-
- #mapContainer {
- width: 100%;
- height: 100%;
- }
- </style>
复制代码
|
|