uniapp nvue map组件

注意:

尽可能在调用api的时候不要使用箭头函数,
会造成this指向问题 拿不到数据
关键:控制台也不会报错

在这里插入图片描述

uni.getLocation 实时定位功能

  1. uni.getLocation开启实时定位功能
  2. 配合 markers 显示控件 (当前位置和地址
  3. 注意:uniapp中markers 数据赋值修改 必须 把markers数据都重新修改

设置地图宽高

//原生方法
const mapSearch = weex.requireModule('mapSearch')

<map 
class="mapStyle"   // 样式  nuve 只支持 .css 样式选择器
:style="`width:${windowWidth}px; height: ${windowHeight}px;`"    //  uni.getSystemInfo
id="mapStyleId" 
:latitude="latitude" 
:longitude="longitude" 
:markers="markers"  // 控件显示1.定位,2.气泡显示
@regionchange="regionchangeFn"  // 拖动地图 获中心经纬度 反解析获取地址
</map>
// 获取屏幕宽高,设置地图
uni.getSystemInfo({
	success: function(res) {
		that.windowWidth = res.windowWidth
		that.windowHeight = res.windowHeight
		that.screenHeight = res.windowHeight
	}
})

markers 设置地图控件ioc 和气泡

	uni.getLocation({
		type: 'wgs84',
		geocode: true,
		success: function (res) {
		that.latitude=res.latitude
		that.longitude=res.longitude
		that.selectTree = res.address.city.replace(/\市/, '')
		that.markers = [{
				id: 121, 	// id作为控件的唯一标识,用于修改拖动地图或者修改地址的时候 更改控件
				latitude: res.longitude, //经纬度
				longitude: res.longitude,
				iconPath: '../../static/images/ic_position.png',  //控件图标,可修改 
				width: 25, // 控件图标大小 
				height: 40,
				callout:{   // 控件气泡展示内容
					content: `${res.address.street}${res.address.streetNum}${res.address.poiName}`,
					color: '#333848',
					borderRadius: 5,
					padding: 10,
					display:'ALWAYS',
				}
		}]
	},
	//最好加上 真机调试的时候返回错误信息 便于调试
	fail: function (err) {  },    
	complete: function (err) {  }    
})

@regionchangeFn 拖动地图 从新定位 获取地址

1.@regionchangeFn 拖动函数 返回开始和结尾两个参数
2.配合mapContext .getCenterLocation 获取中心点经纬度
3.配合mapSearch.reverseGeocode 反解析地址 修改控件

			// 两个参数 移动前后 start 和 end
			regionchangeFn (e) {
				if(e.type =='end'){
					this.getCenterLanLat()
				}
			},
			getCenterLanLat() {
				let that = this
				let mapContext = uni.createMapContext("mapStyleId", this);
				mapContext.getCenterLocation({
					type: 'wgs84',
					success: (res) => {
						mapContext.translateMarker({
							markerId:121,  
							destination:{longitude:res.longitude,latitude:res.latitude},
							duration: 1000,
						})  
						let point = {latitude:res.latitude, longitude:res.longitude}
						mapSearch.reverseGeocode({point}, function (res) {
							let i = res.address.indexOf('市')
							let addressName = res.address.substr(i+1)
							that.covers = [{
								id: 121,
								latitude: point.longitude,
								longitude: point.longitude,
								iconPath: '../../static/images/ic_position.png',
								width: 25,
								height: 40,
								callout:{
								   content: addressName,
								   color: '#333848',
								   borderRadius: 5,
								   padding: 10,
								   display:'ALWAYS',
								}
							}]
						})
						
					}
				})
			},

mapSearch.poiSearchNearBy 周边搜索功能

<input 
class="addres_input" 
v-model="addresInput" 
@input="searchAddressFn" 
confirm-type="search" 
placeholder-style="#AEB1BB" 
placeholder="如:郑汴路东明路" 
/>
			searchAddressFn (e) {
				this.isMapList = true
				this.isfadeInfo = true
				let option = {
					point: {
						latitude: this.latitude,
						longitude: this.longitude,
					},
					key: e.detail.value,  // 搜索关键字
					radius: 3000  // 搜索范围 3公里
				}
				mapSearch.poiSearchNearBy(option, (res)=>{
					console.log(res.poiList)
					if(res.poiList && res.poiList.length>0){
						this.addressList = res.poiList
					}
				})
			}

在这里插入图片描述

### UniAppmap 组件的使用教程 #### 属性介绍 在 UniApp 开发环境中,`<map>` 组件提供了丰富的属性来配置地图展示效果。这些属性允许开发者定制化地图的行为和外观[^1]。 - `longitude`: 设置地图中心点经度。 - `latitude`: 设置地图中心点纬度。 - `scale`: 地图缩放级别,默认值为 16。 - `markers`: 定义要在地图上显示的一系列标记点数组。每个标记对象支持多个参数设置,如位置、图标路径等。 对于自定义图标不展示的问题,在微信小程序环境下需注意:如果希望显示自定义图标,则应采用外链形式而非本地图片资源[^4]。 ```html <!-- 示例代码 --> <template> <view class="container"> <!-- 地图组件 --> <map style="width: 100%; height: 300px;" :longitude="lng" :latitude="lat" :markers="covers"></map> </view> </template> <script> export default { data() { return { lng: 113.32452, lat: 23.10229, covers: [ { id: 'A', latitude: 23.10229, longitude: 113.32452, iconPath: '/static/images/marker.png', // 如果是在微信小程序中建议改为网络链接 width: 20, height: 30 } ] }; }, }; </script> ``` #### 动态加载 marks 标记 为了提高用户体验并优化性能表现,可以通过编程方式动态更新地图上的 markers 数据集。这不仅限于初始渲染阶段,还可以响应用户的交互操作实时调整标记内容或样式[^2]。 #### 实际应用场景案例分析 通过具体实例进一步理解如何利用 `<map>` 和 nvue 技术栈构建复杂的应用场景。例如,在一个旅游类应用里,当用户浏览某个城市景点时,程序可以根据当前视窗范围内的兴趣点数量自动调节缩放比例,并且只呈现那些位于屏幕可见区域内的地点信息给访客查看[^3]。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值