1,web 页面接入高德
<script src="https://webapi.amap.com/maps?v=1.4.0&key=自己的KEY"></script>
2.废话不说 来时撸代码
$(function () {
var map, geolocation;
//加载地图,调用浏览器定位服务
map = new AMap.Map('container', {
resizeEnable: true
});
map.plugin('AMap.Geolocation', function () {
geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默认:true
timeout: 100000, //超过100秒后停止定位,默认:无穷大
buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
buttonPosition: 'RB'
});
map.addControl(geolocation);
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
AMap.event.addListener(geolocation, 'error', onError); //返回定位出错信息
});
//解析定位结果成功时的回掉函数
function onComplete(data) {
console.log(data);
if (data) {
var city = data.addressComponent.city;
//此时获取城市信息成功获取天气
weather(city)
}
}
//解析定位失败时的回掉函数
function onError(data) {
console.log(data.message + "定位异常");
//定位异常 定位异常默认武汉市
weather("武汉市")
}
function weather(city) {
AMap.service('AMap.Weather', function () {
var weather = new AMap.Weather();
weather.getLive(city, function (err, data) {
var city = data.city, //定位城市
weather = data.weather, //天气
temperature = data.temperature + '℃'; // 温度
//此时获取天气信息成功
})
})
}
})