百度地圖 api bug 解決.......
阿新 • • 發佈:2018-04-17
默認 rim content ext hide 定義 zoom ESS find
百度地圖 遇到了一個默明奇妙的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); } } }); }
百度地圖 api bug 解決.......