百度地图 遇到了一个默明奇妙的bug..... 调用后中心点 不再 point(标注的点上...)这是需要执行一次(仅一次) 当 地图 加载完后 执行(这个方法你每次改地图 都会执行...所以让他执行一次......因为这个问题只会在 首次出现...)
//百度地图的 js ....
var userLocation = {la: 0, lg: 0}; //定义一个 变量.标志 方法只能执行一次...在执行完 百度 tilesloaded 后 将此变量设为 false...
var flag = true; //只能执行一次的方法.....
function initBaiduMap() {
if (userLocation.la === 0 || userLocation.lg === 0) {
//给保定的位置
userLocation.la = 38.87;
userLocation.lg = 115.47;
}
// 百度地图API功能
var map = new BMap.Map("allmap");
var point = new BMap.Point(userLocation.lg, userLocation.la);
map.centerAndZoom(point, 18);
var local = new BMap.LocalSearch(map, {
renderOptions: {map: map}
});
map.enableScrollWheelZoom(true);
map.addControl(new BMap.MapTypeControl({
mapTypes: [
BMAP_NORMAL_MAP,
BMAP_HYBRID_MAP
]
}));
//添加工具栏
var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角,添加比例尺
var top_left_navigation = new BMap.NavigationControl(); //左上角,添加默认缩放平移控件
var top_right_navigation = new BMap.NavigationControl({
anchor: BMAP_ANCHOR_TOP_RIGHT,
type: BMAP_NAVIGATION_CONTROL_SMALL
});
map.addControl(top_right_navigation); map.addEventListener('tilesloaded', function () {
//加载完成是有一个bug...该bug会导致..点偏移一次....这个方法只应该执行一次----
//默认检索...高阳县
// local.search("河北省保定市高阳县");
if (flag) {
map.clearOverlays();
var marker = new BMap.Marker(point);
map.addOverlay(marker);
map.centerAndZoom(marker, 1);
map.panTo(point, {noAnimation: true});
map.setCenter(point);
flag = false;
}
});
//绑定搜索事件....
$("#searchLocation").click(function () {
//清空所有标注....
map.clearOverlays();
local.search($("#detailLocation").val().trim());
});
//定义一个 marker 对象...
var mapLocation = {lg: 0, la: 0, label: {}};
map.addEventListener("click", function (e) {
//alert(e.point.lng + ", " + e.point.lat);
//清空所有标注....
map.clearOverlays();
//新建一个标注...
var marker = new BMap.Marker(e.point);
map.addOverlay(marker);
}); //绑定取消..
$('#cancelSelectMap').on('tap', function () {
$("#baiduMapWrapper").hide();
$("#form").show();
}); $("#confirmMapLocation").click(function () {
var markers = map.getOverlays();
for (var i = 0; i < markers.length; i++) {
console.log(typeof markers);
}
if (markers !== undefined) {
if (markers.length > 1) {
alert('标记大于一个...' + markers.length);
} else {
var point = markers[0].point;
mapLocation.lg = point.lng;
mapLocation.la = point.lat;
try {
mapLocation.infoWindow = markers[0].yb['content'];
var html = markers[0].yb['content'];
var marker = markers[0];
var address = $(html).find('tr:first').find('td:eq(1)').text();
vm.shop.address = address.trim();
vm.shop.map_location = mapLocation.lg + "," + mapLocation.la;
//$("#map_location").val(mapLocation.lg + ',' + mapLocation.la);
} catch (e) {
console.log(e);
vm.shop.map_location = mapLocation.lg + "," + mapLocation.la;
}
$("#baiduMapWrapper").hide(200);
$("#form").show(200);
}
}
});
}