Procházet zdrojové kódy

使用H5API获取定位信息

gcz před 3 roky
rodič
revize
97ccbce954
3 změnil soubory, kde provedl 66 přidání a 32 odebrání
  1. 2 2
      common/config.js
  2. 28 14
      pages/index/index.vue
  3. 36 16
      pages/parkingLists/parkingLists.vue

+ 2 - 2
common/config.js

@@ -2,8 +2,8 @@ const config = {
 	wxAppid:'wxbe90cc7c5233dd84',// 测试wxAppid 
 	// wxAppid: 'wx45c3cf2b632f5fd5', // 正式wxAppid
 	
-	baseUrl:'http://wx.hw.hongweisoft.com/parking/client',// 64服务器
-	// baseUrl:'http://parking.pdzhtc.com/client', //正式接口访问地址
+	baseUrl:'https://wx.hw.hongweisoft.com/parking/client',// 64服务器
+	// baseUrl:'https://parking.pdzhtc.com/client', //正式接口访问地址
 }
 export {
 	config

+ 28 - 14
pages/index/index.vue

@@ -218,21 +218,35 @@
 		methods: {
 			// 定位
 			getLocation(){
-				const that = this
-				uni.getLocation({
-					type: 'gcj02',
-					geocode: true,
-					altitude: true,
-					success: (res) => {
-						that.latLongItude = {latitude: res.latitude,longitude:res.longitude};
-						that.latitude = res.latitude;
-						that.longitude = res.longitude;
+				const that = this;
+				if(navigator.geolocation){
+				//判断是否有这个对象
+					navigator.geolocation.getCurrentPosition(function(pos){
+						console.log("经度:"+pos.coords.longitude+"纬度:"+pos.coords.latitude);
+						// alert(latitude)
+						// alert(longitude)
+						that.latLongItude = {latitude: pos.coords.latitude,longitude:pos.coords.longitude};
+						that.latitude = pos.coords.latitude;
+						that.longitude = pos.coords.longitude;
 						that.getCityNameByLonLat(that.latLongItude)
-					},
-					fail: () => {
-						console.log("获取经纬度失败");
-					}
-				})
+					})
+				}else{
+					console.log("当前系统不支持GPS API")
+				};
+				// uni.getLocation({
+				// 	type: 'gcj02',
+				// 	geocode: true,
+				// 	altitude: true,
+				// 	success: (res) => {
+				// 		that.latLongItude = {latitude: res.latitude,longitude:res.longitude};
+				// 		that.latitude = res.latitude;
+				// 		that.longitude = res.longitude;
+				// 		that.getCityNameByLonLat(that.latLongItude)
+				// 	},
+				// 	fail: () => {
+				// 		console.log("获取经纬度失败");
+				// 	}
+				// })
 			},
 			// 通过经纬度获取地区详细信息
 			getCityNameByLonLat({longitude,latitude} = {}){

+ 36 - 16
pages/parkingLists/parkingLists.vue

@@ -181,24 +181,44 @@
 				let that = this;
 				console.log("请求定位")
 				that.loading = true
-				uni.getLocation({
-					type:"gcj02",
-					success : function (res) {
-						console.log("定位返回信息:", res);
-						that.latitude = res.latitude;
-						that.longitude = res.longitude;
-						that.currentPosition.latitude = res.latitude
-						that.currentPosition.longitude = res.longitude
+				
+				if(navigator.geolocation){
+				//判断是否有这个对象
+					navigator.geolocation.getCurrentPosition(function(pos){
+						console.log("经度:"+pos.coords.longitude+"纬度:"+pos.coords.latitude);
+						that.latitude = pos.coords.latitude;
+						that.longitude = pos.coords.longitude;
+						that.currentPosition.latitude = pos.coords.latitude
+						that.currentPosition.longitude = pos.coords.longitude
 						that.loading = false
 						that.getNearRoadsl()
-					},
-					fail: function(res){
-						this.$refs.uToast.show({
-							title: res,
-							type: 'error',
-						})
-					}
-				})
+					})
+				}else{
+					this.$refs.uToast.show({
+						title: "当前系统不支持GPS API",
+						type: 'error',
+					})
+				};
+				
+				// uni.getLocation({
+				// 	type:"gcj02",
+				// 	success : function (res) {
+				// 		console.log("定位返回信息:", res);
+				// 		that.latitude = res.latitude;
+				// 		that.longitude = res.longitude;
+				// 		that.currentPosition.latitude = res.latitude
+				// 		that.currentPosition.longitude = res.longitude
+				// 		that.loading = false
+				// 		that.getNearRoadsl()
+				// 	},
+				// 	fail: function(res){
+				// 		this.$refs.uToast.show({
+				// 			title: res,
+				// 			type: 'error',
+				// 		})
+				// 	}
+				// })
+				
 			},
 			/**
 			  * 导航