<template>
	<view class="content">
		<map id="myMap" style="width: 100%; height: 780px;" :latitude="latitude" :longitude="longitude"
			:markers="markers" show-location="true" :scale="18">
			<cover-image class="returnBack" src="/static/login/returnBack.png" @click="goBack()"></cover-image>
			<cover-image class="start-button" src="/static/sport/start1.png" @click="start"></cover-image>
		</map>
	</view>
</template>

<script>
	var self, mapCtx
	export default {
		onLoad(option) {
			this.type = option.type
			this.checkOpenGPSServiceByAndroid()
			if (this.location) {
				this.getLocationInfo()
			}
		},
		data() {
			return {
				type: '',
				location: null,
				latitude: 23.099994,
				longitude: 113.324520,
				markers: []
			}
		},
		methods: {
			goBack() {
				uni.reLaunch({
					url: '/pages/sport/main',
					animationType: 'pop-in',
					animationDuration: 300
				})
			},
			start() {
				this.checkOpenGPSServiceByAndroid()
				if (this.location) {
					if (this.type === '跑步') {
						uni.reLaunch({
							url: '/pages/sport/start?type=跑步',
							animationType: 'pop-in',
							animationDuration: 300
						})
					} else {
						uni.reLaunch({
							url: '/pages/sport/start?type=健走',
							animationType: 'pop-in',
							animationDuration: 300
						})
					}
				}
			},
			// 位置授权
			checkOpenGPSServiceByAndroid() {
				this.location = false;
				let system = uni.getSystemInfoSync(); // 获取系统信息
				if (system.platform === 'android') {
					var context = plus.android.importClass('android.content.Context');
					var locationManager = plus.android.importClass('android.location.LocationManager');
					var main = plus.android.runtimeMainActivity();
					var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
					if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
						uni.showModal({
							title: '提示',
							content: '请打开系统定位功能',
							showCancel: false, // 不显示取消按钮
							success() {
								if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
									var Intent = plus.android.importClass('android.content.Intent');
									var Settings = plus.android.importClass('android.provider.Settings');
									var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
									main.startActivity(intent); // 打开系统设置GPS服务页面
								} else {
									console.log('GPS功能已开启');
								}
							}
						});
					} else {
						this.location = true
					}
				}
			},
			// 获取地理位置
			getLocationInfo() {
				let that = this;
				uni.getLocation({
					type: 'gcj02',
					cacheTimeout: 3001,
					isHighAccuracy: true,
					highAccuracyExpireTime: 3001,
					success: function(res) {
						console.log(res, "当前位置");
						that.latitude = res.latitude
						that.longitude = res.longitude
						//标记点
						that.markers = [{
							id: 1,
							latitude: res.latitude,
							longitude: res.longitude,
							iconPath: '/static/sport/marker1.png'
						}]
					}
				});
			}
		}
	}
</script>

<style>
	.returnBack {
		height: 55rpx;
		width: 55rpx;
		margin-top: 50rpx;
		margin-left: 25rpx;
		margin-right: auto;
	}

	.start-button {
		display: flex;
		justify-content: center;
		width: 300rpx;
		height: 300rpx;
		margin: 0 auto;
		margin-top: 120%;
	}
</style>