微信小程序根据经纬度导航,打开地图软件

586 阅读1分钟
   <view class="main">
   	<view class="map">
   		<map :style="{height:contentList.content.height*2+'rpx'}" :scale='16' id='myMap' ref="myMap" :markers="markers" :latitude="latitude" :longitude="longitude" show-location  @markertap="markertap" v-if="hanmarkers"></map>
   		<view class="bottom_sty1">
   			<view class="address_sty1">{{address}}</view>
   			<view class="navigation_sty1" @click="address_nav">导航</view>
   		</view>
   	</view>
   </view>
   <script>
   export default {
   	props: {
   		contentList: {
   			type: Object,
   			default: {}
   		}
   	},
           data() {
   		return {
   			latitude: 22.578975,
   			longitude: 114.120253,
   			address:'深圳市罗湖区富基大厦',
   			markers: [{
   				latitude: 22.578975,
   				longitude: 114.120253,
   				id:1,
   				width: 20, // 浮标宽度
   				height: 30, // 浮标高度
   			}],
   			hanmarkers:false
   		}
   	},
       		methods: {
   		markertap(e){
   			console.log('点击标记',e)
   		},
   		address_nav(){
   			console.log(this.address,this.latitude,this.latitude)
   			if(this.contentList.content.navigation){
   				uni.openLocation({
   					address:this.address,
   					latitude:this.latitude,
   					longitude:this.longitude,
   					scale: 18,
   				})
   			}else{
   				uni.showToast({
   					title:'未开通权限',
   					icon:'none'
   				})
   			}
   		
   		},
   	},
   	mounted() {
   		this.mapCtx = uni.createMapContext('myMap')
   		setTimeout(()=>{
   			//后台的经纬度传反了
   			this.address = this.contentList.content.address
   			this.latitude =  parseFloat(this.contentList.content.longitude)
   			this.longitude = parseFloat(this.contentList.content.latitude)
   			this.markers[0].latitude =this.contentList.content.longitude
   			this.markers[0].longitude =  this.contentList.content.latitude
   			this.hanmarkers = true
   			console.log('这里',this.hanmarkers)
   		},1000)
   	},
     </script>