<android>webview 嵌套vue项目 采用高德地图地理位置权限获取onGeolocationPermissionsShowPrompt方法不回调

webview 嵌套vue项目  采用高德地图地理位置权限获取onGeolocationPermissionsShowPrompt方法不回调

最近嵌套vue项目时,vue里面接的高德地图,用户运动时需要获取本地的位置权限,但是总是获取不到,vue自然也不会有定位信息,我是这样解决的:

        String dir = getActivity().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        webSettings.setGeolocationEnabled(true);
        webSettings.setGeolocationDatabasePath(dir);

开启后 mWebView.setWebChromeClient();再setWebChromeClient的入参的类中重写

@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
    Log.e("", "获取地理权限");
    callback.invoke(origin, true, false);
    super.onGeolocationPermissionsShowPrompt(origin, callback);

    ActivityCompat.requestPermissions(getActivity(), new String[]{
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    }, 100);

}

这样的话基本就差不多了,但是发现还是走不到这个方法,于是网络各种查阅资料,查了整整一个晚上,终于在一个博客上看到,如果是androidstudio的话,高德规定需要把targetSdkVersion置为得23,我改完之后,发现果然是这样。在onGeolocationPermissionsShowPrompt方法里拿到了回调信息,功夫不负有心人啊,特此一记,谨记在心!

<!-- static/map.html --> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>高德地图</title> <script src="https://webapi.amap.com/loader.js"></script> <script src="https://cdn.bootcss.com/vue/2.6.11/vue.js"></script> <style> #map-container { width: 100%; height: 584px; border-radius: 22px; z-index: 4; } </style> </head> <body> <div id="map-container"></div> <script type="text/javascript"> window._AMapSecurityConfig = { securityJsCode: 'cf331d54f67e306ca5c0b17f7122d559' }; </script> <script src="https://webapi.amap.com/maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d&plugin=AMap.MarkerCluster"> </script> <script> // 解析URL参数 const params = new URLSearchParams(location.search); const points = JSON.parse(params.get('points')); const markers = JSON.parse(params.get('markers')); // 初始化地图 const map = new AMap.Map('map-container', { zoom: 10, center: [points[0].lng, points[0].lat], pitch: 45, showBuildingBlock: true }); markers.forEach(marker => { new AMap.Marker({ position: [marker.lng, marker.lat], title: marker.title, map: map }).on('click', () => { // 向UniApp发送点击事件 window.parent.postMessage({ type: 'markerClick', title: marker.title }, '*'); }); }); //构建带标题的HTML内容 markers.forEach(marker => { // 构建HTML内容(包含图片和标题) const content = ` <div style="position:relative; text-align:center;"> <img src="/static/images/flag.png" style="width:40px; height:50px; display:block;"> <div style="position:absolute; bottom:-90%; left:0%; transform:translateX(-50%); background: linear-gradient(180deg, rgba(227, 57, 59, 0.2) 0%, rgba(244, 124, 88, 0.2) 100%); padding:6px 0; border-radius: 18px; white-space:wrap; font-family:ShuHuiTi; font-size: 12px;font-weight: bold; margin-top:5px;border: 2px solid #F47C58;color:#E4393C;width:80px;"> ${marker.title} </div> </div> `; const markerObj = new AMap.Marker({ position: [marker.lng, marker.lat], content: content, // 使用HTML内容 anchor: 'bottom-center', // 锚点在底部中心 offset: new AMap.Pixel(0, -25) // 向上偏移 }); markerObj.on('click', () => { console.log(marker, '111'); // 将数据转为字符串 const message = JSON.stringify({ type: 'markerClick', title: marker.title }); window.parent.postMessage(message, '*'); }); map.add(markerObj); }); // 准备聚合数据 const clusterPoints = markers.map(marker => ({ lnglat: [marker.lng, marker.lat], title: marker.title, originalData: marker // 保留原始数据 })); // 点聚合初始化函数 function initCluster() { const cluster = new AMap.MarkerCluster(map, clusterPoints, { gridSize: 80, maxZoom: 15, minClusterSize: 2, renderClusterMarker: function(context) { const count = context.count; // 根据聚合点数量计算大小 const div = document.createElement('div'); div.className = 'cluster-marker'; div.style.backgroundColor = 'rgba(255,153,0,0.6)'; div.style.border = '1px solid rgba(255,153,0,1)'; div.style.borderRadius = '50%'; div.style.color = '#fff'; div.style.textAlign = 'center'; div.style.lineHeight = '40px'; div.style.width = '40px'; div.style.height = '40px'; div.innerHTML = count; return div; }, // 1. 添加标记点 renderMarker: function(context) { const markerData = context.data[0].originalData; // console.log('markerData.title', markerData.title); const content = ` <div style="position:relative; text-align:center;"> <img src="/static/images/flag.png" style="width:40px; height:50px; display:block;"> <div style="position:absolute; bottom:-90%; left:50%; transform:translateX(-50%); background: linear-gradient(180deg, rgba(227, 57, 59, 0.2) 0%, rgba(244, 124, 88, 0.2) 100%); padding:6px 0; border-radius: 18px; white-space:nowrap; font-family:ShuHuiTi; font-size: 12px; font-weight: bold; margin-top:5px; border: 2px solid #F47C58; color:#E4393C; width:80px;"> ${markerData.title} </div> </div> `; // console.log('markerDatat', markerData); const markerObj = new AMap.Marker({ position: [markerData.lng, markerData.lat], content: content, anchor: 'bottom-center', offset: new AMap.Pixel(0, -25) }); markerObj.on('click', function() { window.parent.postMessage({ type: 'markerClick', title: markerData.title }, '*'); }); return markerObj; } }); } // 加载点聚合插件 AMap.plugin('AMap.MarkerCluster', function() { initCluster(); // 插件加载完成后执行初始化 }); // 2. 绘制路线 const path = points.map(p => [p.lng, p.lat]); new AMap.Polyline({ path: path, strokeColor: "#3366FF", strokeWeight: 5, map: map }); // 3. 添加起点终点标记 points.forEach((point, i) => { new AMap.Marker({ position: [point.lng, point.lat], content: `<div class="point-label">${i === 0 ? '起' : '终'}</div>`, offset: new AMap.Pixel(-10, -10), map: map }); }); // 4. 绘制驾车路径(如果需要,可以注释掉直线路径,保留驾车路径) // 注意:这里同时绘制了直线和驾车路径,可能会重叠 if (points.length >= 2) { AMap.plugin('AMap.Driving', () => { const driving = new AMap.Driving({ map: map, // 这样驾车路线会直接显示在地图上 policy: AMap.DrivingPolicy.LEAST_TIME }); driving.search( [points[0].lng, points[0].lat], [points[points.length - 1].lng, points[points.length - 1].lat], (status, result) => { // 可以根据结果处理 } ); }); } </script> </body> </html>uniapp的vue2项目使用webview嵌套高德地图,地图聚合点无法正常显示,都是只有默认图标,还有地图点击事件,无法正常回传给 <view class="description-card"> <web-view :src="mapUrl" @message="handleMessage"></web-view> </view>// 接收WebView消息 handleMessage(e) { console.log(e, '点击了标记点'); // e.detail.data 是一个数组,数组的每个元素是H5发送的消息(字符串) const [message] = e.detail.data; try { const data = JSON.parse(message); if (data.type === 'markerClick') { // 处理标记点击 } } catch (e) { console.error('解析消息失败', e); } },
08-07
markerObj.on('click', () => { console.log(marker, '111'); // 将数据转为字符串 // const message = JSON.stringify({ // type: 'markerClick', // title: marker.title // }); // window.parent.postMessage(message, '*'); // 通过uni.postMessage发送数据 uni.postMessage({ data: { event: 'markerClick', id: i, position: marker.getPosition() } }); });{lng: 106.28776197503275, lat: 38.47105952636814, title: '鼓楼'} '111' map.html?key=f3323c24769b10828db07f07c0fcec0d&points=%5B%7B%22lng%22%3A106.235589%2C%22lat%22%3A38.45766%2C%22name%22%3A%22%E8%B5%B7%E7%82%B9%22%7D%2C%7B%22lng%22%3A106.287761%2C%22lat%22%3A38.4710595%2C%22name%22%3A%22%E9%BC%93%E6%A5%BC%22%7D%5D&markers=%5B%7B%22lng%22%3A106.28776197503275%2C%22lat%22%3A38.47105952636814%2C%22title%22%3A%22%E9%BC%93%E6%A5%BC%22%7D%2C%7B%22lng%22%3A106.01313200136352%2C%22lat%22%3A38.434956774850576%2C%22title%22%3A%22%E8%A5%BF%E5%A4%8F%E9%99%B5%22%7D%2C%7B%22lng%22%3A106.235068%2C%22lat%22%3A38.484468%2C%22title%22%3A%22%E5%AE%81%E5%A4%8F%E5%8D%9A%E7%89%A9%E9%A6%86%22%7D%2C%7B%22lng%22%3A106.123554%2C%22lat%22%3A38.231086%2C%22title%22%3A%22%E5%AE%81%E5%A4%8F%E5%B7%A5%E5%A7%94%E7%BA%AA%E5%BF%B5%E9%A6%86%22%7D%2C%7B%22lng%22%3A105.975629%2C%22lat%22%3A38.244193%2C%22title%22%3A%22%E9%97%BD%E5%AE%81%E6%96%B0%E8%B2%8C%E5%B1%95%E7%A4%BA%E4%B8%AD%E5%BF%83%22%7D%2C%7B%22lng%22%3A106.381172%2C%22lat%22%3A38.566882%2C%22title%22%3A%22%E7%81%B5%E6%AD%A6%E5%B8%82%E5%9B%BD%E5%AE%B6%E7%94%9F%E6%80%81%E6%96%87%E6%98%8E%E6%95%99%E8%82%B2%E5%9F%BA%E5%9C%B0%22%7D%2C%7B%22lng%22%3A106.584342%2C%22lat%22%3A38.159979%2C%22title%22%3A%22%E5%9B%BD%E5%AE%B6%E8%83%BD%E6%BA%90%E9%9B%86%E5%9B%A2%E5%AE%81%E5%A4%8F%E7%85%A4%E4%B8%9A%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%E5%AE%9E%E8%B7%B5%E6%95%99%E8%82%B2%E5%9F%BA%E5%9C%B0%22%7D%5D:91 Uncaught ReferenceError: uni is not defined at z.<anonymous> (map.html?key=f3323c24769b10828db07f07c0fcec0d&points=%5B%7B%22lng%22%3A106.235589%2C%22lat%22%3A38.45766%2C%22name%22%3A%22%E8%B5%B7%E7%82%B9%22%7D%2C%7B%22lng%22%3A106.287761%2C%22lat%22%3A38.4710595%2C%22name%22%3A%22%E9%BC%93%E6%A5%BC%22%7D%5D&markers=%5B%7B%22lng%22%3A106.28776197503275%2C%22lat%22%3A38.47105952636814%2C%22title%22%3A%22%E9%BC%93%E6%A5%BC%22%7D%2C%7B%22lng%22%3A106.01313200136352%2C%22lat%22%3A38.434956774850576%2C%22title%22%3A%22%E8%A5%BF%E5%A4%8F%E9%99%B5%22%7D%2C%7B%22lng%22%3A106.235068%2C%22lat%22%3A38.484468%2C%22title%22%3A%22%E5%AE%81%E5%A4%8F%E5%8D%9A%E7%89%A9%E9%A6%86%22%7D%2C%7B%22lng%22%3A106.123554%2C%22lat%22%3A38.231086%2C%22title%22%3A%22%E5%AE%81%E5%A4%8F%E5%B7%A5%E5%A7%94%E7%BA%AA%E5%BF%B5%E9%A6%86%22%7D%2C%7B%22lng%22%3A105.975629%2C%22lat%22%3A38.244193%2C%22title%22%3A%22%E9%97%BD%E5%AE%81%E6%96%B0%E8%B2%8C%E5%B1%95%E7%A4%BA%E4%B8%AD%E5%BF%83%22%7D%2C%7B%22lng%22%3A106.381172%2C%22lat%22%3A38.566882%2C%22title%22%3A%22%E7%81%B5%E6%AD%A6%E5%B8%82%E5%9B%BD%E5%AE%B6%E7%94%9F%E6%80%81%E6%96%87%E6%98%8E%E6%95%99%E8%82%B2%E5%9F%BA%E5%9C%B0%22%7D%2C%7B%22lng%22%3A106.584342%2C%22lat%22%3A38.159979%2C%22title%22%3A%22%E5%9B%BD%E5%AE%B6%E8%83%BD%E6%BA%90%E9%9B%86%E5%9B%A2%E5%AE%81%E5%A4%8F%E7%85%A4%E4%B8%9A%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8%E5%AE%9E%E8%B7%B5%E6%95%99%E8%82%B2%E5%9F%BA%E5%9C%B0%22%7D%5D:91:6) at f.emit (maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d&plugin=AMap.MarkerCluster:1:209434) at z.emit (maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d&plugin=AMap.MarkerCluster:1:985864) at n.ci (maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d&plugin=AMap.MarkerCluster:1:727096) at n.fireEvent (maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d&plugin=AMap.MarkerCluster:1:723206) at n.tt (maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d&plugin=AMap.MarkerCluster:1:724624)
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值