标签:alert local fail console show res git address tom
<template>
<baidu-map class="map" :zoom="zoom" :center="center" @ready="handler">
<bm-geolocation
anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
:showAddressBar="true"
:autoLocation="true"
></bm-geolocation>
</baidu-map>
</template>
//js代码//--------------------------------------//
data() {
return {
center: { lng: 0, lat: 0 },
zoom:15//必须写上,自己因为忘记写一直无法自动定位
};
},
/*-------------------------地图插件准备好后触发事件---------------------------------------*/
handler({ BMap, map }) {
const that = this;
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r) {
if (this.getStatus() == BMAP_STATUS_SUCCESS) {
that.center.lng = r.longitude;
that.center.lat = r.latitude;
console.log(that.center);
} else {
alert("failed" + this.getStatus());
}
});
}
<script type="text/javascript">
var map = new BMap.Map("allmap");//创建Map实例,注意页面中一定要有个id为allmp的div
var point = new BMap.Point(116.331398,39.897445);//创建定坐标
map.centerAndZoom(point,12);//// 初始化地图,设置中心点坐标和地图级别
var geolocation = new BMap.Geolocation();
var gc = new BMap.Geocoder();//创建地理编码器
// 开启SDK辅助定位
geolocation.enableSDKLocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
var mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
alert('您的位置:' + r.point.lng + ',' + r.point.lat);
var pt = r.point;
map.panTo(pt);//移动地图中心点
//alert(r.point.lng);//X轴
//alert(r.point.lat);//Y轴
gc.getLocation(pt, function(rs){
var addComp = rs.addressComponents;
//alert(addComp.city);
alert(addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber);
});
}
else {
alert('failed'+this.getStatus());
}
});
</script>
四、通过浏览器定位获取当前经纬度:
<script type="text/javascript">
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445);
map.centerAndZoom(point,12);
var geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() == BMAP_STATUS_SUCCESS){
var mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
alert('您的位置:'+r.point.lng+','+r.point.lat);
}
else {
alert('failed'+this.getStatus());
}
});
</script>
五、ip定位获取当前所在城市
<script type="text/javasript">
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445);
map.centerAndZoom(point,12);
function myFun(result){
var cityName = result.name;
map.setCenter(cityName);
alert("当前定位城市:"+cityName);
}
var myCity = new BMap.LocalCity();
myCity.get(myFun);
</script>
标签:alert local fail console show res git address tom
原文地址:https://www.cnblogs.com/shiazhen/p/12167962.html