Dec
03
2021
高德地图获取当前地理详细位置信息
作者:
绝缘体.. 发布:
2021-12-03 12:09 分类:
未分类 阅读:
抢沙发
调用高德地图api获取当前地理位置的详细信息。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<title>浏览器精确定位</title>
<body>
<p id="result1"></p>
<script type="text/javascript"
src="https://webapi.amap.com/maps?v=1.4.15&key=1d94daed62ea1f37371f44bc151224b2&plugin=AMap.Geocoder"></script>
<script type="text/javascript">
AMap.plugin('AMap.Geolocation', function () {
var geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默认:true
timeout: 10000, //超过10秒后停止定位,默认:5s
useNative: true
});
geolocation.getCurrentPosition(function (status, result1) {
if (status == 'complete') {alert(JSON.stringify(result1));
var geocoder = new AMap.Geocoder({radius: 500}),
value = result1.position.lng + ',' + result1.position.lat;
geocoder.getAddress(value.split(','), function (status, result) {
if (status === 'complete' && result.regeocode) {
document.getElementById('result1').innerHTML = result.regeocode.formattedAddress;
//加载天气查询插件
AMap.plugin('AMap.Weather', function() {
//创建天气查询实例
var weather = new AMap.Weather();alert(result1.addressComponent.adcode);
//执行实时天气信息查询
weather.getLive(result1.addressComponent.adcode, function(err, data) {
console.log(err, data);
alert(data.weather);
});
});
} else {
alert('根据经纬度查询地址失败')
}
});
} else {
alert('定位失败,原因:' + result.message);
}
});
});
</script>
</body>
</html>
微信扫一扫,打赏作者吧~