百度地图
2015年8月13日
地图功能:显示、缩放、平移。比例尺、工具条、鹰眼等工具条。绘制点、线、面等元素。图层控制。路径规划等功能。
参见:..\javascript.docx插件部分。
注意:百度的文档虽然没有下载版本,但是比高德的强10000多倍,条理,清晰,实用。
示例:
<!DOCTYPEhtml>
<html>
<head>
<title>BaiduMapDemo</title>
<style type="text/css">
html{height:100%}
body{height:100%;margin:0px;padding:0px}
#map{height:100%}
</style>
<scripttype="text/javascript"src="jquery.min.js"></script>
<scripttype="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=eQv0G7tIacmmSYWmkgj1gWoz">
</script>
</head>
<body>
<divid="map"></div>
<scripttype="text/javascript">
$(document).ready(function(){
var map = new BMap.Map("map");
var point = new BMap.Point(116.404,39.915);
map.centerAndZoom(point, 15);
});
</script>
</body>
</html>
addTileLayer(),removeTileLayer(),getTileLayer()。
包括各种交互控件,鼠标配置,绘制方法等。
处理地图状态,如平移、缩放等。
缩放:map.enableScrollWheelZoom(),默认关闭鼠标缩放。
平移:map. enableDragging()。默认的鼠标状态是就是平移。
示例:支持缩放和平移的地图。
<!DOCTYPEhtml>
<html>
<head>
<title>BaiduMapDemo</title>
<style type="text/css">
html{height:100%}
body{height:100%;margin:0px;padding:0px}
#map{height:100%}
</style>
<scripttype="text/javascript" src="jquery.min.js"></script>
<scripttype="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=eQv0G7tIacmmSYWmkgj1gWoz">
</script>
</head>
<body>
<divid="map"></div>
<scripttype="text/javascript">
$(document).ready(function(){
var map = new BMap.Map("map");
map.enableScrollWheelZoom();
var point = new BMap.Point(116.404,39.915);
map.centerAndZoom(point, 15);
});
</script>
</body>
</html>
Control包含控件的基本功能,包括停靠位置,可见性等。
var nav = new BMap.NavigationControl();
map.addControl(nav);
var nav = new BMap.ScaleControl();
map.addControl(nav);
var ov = new BMap.OverviewMapControl();
map.addControl(ov);
<!DOCTYPEhtml>
<html>
<head>
<title>BaiduMapDemo</title>
<style type="text/css">
html{height:100%}
body{height:100%;margin:0px;padding:0px}
#map{height:100%}
</style>
<script type="text/javascript"src="jquery.min.js"></script>
<scripttype="text/javascript"src="http://api.map.baidu.com/api?v=2.0&ak=eQv0G7tIacmmSYWmkgj1gWoz"></script>
</head>
<body>
<div id="map"></div>
</body>
</html>
<scripttype="text/javascript">
var map = new BMap.Map("map");
$(document).ready(function(){
map.centerAndZoom(new BMap.Point(116.404,39.915), 11);
var navigationControl = newBMap.NavigationControl({
enableGeolocation: true
});
map.addControl(navigationControl);
console.debug("inited.");
var geolocationControl = newBMap.GeolocationControl();
geolocationControl.addEventListener("locationSuccess",function(e){
console.debug("locationSuccess="+e.message);
});
geolocationControl.addEventListener("locationError",function(e){
alert(e.message);
});
map.addControl(geolocationControl);
});
</script>
设置显示,隐藏等基本功能。
使用map.addOverlay(),map.removeOverlay()操作。
var pt = new BMap.Point(116.417,39.909);
var myIcon=newBMap.Icon("gun.gif",new BMap.Size(50,50));
var marker=newBMap.Marker(pt,{icon:myIcon});
map.addOverlay(marker);
Icon,仅代表一个图标。
newBMap.Icon('Mario.png',new BMap.Size(32,32),{anchor:new BMap.Size(16,16)})
编辑拆线、多边形:enableEditing,disableEditing。
var pl = new BMap.Polyline([newBMap.Point(116.417,39.909),new BMap.Point(116.617,39.909),new BMap.Point(116.417,39.809)],{strokeColor:'#F00'});
map.addOverlay(pl);
var pg = new BMap.Polygon([
newBMap.Point(116.417,39.809),
newBMap.Point(116.617,39.809),
newBMap.Point(116.417,39.709)
],{
strokeColor:'#0000ff',
fillColor:'#00ff00'});
map.addOverlay(pg);
var pg = new BMap.Polygon([
newBMap.Point(116.417,39.809),
newBMap.Point(116.617,39.809),
newBMap.Point(116.417,39.709)
],{
strokeColor:'#0000ff',
fillColor:'#00ff00',
enableEditing:true});
pg.addEventListener('rightclick',function(e){
pg.disableEditing();
});
map.addOverlay(pg);
<scripttype="text/javascript"src="DrawingManager.js"></script>
<link rel="stylesheet"type="text/css" href="DrawingManager_min.css"/>
var drawingManager = new BMapLib.DrawingManager(map,{
isOpen:true,
enableDrawingTool: true,
drawingMode: BMAP_DRAWING_POLYGON,
drawingToolOptions: {
anchor:BMAP_ANCHOR_TOP_RIGHT,
drawingModes : [
BMAP_DRAWING_MARKER,
BMAP_DRAWING_CIRCLE,
BMAP_DRAWING_POLYLINE,
BMAP_DRAWING_POLYGON,
BMAP_DRAWING_RECTANGLE
]
},
circleOptions:{
strokeColor:"red",
fillColor:"blue"
}
}); ;
<!DOCTYPEhtml>
<html>
<head>
<title>BaiduMapDemo</title>
<style type="text/css">
html{height:100%}
body{height:100%;margin:0px;padding:0px}
#map{height:100%}
</style>
<scripttype="text/javascript"src="jquery.min.js"></script>
<scripttype="text/javascript"src="http://api.map.baidu.com/api?v=2.0&ak=eQv0G7tIacmmSYWmkgj1gWoz"></script>
<scripttype="text/javascript"src="DrawingManager.js"></script>
<link rel="stylesheet"type="text/css" href="DrawingManager_min.css"/>
<body>
<div id="map"></div>
</body>
</html>
<scripttype="text/javascript">
$(document).ready(function(){
var map = new BMap.Map("map");
map.centerAndZoom(new BMap.Point(116.404,39.915), 11);
var nav = new BMap.NavigationControl();
map.addControl(nav);
var scale = new BMap.ScaleControl();
map.addControl(scale);
var ov = new BMap.OverviewMapControl();
map.addControl(ov);
//add drawing manager tool
var drawingManager = newBMapLib.DrawingManager(map, {
isOpen:true,
enableDrawingTool: true,
drawingMode: BMAP_DRAWING_POLYGON,
drawingToolOptions: {
anchor:BMAP_ANCHOR_TOP_RIGHT,
drawingModes : [
BMAP_DRAWING_MARKER,
BMAP_DRAWING_CIRCLE,
BMAP_DRAWING_POLYLINE,
BMAP_DRAWING_POLYGON,
BMAP_DRAWING_RECTANGLE
]
},
circleOptions:{
strokeColor:"red",
fillColor:"blue"
}
});
});
</script>
可以独立使用,也可以与Marker配合使用(使用Marker作为位置)。
var lb = new BMap.Label("BMap LabelTest.",{
position:newBMap.Point(116.617,39.809)
});
map.addOverlay(lb);
var lb = new BMap.Label("BMap LabelTest.",{
position:newBMap.Point(116.617,39.809)
});
//map.addOverlay(lb);
marker.setLabel(lb);
var infoContent="this is forinfowindow<p>SecondLine</p><img style='float:right;margin:4px'id='imgDemo' src='Mario.png' width='139' height='104' title='IMG'/>";
var infoWin=newBMap.InfoWindow(infoContent);
map.openInfoWindow(infoWin,pt);
需要InfoBox_min.js。
可以设置标题、关闭按钮图片、显示位置等。
var infoContent="<divclass='title' style='background:#00f;color:#f00'><strong>中海雅园</strong><spanclass='price'>均价43000</span></div>thisis for infowindow<p>SecondLine</p><imgstyle='float:right;margin:4px' id='imgDemo' src='Mario.png' width='139'height='104' title='IMG'/>";
var infoWin=newBMapLib.InfoBox(map,infoContent,{
boxStyle:{
background:"url('gun.gif')no-repeat center top",
width:"350px",
height:"210px"
},
coloseIconMargin:"1px 1px 00",
enableAutoPan:true,
offset:new BMap.Size(25,25)
});
infoWin.open(marker);
需要SearchInfoWindow_min.js,SearchInfoWindow.css,phone.png,iw_close.gif。
var infoContent="this is forinfowindow<p>SecondLine</p><img style='float:right;margin:4px'id='imgDemo' src='Mario.png' width='139' height='104' title='IMG'/>";
var infow=new BMapLib.SearchInfoWindow(map,infoContent,{
title:"SearchXX",
width:290,
height:180,
panel:"panel",
enableAutoPan:true,
searchTypes:[
BMAPLIB_TAB_SEARCH, //周边检索
BMAPLIB_TAB_TO_HERE, //到这里去
BMAPLIB_TAB_FROM_HERE//从这里出发
]
});
marker.addEventListener("click",function(e){
infow.open(marker);
});
下载网址:http://developer.baidu.com/map/jsdemo/demo/convertor.js
http://developer.baidu.com/map/jsdemo/demo/changeMore.js
BMap.Convertor.translate(newBMap.Point(116.397428,39.90923),0,function(point){
console.debug(point);
});
结果:
functioncallback(results){
console.debug(results);
for(var i in results){
console.debug(results[i]);
console.debug(newBMap.Point(results[i].x,results[i].y));
}
}
var ptArray=[newBMap.Point(116.397428,39.90923), new BMap.Point(116.397428,39.80923)];
BMap.Convertor.transMore(ptArray,0,callback);
结果:
格式为:
http://api.map.baidu.com/geoconv/v1/?coords=114.21892734521,29.575429778924;114.21892734521,29.575429778924&from=1&to=5&ak=你的密钥
结果:
{"status":0,"result":[{"x":114.23074897986,"y":29.579085333961},{"x":114.23075006509,"y":29.579081280206}]}
参考:http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition
需要MapWrapper.min.js
使用addOverlay()将一个marker转换为百度坐标。
示例:mario是gps坐标在百度地图上的显示,gun是转换后的位置。
var pt = new BMap.Point(116.417,39.909);
var myIcon=newBMap.Icon("gun.gif",new BMap.Size(50,50));
var marker=newBMap.Marker(pt,{icon:myIcon});
var myIcon0=newBMap.Icon("close.png",new BMap.Size(50,50));
var marker0=newBMap.Marker(pt,{icon:myIcon0});
map.addOverlay(marker0);
var gpsConverter=newBMapLib.MapWrapper(map,BMapLib.COORD_TYPE_GPS);
gpsConverter.addOverlay(marker);
可以设定多个关键字,可以设置查找的范围(矩形或圆形)。
var local=newBMap.LocalSearch(pt,{renderOptions:{
map:map,
panel:"result"
}});
local.search("天安门");
需要DistanceTool_min.js。
open()后鼠标进入测距状态,双击或调用close()关闭。
var dist=new BMapLib.DistanceTool(map,{});
dist.open();
参见:计算两点距离:getDistance(pt1,pt2)。
console.debug(map.getDistance(newBMap.Point(116.404, 39.915),new BMap.Point(117.404, 39.915)));
console.debug(map.getDistance(newBMap.Point(116.404, 38.915),new BMap.Point(117.404, 39.915)));
参见:折线距离、多边形面积:getPolylineDistance()、getPolygonArea()。
参见:折线距离、多边形面积:getPolylineDistance()、getPolygonArea()。
需要MarkerManager_min.js。
示例:设置两组图标,但仅显示第一组。
//multi markers
var markers=[];
var pt = null;
var marker=null;
var lb=null;
for(var i=0;i<8;++i){
pt=newBMap.Point(116+Math.random()*4,38+Math.random()*4);
marker=new BMap.Marker(pt);
lb = new BMap.Label("BMap LabelTest1="+i,null);
marker.setLabel(lb);
markers.push(marker);
}
//makermanager
var mgr=new BMapLib.MarkerManager(map,{
maxZoom:18,
trackMarekers:true
});
mgr.addMarkers(markers);
mgr.showMarkers();
var markers2=[];
for(var i=0;i<8;++i){
pt=newBMap.Point(116+Math.random()*4,38+Math.random()*4);
marker=new BMap.Marker(pt);
lb = new BMap.Label("BMap LabelTest2="+i,null);
marker.setLabel(lb);
markers2.push(marker);
}
//makermanager
var mgr2=new BMapLib.MarkerManager(map,{
maxZoom:18,
trackMarekers:true
});
mgr2.addMarkers(markers2);
// mgr2.showMarkers();
需要lushu_min.js。
注意:路书并不包括轨迹绘制,需要单独绘制轨迹。
var route=[];
for(var i=0;i<8;++i){
pt=newBMap.Point(116+Math.random()*1,38+Math.random()*1);
route.push(pt);
}
var pl = newBMap.Polyline(route,{strokeColor:'#F00'});
map.addOverlay(pl);
var lushu = new BMapLib.LuShu(map,route,{
defaultContent:"from a tob.",
speed:4500,
landmarkPois:[{lng:route[1].lng,lat:route[1].lat,html:'pausept',pauseTime:2}],
autoView:true,
enableRotation:true
});
lushu.start();
需要texticonoverlay_min.js和markerclusterer_min.js两个js文件。
注意:聚类与标注Label有冲突,可能导致部分标注无法正常显示。
var markers=[];
var pt = null;
var marker=null;
var lb=null;
for(var i=0;i<8;++i){
pt=newBMap.Point(116+Math.random()*4,38+Math.random()*4);
marker=new BMap.Marker(pt);
lb = new BMap.Label("BMapLabel Test="+i,null);
marker.setLabel(lb);
markers.push(marker);
map.addOverlay(marker);
}
//cluster
var markerCluster = newBMapLib.MarkerClusterer(map,{markers:markers});
可以设置一个html的id作为返回结果。
var driving=new BMap.DrivingRoute(map,{
policy:BMAP_DRIVING_POLICY_LEAST_TIME,
renderOptions:{
map:map,
panel:"panel",
autoViewport:true
}
});
driving.search("天安门","四惠东");
var transit=new BMap.TransitRoute(map,{
renderOptions:{
map:map,
panel:"result",
autoViewport:true
},
policy:BMAP_TRANSIT_POLICY_LEAST_TIME
});
transit.setSearchCompleteCallback(function(e){
console.debug(transit.getResults().getPlan(0).getRoute(0).getDistance(true));
});
transit.search("天安门","四惠东");
var walking=new BMap.WalkingRoute(map,{
renderOptions:{
map:map,
panel:"result",
autoViewport:true
}
});
walking.setSearchCompleteCallback(function(e){
console.debug(walking.getResults().getPlan(0).getRoute(0).getDistance(true));
});
walking.search("天安门","故宫");
需要GeoUtils_min.js。
console.debug("45degree=rad:"+BMapLib.GeoUtils.degreeToRad(45));
console.debug("pairad=degree:"+BMapLib.GeoUtils.radToDegree(Math.PI));
var pt1 = new BMap.Point(116.400,39.910);
var pt2 = new BMap.Point(116.402,39.912);
console.debug("pt1-pt2="+BMapLib.GeoUtils.getDistance(pt1,pt2));
console.debug("pt1-pt2=(map)"+map.getDistance(pt1,pt2));
参考:http://blog.csdn.net/chenguang79/article/details/40506469
var pts = [];
var pt1 = new BMap.Point(116.400,39.910);
var pt2 = new BMap.Point(116.402,39.912);
var pt3 = new BMap.Point(116.403,39.914);
var pt4 = new BMap.Point(116.404,39.917);
var pt5 = new BMap.Point(116.406,39.918);
pts.push(pt1);
pts.push(pt2);
pts.push(pt3);
pts.push(pt4);
pts.push(pt5);
var polyline = new BMap.Polyline(pts);
var polygon = new BMap.Polygon(pts);
var dis =BMapLib.GeoUtils.getPolylineDistance(polyline);
var area =BMapLib.GeoUtils.getPolygonArea(polygon);
console.debug("dis="+dis+",area="+area);
var pt1 = new BMap.Point(116.400,39.910);
var pt2 = new BMap.Point(116.402,39.912);
var pt3 = new BMap.Point(116.403,39.914);
var pt4 = new BMap.Point(116.404,39.917);
var pt5 = new BMap.Point(116.406,39.918);
pts.push(pt1);
pts.push(pt2);
pts.push(pt3);
pts.push(pt4);
pts.push(pt5);
var polyline = new BMap.Polyline(pts);
var polygon = new BMap.Polygon(pts);
console.debug("pt1 in polygon?"+BMapLib.GeoUtils.isPointInPolygon(pt1,polygon));
console.debug("pt1 onpolyline?"+BMapLib.GeoUtils.isPointOnPolyline(pt1,polyline));