baidu map api note
地圖級別控制
// 百度地圖API功能
var map = new BMap.Map("allmap", {enableMapClick:false});//構造底圖時,關閉底圖可點功能
// 初始化地圖,設置中心點座標(城市名)和地圖級別
map.centerAndZoom("上海",15);
map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);
// 移動地圖到中心點
map.panTo(new BMap.Point(113.262232,23.154345));
// 設置縮放級別
map.setZoom(14);
// 開啓地圖縮放
map.enableScrollWheelZoom();
// 能夠拖拽
marker.enableDragging();
// 不能夠拖拽
marker.disableDragging();
獲取兩點之間距離
var pointA = new BMap.Point(106.486654,29.490295); // 建立點座標A--大渡口區
var pointB = new BMap.Point(106.581515,29.615467); // 建立點座標B--江北區
alert('從大渡口區到江北區的距離是:'+(map.getDistance(pointA,pointB)).toFixed(2)+' 米。'); //獲取兩點距離
添加 圖形,點,線,面 文字
// marker是隨着地圖縮放級別,尺寸變化的
var marker = new BMap.Marker(new BMap.Point(116.404, 39.915)); // 建立點
// 折線
var polyline = new BMap.Polyline([
new BMap.Point(116.399, 39.910),
new BMap.Point(116.405, 39.920),
new BMap.Point(116.425, 39.900)
], {strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.5}); //建立折線
// 建立圓
var circle = new BMap.Circle(
point, 1500,
{strokeColor: "blue",
fillColor: "#ccc",
strokeWeight: 2,
strokeOpacity: 0.5,
fillOpacity: 0.7}
);
// 多邊形
var polygon = new BMap.Polygon([
new BMap.Point(116.387112, 39.920977),
new BMap.Point(116.385243, 39.913063),
new BMap.Point(116.394226, 39.917988),
new BMap.Point(116.401772, 39.921364),
new BMap.Point(116.41248, 39.927893)
], {strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.5});
//建立矩形
var pStart = new BMap.Point(116.392214, 39.918985);
var pEnd = new BMap.Point(116.41478, 39.911901);
var rectangle = new BMap.Polygon([
new BMap.Point(pStart.lng, pStart.lat),
new BMap.Point(pEnd.lng, pStart.lat),
new BMap.Point(pEnd.lng, pEnd.lat),
new BMap.Point(pStart.lng, pEnd.lat)
], {strokeColor: "blue", strokeWeight: 2, strokeOpacity: 0.5});
// 開啓(關閉)點線面的編輯(能夠拖拽,移動點)
polygon.enableEditing()
polygon.disableEditing()
// 文字label
var opts = {
position : new BMap.Point(116.417854,39.921988), // 指定文本標註所在的地理位置
offset : new BMap.Size(30, -30) //設置文本偏移量
}
var label = new BMap.Label("歡迎使用百度地圖,這是一個簡單的文本標註哦~", opts); // 建立文本標註對象
label.setStyle({
color : "red",
fontSize : "12px",
height : "20px",
lineHeight : "20px",
fontFamily:"微軟雅黑"
});
// 覆蓋物顯示,隱藏
marker.show()
marker.hide()
// 添加覆蓋物
map.addOverlay(marker);
// 清除覆蓋物
map.clearOverlays();
刪除指定覆蓋物
getOverlays方法能夠獲取 覆蓋物數組,getLabel方法能夠,獲取他的label
function deletePoint(){
var allOverlay = map.getOverlays();
for (var i = 0; i < allOverlay.length -1; i++){
if(allOverlay[i].getLabel().content == "我是id=1"){
map.removeOverlay(allOverlay[i]);
return false;
}
}
}
marker 自定義icon
// 建立小狐狸icon
var pt = new BMap.Point(116.417, 39.909);
var myIcon = new BMap.Icon("http://lbsyun.baidu.com/jsdemo/img/fox.gif", new BMap.Size(300,157));
var marker2 = new BMap.Marker(pt,{icon:myIcon}); // 建立標註
map.addOverlay(marker2);
加載海量點
// data source
var data = {"data":[[74.438,39.006,1]]}
if (document.createElement('canvas').getContext) { // 判斷當前瀏覽器是否支持繪製海量點
var points = []; // 添加海量點數據
for (var i = 0; i < data.data.length; i++) {
points.push(new BMap.Point(data.data[i][0], data.data[i][1]));
}
var options = {
size: BMAP_POINT_SIZE_SMALL,
shape: BMAP_POINT_SHAPE_STAR,
color: '#d340c3'
}
var pointCollection = new BMap.PointCollection(points, options); // 初始化PointCollection
pointCollection.addEventListener('click', function (e) {
alert('單擊點的座標爲:' + e.point.lng + ',' + e.point.lat); // 監聽點擊事件
});
map.addOverlay(pointCollection); // 添加Overlay
} else {
alert('請在chrome、safari、IE8+以上瀏覽器查看本示例');
}
兩點之間 畫弧線 圓弧
// 引入一個庫
<script type="text/javascript" src="http://api.map.baidu.com/library/CurveLine/1.5/src/CurveLine.min.js"></script>
var points = []
points.push( new BMap.Point(116.432045,39.910683) )
points.push( new BMap.Point(120.129721,30.314429) )
points.push( new BMap.Point(121.491121,25.127053) )
var curve = new BMapLib.CurveLine(points, {strokeColor:"blue", strokeWeight:3, strokeOpacity:0.5}); //建立弧線對象
map.addOverlay(curve); //添加到地圖中
curve.enableEditing(); //開啓編輯功能
// centre:橢圓中心點,X:橫向經度,Y:縱向緯度
function add_oval(centre,x,y){
var assemble= []
var angle;
var dot;
var tangent=x/y;
for(i=0;i<36;i++)
{
angle = (2* Math.PI / 36) * i;
dot = new BMap.Point(centre.lng+Math.sin(angle)*y*tangent, centre.lat+Math.cos(angle)*y);
assemble.push(dot);
}
return assemble;
}
var oval = new BMap.Polygon(add_oval(point,0.1,0.3), {strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});
map.addOverlay(oval);
畫行政區
function getBoundary(){
var bdary = new BMap.Boundary();
console.log(bdary)
bdary.get("上海市徐彙區", function(rs){ //獲取行政區域
map.clearOverlays(); //清除地圖覆蓋物
// rs.boundaries 是一個數組,每一個元素,是地理位置的字符串 "12,12;123,23"
var count = rs.boundaries.length;
if (count === 0) {
alert('未能獲取當前輸入行政區域');
return ;
}
console.log(rs.boundaries)
var pointArray = [];
for (var i = 0; i < count; i++) {
var ply = new BMap.Polygon(rs.boundaries[i],
{
strokeWeight: 2,
strokeColor: "#ff0000",
fillColor: 'blue'
}
); //創建多邊形覆蓋物
map.addOverlay(ply); //添加覆蓋物
pointArray = pointArray.concat(ply.getPath());
console.log(pointArray)
}
map.setViewport(pointArray); //調整視野
addlabel();
});
}
自定義覆蓋物
// 複雜的自定義覆蓋物
function ComplexCustomOverlay(point, text, mouseoverText){
this._point = point;
this._text = text;
this._overText = mouseoverText;
}
ComplexCustomOverlay.prototype = new BMap.Overlay();
ComplexCustomOverlay.prototype.initialize = function(map){
this._map = map;
var div = this._div = document.createElement("div");
div.style.position = "absolute";
div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
div.style.backgroundColor = "#EE5D5B";
div.style.border = "1px solid #BC3B3A";
div.style.color = "white";
div.style.height = "18px";
div.style.padding = "2px";
div.style.lineHeight = "18px";
div.style.whiteSpace = "nowrap";
div.style.MozUserSelect = "none";
div.style.fontSize = "12px"
var span = this._span = document.createElement("span");
div.appendChild(span);
span.appendChild(document.createTextNode(this._text));
var that = this;
var arrow = this._arrow = document.createElement("div");
arrow.style.background = "url(http://map.baidu.com/fwmap/upload/r/map/fwmap/static/house/images/label.png) no-repeat";
arrow.style.position = "absolute";
arrow.style.width = "11px";
arrow.style.height = "10px";
arrow.style.top = "22px";
arrow.style.left = "10px";
arrow.style.overflow = "hidden";
div.appendChild(arrow);
div.onmouseover = function(){
this.style.backgroundColor = "#6BADCA";
this.style.borderColor = "#0000ff";
this.getElementsByTagName("span")[0].innerHTML = that._overText;
arrow.style.backgroundPosition = "0px -20px";
}
div.onmouseout = function(){
this.style.backgroundColor = "#EE5D5B";
this.style.borderColor = "#BC3B3A";
this.getElementsByTagName("span")[0].innerHTML = that._text;
arrow.style.backgroundPosition = "0px 0px";
}
mp.getPanes().labelPane.appendChild(div);
return div;
}
ComplexCustomOverlay.prototype.draw = function(){
var map = this._map;
var pixel = map.pointToOverlayPixel(this._point);
this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
this._div.style.top = pixel.y - 30 + "px";
}
var txt = "銀湖海岸城", mouseoverTxt = txt + " " + parseInt(Math.random() * 1000,10) + "套" ;
var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(116.407845,39.914101), "銀湖海岸城",mouseoverTxt);
mp.addOverlay(myCompOverlay);
點聚合(放大縮小)
<script type="text/javascript" src="http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js"></script>
// markers 是一個marker數組
var markerClusterer = new BMapLib.MarkerClusterer(map, {markers:markers});
矢量圖 圖標
var vectorFCArrow = new BMap.Marker(new BMap.Point(point.lng-0.01,point.lat), {
// 初始化方向向上的閉合箭頭
icon: new BMap.Symbol(BMap_Symbol_SHAPE_FORWARD_CLOSED_ARROW, {
scale: 5,
strokeWeight: 1,
rotation: 0,//順時針旋轉30度
fillColor: 'red',
fillOpacity: 0.8
})
});
var vectorStar = new BMap.Marker(new BMap.Point(point.lng+0.03,point.lat-0.03), {
// 初始化五角星symbol
icon: new BMap.Symbol(BMap_Symbol_SHAPE_STAR, {
scale: 5,
fillColor: "pink",
fillOpacity: 0.8
})
});
信息彈窗
var infoWindow = new BMap.InfoWindow("地址:北京市東城區王府井大街88號樂天銀泰百貨八層", {
width : 200, // 信息窗口寬度
height: 100, // 信息窗口高度
title : "海底撈王府井店" , // 信息窗口標題
enableMessage:true,//設置容許信息窗發送短息
message:"親耐滴,晚上一塊兒吃個飯吧?戳下面的連接看下地址喔~"
});
marker.addEventListener("click", function(){
map.openInfoWindow(infoWindow,point); //開啓信息窗口
});
鼠標右鍵
除了地圖,marker也支持加右鍵
var menu = new BMap.ContextMenu();
var txtMenuItem = [
{
text:'放大',
callback:function(){map.zoomIn()}
},
{
text:'縮小',
callback:function(){map.zoomOut()}
}
];
for(var i=0; i < txtMenuItem.length; i++){
menu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,100));
}
map.addContextMenu(menu);
鼠標測距
// 引入插件
<script type="text/javascript" src="http://api.map.baidu.com/library/DistanceTool/1.2/src/DistanceTool_min.js"></script>
var myDis = new BMapLib.DistanceTool(map);
map.addEventListener("load",function(){
myDis.open(); //開啓鼠標測距
//myDis.close(); //關閉鼠標測距大
});
// 獲取點擊點座標
map.addEventListener("click",function(e){
alert(e.point.lng + "," + e.point.lat);
});