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
					}
				})
			}

在这里插入图片描述

<think>好的,我现在需要帮助用户解决如何将nvue项目转换为uniappx项目的问题。首先,我需要确认用户的需求,他们可能希望将现有的使用nvue技术的UniApp项目迁移到新的Uniappx架构中。接下来,我需要回顾自己已有的知识,特别是关于nvueUniappx的区别,以及迁移步骤。 首先,nvueUniApp中用于原生渲染的页面格式,使用Weex技术,而Uniappx可能是一个新的架构或规范,可能涉及不同的组件系统或构建流程。根据用户提供的引用内容,UniApp支持组件导入,特别是通过easycom机制自动注册组件,这可能在新项目中仍然适用。另外,引用中提到HBuilderX的编译目录从h5调整为web,可能需要更新部署流程。 接下来,我需要考虑迁移的具体步骤。可能的步骤包括项目结构分析、组件迁移、API调整、构建配置更新等。例如,用户可能需要将原有的.nvue文件转换为.vue文件,并检查组件用法是否符合Uniappx的要求。同时,需要注意Uniappx可能引入的新特性或废弃的API,这需要更新代码。 还需要注意潜在的问题,比如样式差异,因为nvue和标准Vue在样式支持上可能有不同。此外,原生模块的兼容性也需要检查,确保在Uniappx中仍然可用。用户可能还需要更新开发工具HBuilderX到最新版本,并调整CI/CD流程,如引用[2]提到的编译目录变化。 然后,我应该组织这些步骤,形成清晰的迁移指南,包括准备工作、逐步迁移、验证测试和部署调整。同时,提供示例代码片段,帮助用户理解如何修改组件注册和模板语法。可能还需要提醒用户处理特定问题,比如第三方库的兼容性或性能优化。 最后,生成相关问题,帮助用户进一步解决可能遇到的挑战,比如性能优化、样式兼容性、组件迁移具体案例等。确保回答结构清晰,步骤详细,并且参考了用户提供的引用内容,比如使用easycom自动注册组件和构建目录的变化。</think>### 从 UniApp nvue 迁移到 Uniappx 项目的分步指南 #### 1. **项目结构分析** - **移除 `.nvue` 文件**:Uniappx 项目使用标准 `.vue` 文件作为页面/组件,需将原有 `.nvue` 文件转换为 `.vue` 格式。 - **检查 `manifest.json` 配置**:确认 `"nvue"` 相关配置(如 `"nvueStyleCompiler"`)已移除,避免编译冲突[^1]。 #### 2. **组件迁移** - **利用 Easycom 自动注册**:Uniappx 支持自动注册组件(如示例中的 `uni-badge`),无需手动 `import` 和 `components` 声明[^1]。 ```vue <!-- 直接使用组件,无需手动注册 --> <template> <uni-badge text="abc" inverted></uni-badge> </template> ``` - **适配样式语法**:原生渲染的 `nvue` 样式支持有限,需检查 `.vue` 中是否使用了 `flex` 布局外的 CSS 特性(如 `position: fixed`)。 #### 3. **API 与生命周期调整** - **替换 `weex` 特有 API**:如 `dom.scrollToElement` 需改为 UniApp 通用 API `uni.pageScrollTo`。 - **统一生命周期**:`nvue` 中 `beforeCreate` 可能触发时机不同,需在 `.vue` 中验证逻辑。 #### 4. **构建配置更新** - **修改编译目录**:从 `unpackage/dist/build/h5` 调整为 `unpackage/dist/build/web`,同步更新部署流程[^2]。 - **调整本地预览路径**:若需 `file://` 协议打开,参考 Vue2 项目的相对路径配置。 #### 5. **验证与调试** - **真机差异测试**:重点验证原生组件(如 `video`、`map`)的渲染性能。 - **控制台日志检查**:使用 `uni.$log` 替代 `console.log` 确保多端输出一致。 --- ### 迁移示例:原生列表改造 **原 `nvue` 代码**(基于 Weex 原生渲染): ```vue <template> <list class="container"> <cell v-for="item in list" :key="item.id"> <text>{{ item.title }}</text> </cell> </list> </template> ``` **迁移后 `.vue` 代码**(使用 UniApp 通用标签): ```vue <template> <scroll-view class="container" scroll-y> <view v-for="item in list" :key="item.id"> <text>{{ item.title }}</text> </view> </scroll-view> </template> ``` ---
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值