wangxy пре 3 месеци
родитељ
комит
c6fca6270a
4 измењених фајлова са 99 додато и 23 уклоњено
  1. 55 22
      components/customMap/customMap.vue
  2. 22 1
      components/customMap/useMap.js
  3. 3 0
      manifest.json
  4. 19 0
      utils/common.js

+ 55 - 22
components/customMap/customMap.vue

@@ -15,7 +15,8 @@
 			:markers="data.covers" :show-location="true"> </map>
 
 		<view v-if="result" class="map-card-list">
-			<view v-for="(item,index) in result.data.data" key="index" @click="xuanzeAdress(item)" class="map-card-box">
+      <!--  #ifdef H5   -->
+			<view v-for="(item,index) in result.data" key="index" @click="xuanzeAdress(item)" class="map-card-box">
 				<view>
 					<view class="map-card-title"><text v-if="index==0" class="dqwz-text">当前位置</text>{{item.title}}
 					</view>
@@ -23,7 +24,17 @@
 				</view>
 				<icon class="map-active-icon" v-if="index==0"></icon>
 			</view>
-
+      <!--   #endif   -->
+      <!--  #ifdef APP   -->
+      <view v-for="(item,index) in result.data.data" key="index" @click="xuanzeAdress(item)" class="map-card-box">
+        <view>
+          <view class="map-card-title"><text v-if="index==0" class="dqwz-text">当前位置</text>{{item.title}}
+          </view>
+          <view class="map-card-address">{{item.address}}</view>
+        </view>
+        <icon class="map-active-icon" v-if="index==0"></icon>
+      </view>
+      <!--   #endif   -->
 		</view>
 	</view>
 </template>
@@ -64,26 +75,48 @@
 	function onSuccess(res) {
 		console.log('数据', res);
 		result.value = res
-		if (res.data && res.data.data && res.data.data.length > 0) {
-					// 获取第一个搜索结果的经纬度
-					const firstResult = res.data.data[0];
-					const newLatitude = firstResult.location.lat; // 纬度
-					const newLongitude = firstResult.location.lng; // 经度
-		
-					// 更新地图的中心点
-					data.latitude = newLatitude;
-					data.longitude = newLongitude;
-		
-					// 更新标记点
-					data.covers = [{
-						latitude: newLatitude,
-						longitude: newLongitude,
-						iconPath: myIcon, // 使用你的图标路径
-						width: 30,
-						height: 30
-					}];
-				}
-		
+    // #ifdef H5
+    if (res && res.data && res.data.length > 0) {
+      // 获取第一个搜索结果的经纬度
+      const firstResult = res.data[0];
+      const newLatitude = firstResult.location.lat; // 纬度
+      const newLongitude = firstResult.location.lng; // 经度
+
+      // 更新地图的中心点
+      data.latitude = newLatitude;
+      data.longitude = newLongitude;
+
+      // 更新标记点
+      data.covers = [{
+        latitude: newLatitude,
+        longitude: newLongitude,
+        iconPath: myIcon, // 使用你的图标路径
+        width: 30,
+        height: 30
+      }];
+    }
+    // #endif
+    // #ifdef APP
+    if (res.data && res.data.data && res.data.data.length > 0) {
+      // 获取第一个搜索结果的经纬度
+      const firstResult = res.data.data[0];
+      const newLatitude = firstResult.location.lat; // 纬度
+      const newLongitude = firstResult.location.lng; // 经度
+
+      // 更新地图的中心点
+      data.latitude = newLatitude;
+      data.longitude = newLongitude;
+
+      // 更新标记点
+      data.covers = [{
+        latitude: newLatitude,
+        longitude: newLongitude,
+        iconPath: myIcon, // 使用你的图标路径
+        width: 30,
+        height: 30
+      }];
+    }
+    // #endif
 	}
 
 	function onError(err) {

+ 22 - 1
components/customMap/useMap.js

@@ -4,6 +4,9 @@ import request from '@/utils/request'
 import {
 	ref
 } from "vue"
+import {
+	jsonp
+} from "@/utils/common.js"
 
 // 关键词不全
 const httpUrl = "https://apis.map.qq.com/ws/place/v1/search"
@@ -19,12 +22,29 @@ export function useCustomMap() {
 		uni.showLoading({
 			title: '加载中'
 		})
+		// #ifdef H5
+		jsonp(httpUrl, {
+			key: Key,
+			output: "jsonp",
+			keyword: keyword,
+			boundary: cboundary,
+			page_size: csize,
+			page_index: cpage,
+		}).then(res => {
+			successFun && successFun(res)
+		}).catch(err => {
+			errorFun && errorFun(err);
+		}).finally(() => {
+			uni.hideLoading();
+		})
+		// #endif
+		// #ifdef APP
 		uni.request({
 			url: httpUrl,
 			method: 'get',
 			data: {
 				key: Key,
-				output: "json",
+				output: "JSON",
 				keyword: keyword,
 				boundary: cboundary,
 				page_size: csize,
@@ -39,6 +59,7 @@ export function useCustomMap() {
 		}).finally(() => {
 			uni.hideLoading();
 		})
+		// #endif
 	}
 
 	return {

+ 3 - 0
manifest.json

@@ -155,6 +155,9 @@
             "maps" : {
                 "qqmap" : {
                     "key" : "KBYBZ-FS3KZ-U2FXR-7VPDX-L7J46-23F55"
+                },
+                "tencent" : {
+                    "key" : "KBYBZ-FS3KZ-U2FXR-7VPDX-L7J46-23F55"
                 }
             }
         }

+ 19 - 0
utils/common.js

@@ -125,4 +125,23 @@ export function formatSecondsToCnhms(value, isNoZero) {
         result = `${h}时${m}分${s}秒`;
     }
     return result;
+}
+
+
+// utils/jsonp.js
+export function jsonp(url, params, callbackName = 'jsonp_callback') {
+  return new Promise((resolve, reject) => {
+    const queryString = new URLSearchParams(params).toString();
+    const fullUrl = `${url}?${queryString}&callback=${callbackName}`;
+    
+    window[callbackName] = (data) => {
+      resolve(data);
+      delete window[callbackName];
+    };
+
+    const script = document.createElement('script');
+    script.src = fullUrl;
+    script.onerror = (error) => reject(error);
+    document.body.appendChild(script);
+  });
 }