董国庆
2025-03-13 c64b1e69da3a72bd9cfd9d725fb812489c904199
H5/pages/location/location.vue
@@ -50,6 +50,7 @@
         };
      },
      methods: {
         clearMarkers() {
            // 清除所有标点
            this.markers.forEach((marker) => {
@@ -99,35 +100,6 @@
               }
            })
            return
            uni.request({
               url: `https://api.map.baidu.com/place/v2/search`,
               method: 'GET',
               data: {
                  query: this.keyword,
                  location: `${this.latitude},${this.longitude }`,
                  radius: 10000, // 搜索半径,单位:米
                  output: 'json',
                  ak: '3mHKIXMArjgIkgADzOlTYp4XssNSNkwr' // 替换为你自己的 AK
               },
               success: (res) => {
                  if (res.data.status === 0) {
                     this.searchResults = res.data.results;
                     this.latitude = res.data.results[0].location.lat
                     this.longitude = res.data.results[0].location.lng
                     this.locationList = res.data.results.map((result, index) => {
                        result.id = index + 1
                        return result
                     });
                  }
               },
               fail: (err) => {
                  console.error('搜索失败:', err);
               }
            });
         },
      },
      onLoad() {
@@ -136,34 +108,34 @@
      onReady() {
         this.map = new BMapGL.Map("container"); // 创建地图实例 
         const that = this
         // uni.getLocation({
         //    type: 'wgs84',
         //    success: (res) => {
         navigator.geolocation.getCurrentPosition(success, error, options);
         function success(position) {
            const latitude = position.coords.latitude;
            const longitude = position.coords.longitude;
            const accuracy = position.coords.accuracy; // 精度,单位米
            //  在这里可以使用获取到的经纬度信息,例如在地图上显示位置等。
            var point = new BMapGL.Point(longitude, latitude); // 创建点坐标
            that.map.enableScrollWheelZoom(true);
            that.map.centerAndZoom(point, 12);
            var marker = new BMapGL.Marker(point);
            that.map.addOverlay(marker);
            that.markers.push(marker);
            var myGeo = new BMapGL.Geocoder();
            // 根据坐标得到地址描述
            myGeo.getLocation(new BMapGL.Point(longitude, latitude), (result) => {
               that.locationList = result.surroundingPois.map((item, index) => {
                  item.id = index + 1
                  return item
               })
            })
            uni.request({
               url: `https://api.map.baidu.com/geoconv/v2/?coords=${position.coords.longitude},${position.coords.latitude}&model=2&ak=3mHKIXMArjgIkgADzOlTYp4XssNSNkwr`,
               method: 'GET',
               success: (res) => {
                  const latitude = res.data.result[0].y;
                  const longitude = res.data.result[0].x;
                  var point = new BMapGL.Point(longitude, latitude); // 创建点坐标
                  that.map.enableScrollWheelZoom(true);
                  that.map.centerAndZoom(point, 12);
                  var marker = new BMapGL.Marker(point);
                  that.map.addOverlay(marker);
                  that.markers.push(marker);
                  var myGeo = new BMapGL.Geocoder();
                  // 根据坐标得到地址描述
                  myGeo.getLocation(new BMapGL.Point(longitude, latitude), (result) => {
                     that.locationList = result.surroundingPois.map((item, index) => {
                        item.id = index + 1
                        return item
                     })
                  })
               },
            });
         }
         function error(err) {
            switch (err.code) {
               case err.PERMISSION_DENIED:
                  console.error("用户拒绝了位置请求。");
@@ -184,6 +156,7 @@
            timeout: 5000, // 5 秒超时
            maximumAge: 0 //  不使用缓存
         };
         this.map.addEventListener('click', (e) => {
            this.clearMarkers()
            var mercator = this.map.lnglatToMercator(e.latlng.lng, e.latlng.lat);
@@ -194,31 +167,29 @@
            var myGeo = new BMapGL.Geocoder();
            // 根据坐标得到地址描述    
            myGeo.getLocation(new BMapGL.Point(e.latlng.lng, e.latlng.lat), (result) => {
               console.log(result,'result');
               if( result.surroundingPois.length>0){
               this.city = result.addressComponents.city
               if (result.surroundingPois.length > 0) {
                  this.locationList = result.surroundingPois.map((item, index) => {
                     item.id = index + 1
                     return item
                  })
               }else{
                  this.locationList =[{
                     id:1,
                     title:result.address,
                     point:{lat:e.latlng.lat,lng:e.latlng.lng}
               } else {
                  this.locationList = [{
                     id: 1,
                     title: result.address,
                     point: {
                        lat: e.latlng.lat,
                        lng: e.latlng.lng
                     }
                  }]
               }
            })
            var marker = new BMapGL.Marker(point);
            this.map.addOverlay(marker);
            this.markers.push(marker);
         });
         // },
         // error:err=>{
         //
         // }
         // });
      }
   }
</script>