Commit 177df08c82b0ae15997ef33d9b9e8b387e0c0d49
1 parent
fa18d722
提交12-4
Showing
1 changed file
with
77 additions
and
0 deletions
garbage-removal/src/pages/home-info/address/addSite.vue
| @@ -166,6 +166,7 @@ const submit = () => { | @@ -166,6 +166,7 @@ const submit = () => { | ||
| 166 | */ | 166 | */ |
| 167 | const chooseAddressDetail = () => { | 167 | const chooseAddressDetail = () => { |
| 168 | console.log('打开地图选择地址'); | 168 | console.log('打开地图选择地址'); |
| 169 | + takeLocation(); | ||
| 169 | 170 | ||
| 170 | // 创建选择方式的模态框 | 171 | // 创建选择方式的模态框 |
| 171 | const choiceContainer = document.createElement('div'); | 172 | const choiceContainer = document.createElement('div'); |
| @@ -362,6 +363,11 @@ const chooseAddressDetail = () => { | @@ -362,6 +363,11 @@ const chooseAddressDetail = () => { | ||
| 362 | center: center, | 363 | center: center, |
| 363 | zoom: 15 | 364 | zoom: 15 |
| 364 | }); | 365 | }); |
| 366 | + window.currentMapInstance = map; | ||
| 367 | + // 地图初始化完成后自动获取当前位置 | ||
| 368 | + setTimeout(() => { | ||
| 369 | + takeLocation(); | ||
| 370 | + }, 500); | ||
| 365 | 371 | ||
| 366 | // 添加点击事件监听器 | 372 | // 添加点击事件监听器 |
| 367 | qq.maps.event.addListener(map, 'click', function(event) { | 373 | qq.maps.event.addListener(map, 'click', function(event) { |
| @@ -816,8 +822,79 @@ const chooseAddressDetail = () => { | @@ -816,8 +822,79 @@ const chooseAddressDetail = () => { | ||
| 816 | }; | 822 | }; |
| 817 | } | 823 | } |
| 818 | 824 | ||
| 825 | +// 定义定位回调函数 | ||
| 826 | +const takeLocationCallBack = (lngLat) => { | ||
| 827 | + const ll = lngLat.split(","); | ||
| 828 | + console.log("定位结果:", ll); | ||
| 829 | + | ||
| 830 | + if (ll.length >= 2) { | ||
| 831 | + const longitude = parseFloat(ll[0]); | ||
| 832 | + const latitude = parseFloat(ll[1]); | ||
| 833 | + | ||
| 834 | + // 在地图上定位到当前位置 | ||
| 835 | + const mapContainer = document.getElementById('mapContainer'); | ||
| 836 | + if (mapContainer && window.qq && qq.maps && window.currentMapInstance) { | ||
| 837 | + const pos = new qq.maps.LatLng(latitude, longitude); | ||
| 838 | + window.currentMapInstance.setCenter(pos); | ||
| 839 | + window.currentMapInstance.setZoom(17); // 放大一些以便更好地查看位置 | ||
| 840 | + | ||
| 841 | + // 清除之前的标记(如果有的话) | ||
| 842 | + if (window.currentLocationMarker) { | ||
| 843 | + window.currentLocationMarker.setMap(null); | ||
| 844 | + } | ||
| 845 | + | ||
| 846 | + // 添加新的当前位置标记 | ||
| 847 | + window.currentLocationMarker = new qq.maps.Marker({ | ||
| 848 | + position: pos, | ||
| 849 | + map: window.currentMapInstance | ||
| 850 | + }); | ||
| 851 | + | ||
| 852 | + // 创建或更新信息窗口 | ||
| 853 | + if (!window.currentLocationInfoWindow) { | ||
| 854 | + window.currentLocationInfoWindow = new qq.maps.InfoWindow({ | ||
| 855 | + map: window.currentMapInstance | ||
| 856 | + }); | ||
| 857 | + } | ||
| 858 | + | ||
| 859 | + // 设置信息窗口内容并打开 | ||
| 860 | + window.currentLocationInfoWindow.setContent('<div style="padding:10px;">您的当前位置</div>'); | ||
| 861 | + window.currentLocationInfoWindow.setPosition(pos); | ||
| 862 | + window.currentLocationInfoWindow.open(); | ||
| 863 | + } | ||
| 864 | + } else { | ||
| 865 | + uni.showToast({ | ||
| 866 | + title: '定位结果异常', | ||
| 867 | + icon: 'none' | ||
| 868 | + }); | ||
| 869 | + } | ||
| 870 | +} | ||
| 871 | + | ||
| 872 | +const takeLocation = () => { | ||
| 873 | + // 调用原生接口获取定位 | ||
| 874 | + if (window.JsInterface && typeof window.JsInterface.takeLocation === 'function') { | ||
| 875 | + window.JsInterface.takeLocation(); | ||
| 876 | + } else { | ||
| 877 | + // 如果没有原生接口,使用uni.getLocation作为备选方案 | ||
| 878 | + uni.getLocation({ | ||
| 879 | + type: 'gcj02', | ||
| 880 | + success: function (res) { | ||
| 881 | + // 直接处理结果 | ||
| 882 | + handleLocationResult(res.longitude, res.latitude); | ||
| 883 | + }, | ||
| 884 | + fail: function (err) { | ||
| 885 | + console.error('定位失败:', err); | ||
| 886 | + uni.showToast({ | ||
| 887 | + title: '定位失败', | ||
| 888 | + icon: 'none' | ||
| 889 | + }); | ||
| 890 | + } | ||
| 891 | + }); | ||
| 892 | + } | ||
| 893 | +} | ||
| 894 | + | ||
| 819 | onMounted(() => { | 895 | onMounted(() => { |
| 820 | proxy.$refs.addressFrom.setRules(rules) | 896 | proxy.$refs.addressFrom.setRules(rules) |
| 897 | + window.takeLocationCallBack = takeLocationCallBack | ||
| 821 | }) | 898 | }) |
| 822 | 899 | ||
| 823 | onLoad((options) => { | 900 | onLoad((options) => { |