uniapp 定位 android ios
<view class="cen-style">
<view class="cens-title">详细地址:</view>
<view class="fbc">
<view style="width: 400rpx;"><u-input v-model="address" type="text" :border="false" placeholder="街道,楼牌号等" /></view>
<u-icon name="map" size="40" @click="getaddress"></u-icon>
</view>
</view>
checkOpenGPSService() {
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);
} else {
console.log('GPS功能已开启');
}
}
});
}
} else if (system.platform === 'ios') {
var cllocationManger = plus.ios.import('CLLocationManager');
var enable = cllocationManger.locationServicesEnabled();
var status = cllocationManger.authorizationStatus();
plus.ios.deleteObject(cllocationManger);
if (enable && status != 2) {
console.log('手机系统的定位已经打开');
} else {
console.log('手机系统的定位没有打开');
uni.showModal({
title: '提示',
content: '请打开定位服务功能',
showCancel: false,
success() {
var UIApplication = plus.ios.import('UIApplication');
var application2 = UIApplication.sharedApplication();
var NSURL2 = plus.ios.import('NSURL');
var setting2 = NSURL2.URLWithString('App-Prefs:root=Privacy&path=LOCATION');
application2.openURL(setting2);
plus.ios.deleteObject(setting2);
plus.ios.deleteObject(NSURL2);
plus.ios.deleteObject(application2);
}
});
}
}
},
getaddress() {
this.checkOpenGPSService();
let that = this;
uni.getLocation({
type: 'wgs84',
geocode: true,
success: function(res) {
that.province = res.address.province;
that.city = res.address.city;
that.area = res.address.district;
that.address = res.address.province + res.address.city + res.address.district + res.address.street + res.address.streetNum + res.address.poiName;
console.log(res.address.city);
}
});
},
<view class="pad">
<map style="width: 100%; height: 300rpx;" :latitude="latitude" :longitude="longitude" :markers="covers"></map>
<view class="ipnand" @click="gotoLocation">
<view class="fc">
<u-icon name="map-fill" color="#fff" size="28"></u-icon>
<text class="ipbxassa ellipsis1">{{ covers[0].title }}</text>
</view>
<u-icon name="arrow-right" color="#fff" size="28"></u-icon>
</view>
</view>
<script>
gotoLocation() {
this.$nextTick(() => {
uni.openLocation({
latitude: Number(this.covers[0].latitude), //要去的纬度-地址
longitude: Number(this.covers[0].longitude), //要去的经度-地址
name: this.covers[0].title, //地址名称
address: this.covers[0].title, //详细地址名称
success: function() {
console.log('导航成功');
},
fail: function(error) {
console.log(error);
}
});
});
}
</script>