1、安装插件
cordova plugin add cordova-plugin-geolocation
2、申请高德地图key
https://lbs.amap.com/
3、index.html界面引入高德地图js文件
<!-- 高德地图 -->
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.4.1&key=99f20bf360cfecd1eb94acfcb6819474"></script>
4、获取经纬度
declare let AMap;
public lPosition: any = "";
public rPosition: any = "";
public formattedAddress: string = "";
// 获取经纬度,转换地址。
getPositionFun() {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position);
console.log('纬度: ' + position.coords.latitude + '\n' +
'经度: ' + position.coords.longitude + '\n' +
'海拔: ' + position.coords.altitude + '\n' +
'水平精度: ' + position.coords.accuracy + '\n' +
'垂直精度: ' + position.coords.altitudeAccuracy + '\n' +
'方向: ' + position.coords.heading + '\n' +
'速度: ' + position.coords.speed + '\n' +
'时间戳: ' + position.timestamp + '\n');
console.log(AMap);
// 外面定义获取经纬度的变量,方便界面显示。
this.lPosition = position.coords.latitude;
this.rPosition = position.coords.longitude;
AMap.service("AMap.Geocoder", () => {
let geocoder = new AMap.Geocoder({
city: "010"
});
console.log(geocoder, "fuwu");
let positionInfo = [-position.coords.longitude + "", position.coords.latitude + ""];
console.log(positionInfo);
geocoder.getAddress(positionInfo, (status, result) => {
console.log(status, result, "转换定位信息");
if (status === 'complete' && result.info === 'OK') {
//获得了有效的地址信息:
console.log(result.regeocode.formattedAddress);
this.formattedAddress = result.regeocode.formattedAddress;
} else {
//获取地址失败
console.log("获取地址失败");
}
})
})
}, this.onError, { timeout: 30000 });
}
//定位数据获取失败响应
onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}