草庐IT

uniapp APP、H5和微信小程序 使用百度地图,H5动态加载百度地图sdk,cover-image图片不显示,标准基座模拟器地图不显示,表单校验字段[‘**‘]在数据库中不存在

Junmay66 2024-03-06 原文

APP里面的几个注意项

  1. 百度地图开放平台申请密匙,在manifest.json App模块配置的地图模块选择百度地图并填入申请到的appkey。
  2. 页面使用uniapp的map标签,要在地图上面覆盖图片、内容等,使用cover-image、cover-view,因为map是原生组件,覆盖的内容有时不显示,使用v-if控制(这里不能使用v-show),在onload里面设置延迟几百毫秒显示;百度地图在自定义基座和打包才能正常显示,标准基座不会显示;
  3. 使用uni.getLocation({})获取定位,type传gcj02,在自定义基座中,定位获取到的坐标不用转为百度就是正常,但是打包后需要转为百度marker才能准确标记;
<template>
	<map ref="allmap" class="allmap" :latitude="latitude" :longitude="longitude" :markers="markers" :scale="scale">
		<cover-image class="icon_img" v-if="showCoverImg" src="/static/image/map_search.png"></cover-image>
	</map>
</template>
<script>
	import { gcj02tobd09 } from '@/utils/index.js'
	export default {
		data() {
			return {
				scale: "16",
				latitude: '',
				longitude:'',
				markers: [],
			}
		},
		onLoad() {
			const self = this
			self.getUserLocation()
			setTimeout(()=>{
				self.showCoverImg = true
			}, 500)
		},
		methods: {
			getUserLocation(){
				const self = this
				uni.showToast({
					title: '正在获取用户定位...',
					icon: 'none'
				});
				uni.getLocation({
					type: 'gcj02',
					geocode: true,
					success: res => {
						uni.showToast({
							title: '定位成功',
							icon: 'none'
						});
						// gcj02要转成百度坐标, gcj02tobd09为事先定义好的经纬度转换方法
						let formatLonlat = gcj02tobd09(res.longitude, res.latitude)
						self.latitude = formatLonlat.latitude
						self.longitude = formatLonlat.longitude
						// 获取定位后可进行其他操作
					},
					fail: (fail) => {
						uni.showToast({
							title: '定位失败',
							icon: 'none'
						});
					},
					complete: () => {
						
					}
				})
			},
		}
	}
	
</script>
<style lang="scss" scoped>
	.allmap {
		width: 750rpx;
		width: 100%;
		height: calc(50vh - var(--status-bar-height));
	}
	.icon_img {
		position: absolute;
		right: 12rpx;
		width: 80rpx;
		height: 80rpx;
	}
</style>

H5里面的几个注意项

  1. H5也要在开放平台申请ak,与APP的不可通用;
  2. H5里面不使用原生组件map;
  3. 动态引入百度地图sdk,并使用地图加载成功后的回调函数进行其他操作,否则地图没加载成功就使用new BMap()等方法会报错;
<template>
	<view id="allmap"></view>
	<image class="icon_img" src="/static/image/map_search.png"></image>
</template>
<script>
	import { gcj02tobd09 } from '@/utils/index.js'
	export default {
		data() {
			return {
				latitude: '',
				longitude:'',
			}
		},
		onLoad() {
			loadBaiduMap()
			const self = this
			setTimeout(()=>{
				self.initialize()
				self.getUserLocation()
			},200)
		},
		methods: {
			loadBaiduMap() {
			 	// 移动端H5使用v3.0版本比较好,h5的ak是申请的web平台的ak与app的ak是不一样的,initialize为地图加载成功的回调
				var script = document.createElement('script');
				script.src = "https://api.map.baidu.com/api?v=3.0&ak=百度AK&callback=initialize";
				document.body.appendChild(script);
			},
			initialize(){
				var map = new BMap.Map("allmap");
				map.centerAndZoom(new BMap.Point(this.longitude, this.latitude), 14);  // 初始化地图,设置中心点坐标和地图级别
				// 自定义marker图标	
				const myIcon = new BMap.Icon('static/map/marker.png', new BMap.Size(32, 32));
				let marker = new BMap.Marker(new BMap.Point(this.longitude, this.latitude),{
					icon: myIcon
				});
				map.addOverlay(marker)
			},
			getUserLocation(){
				// 这里可以使用uni.getLocation()定位,也可以使用百度地图里面的获取定位,使用百度地图里面的获取定位不需要进行经纬度转换
				var map = new BMap.Map("allmap");
				map.centerAndZoom(new BMap.Point(this.longitude, this.latitude), 14);
				uni.showLoading({
					title: '定位中...',
					mask: true,
				})
				const self = this
				var geolocation = new BMap.Geolocation()
				geolocation.getCurrentPosition(function(res){
					if(this.getStatus() == BMAP_STATUS_SUCCESS){
						self.getLocationSuccess = true
						uni.hideLoading()
						uni.showToast({
							title: '定位成功',
							icon: 'none',
						});
						map.setCenter(res.point)
						self.longitude = res.longitude
						self.latitude = res.latitude
					
						const myIcon = new BMap.Icon('static/map/marker.png', new BMap.Size(32, 32));
						let marker = new BMap.Marker(new BMap.Point(self.searchQuery.lon, self.searchQuery.lat),{
							icon: myIcon
						});
						map.addOverlay(marker)
						map.panTo(res.point) // 平滑移动
						// 获取定位成功后进行其他操作
					}else{
						uni.showToast({
							title: '定位失败,请稍后重试!',
							icon: 'none',
						});
					}
				})
			},
		}
	}
	
</script>
<style lang="scss" scoped>
	#allmap {
		width: 750rpx;
		width: 100%;
		height: calc(50vh - var(--status-bar-height));
	}
	.icon_img {
		position: absolute;
		right: 12rpx;
		width: 80rpx;
		height: 80rpx;
	}
</style>

微信小程序注意项

  1. 虽然百度地图有微信小程序的api,但并没有用,因为小程序根本不能直接使用百度地图,只能采用web-view方式引入外链嵌入,其实uniapp H5的百度地图实现了,将H5地图页面地址赋值web-view的src就可以,在小程序里面配置业务域名即可;
<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		// webUrl是H5页面的地图
		<web-view :src="webUrl"></web-view>
		<!-- #endif -->
		<!-- #ifndef MP-WEIXIN -->
		<map ref="allmap" class="allmap" :latitude="latitude" :longitude="longitude" :markers="markers" :scale="scale">
			<cover-image class="icon_img" v-if="showCoverImg" src="/static/image/map_search.png"></cover-image>
		</map>
		<!-- #endif -->
	</view>
</template>

uni-forms校验出现字段在数据库中不存在的错误的可能原因是:

  1. uni-forms-item 加了name 属性,但是在rules中没有设置规则,即使是非必填,只要加了 name 属性,就必须在 rules 中设置规则,否则报错 “字段在数据库中不存在”;
  2. 使用了 validateFunction 时,必须在 onReady 生命周期调用组件的 setRules 方法绑定验证规则:this.$refs.form.setRules(this.rules),或者在 form 显示后立即调用 setRules (比如 form 在弹窗里面时弹窗显示后,tab 选项卡切换后)。

有关uniapp APP、H5和微信小程序 使用百度地图,H5动态加载百度地图sdk,cover-image图片不显示,标准基座模拟器地图不显示,表单校验字段[‘**‘]在数据库中不存在的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 解析 RDFa、微数据等的最佳方式是什么,使用统一的模式/词汇(例如 schema.org)存储和显示信息 - 2

    我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i

  3. ruby - 如何在续集中重新加载表模式? - 2

    鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende

  4. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  5. ruby-on-rails - link_to 不显示任何 rails - 2

    我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article

  6. ruby - RuntimeError(自动加载常量 Apps 多线程时检测到循环依赖 - 2

    我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("

  7. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  8. ruby-on-rails - 使用 config.threadsafe 时从 lib/加载模块/类的正确方法是什么!选项? - 2

    我一直致力于让我们的Rails2.3.8应用程序在JRuby下正确运行。一切正常,直到我启用config.threadsafe!以实现JRuby提供的并发性。这导致lib/中的模块和类不再自动加载。使用config.threadsafe!启用:$rubyscript/runner-eproduction'pSim::Sim200Provisioner'/Users/amchale/.rvm/gems/jruby-1.5.1@web-services/gems/activesupport-2.3.8/lib/active_support/dependencies.rb:105:in`co

  9. ruby-on-rails - 复数 for fields_for has_many 关联未显示在 View 中 - 2

    目前,Itembelongs_toCompany和has_manyItemVariants。我正在尝试使用嵌套的fields_for通过Item表单添加ItemVariant字段,但是使用:item_variants不显示该表单。只有当我使用单数时才会显示。我检查了我的关联,它们似乎是正确的,这可能与嵌套在公司下的项目有关,还是我遗漏了其他东西?提前致谢。注意:下面的代码片段中省略了不相关的代码。编辑:不知道这是否相关,但我正在使用CanCan进行身份验证。routes.rbresources:companiesdoresources:itemsenditem.rbclassItemi

  10. ruby-on-rails - 从应用程序中自定义文件夹内的命名空间自动加载 - 2

    我们目前正在为ROR3.2开发自定义cms引擎。在这个过程中,我们希望成为我们的rails应用程序中的一等公民的几个类类型起源,这意味着它们应该驻留在应用程序的app文件夹下,它是插件。目前我们有以下类型:数据源数据类型查看我在app文件夹下创建了多个目录来保存这些:应用/数据源应用/数据类型应用/View更多类型将随之而来,我有点担心应用程序文件夹被这么多目录污染。因此,我想将它们移动到一个子目录/模块中,该子目录/模块包含cms定义的所有类型。所有类都应位于MyCms命名空间内,目录布局应如下所示:应用程序/my_cms/data_source应用程序/my_cms/data_ty

随机推荐