百度地图API学习总结

一、百度地图API介绍

 

百度地图API是可以给开发人员调用的开放API,我们可以用Javascript语言进行调用;

听起来API这个概念很抽象,初学者想:“百度的API,我们怎么能调用?”,其实不是这样,我们通过一个URL地址进行引用,然后就可以调用他的API;

不需要Web服务器,只需要一个浏览器就能够使用百度地图API;

 

二、调用API的基本文件格式

 

以下为开发百度地图API的最基础代码:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
< html >
     < head >
         <!--引用百度地图-->
         < script type = "text/javascript" src = "http://api.map.baidu.com/api?v=1.3" >
         </ script >
         < title >
             如何调用API
         </ title >
                 <!--
         设计样式
             container容器:占50%大小
         -->
         < style type = "text/css" >
         #container{
             width:50%;
             height:50%;
         }
         </ style >
     </ head >
     < body >
         < div id = "container" ></ div >
         < script type = "text/javascript" >
             var map = new BMap.Map("container");//在container容器中创建一个地图,参数container为div的id属性;
             var point = new BMap.Point(500,500);//定位
             map.centerAndZoom(point,15);                //将point移到浏览器中心,并且地图大小调整为15;
             
             <!--以后只需要在此处添加代码即可-->
         </ script >
     </ body >
</ html >

 

三、常用技术


1.创建地图: var map = new BMap.Map("divid");

2.创建坐标点:var point = new BMap.Point("经度","纬度");

3.设置视图中心点:map.centerAndZoom(point,size);

4.激活滚轮调整大小功能:map.enableScrollWheelZoom();

5.添加控件:map.addControl(new BMap.Xxx());

6.添加覆盖物:map.addOverlay();

 


 

控件介绍

 

1.NavigationControl:缩放地图的控件,默认在左上角;

2.OverviewMapControl:地图的缩略图的控件,默认在右下方;

3.ScaleControl:地图显示比例的控件,默认在左下方;

4.MapTypeControl:地图类型控件,默认在右上方;

如下图所示:

 百度地图API学习总结

 

map.addControl()方法添加控件;

代码示例:

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<html>
     <head>
         <!--引用百度地图-->
         <script type= "text/javascript" src= "http://api.map.baidu.com/api?v=1.3" >
         </script>
         <title>
             控件使用
         </title>
                 <!--
         设计样式
             container容器:占50%大小
         -->
         <style type= "text/css" >
         #container{
             width:50%;
             height:50%;
         }
         </style>
     </head>
     <body>
         <div id= "container" ></div>
         <script type= "text/javascript" >
             var map =  new BMap.Map( "container" ); //在container容器中创建一个地图,参数container为div的id属性;
             var point =  new BMap.Point(500,500); //定位
             map.centerAndZoom(point,15);                 //将point移到浏览器中心,并且地图大小调整为15;
             map.addControl( new BMap.NavigationControl());
             map.addControl( new BMap.MapTypeControl());
             map.addControl( new BMap.ScaleControl());
             map.addControl( new BMap.OverviewMapControl());
         </script>
     </body>
</html>

 

 

覆盖物介绍

 

覆盖物就是覆盖在地图上的某个事物;

 

1.标注:Marker

 百度地图API学习总结

(1)在point处添加标注:var marker = new BMap.Marker(point);     

(2)添加覆盖物:map.addOverlay(marker);

(3)激活标注的拖拽功能:marker.enableDragging();

(4)为标注添加事件:marker.addEventListener("名称",function(){

    //点击标注后的事件

});

(5)删除覆盖物:map.removeOverlay(marker);

(6)销毁标注:marker.dispose();

 

2.信息窗口:InfoWindow

 百度地图API学习总结

(1)在某个特定的位置创建一个信息窗口:var infowindow = new BMap.InfoWindow("内容",{width:250,height:100,title:"hello"});

(2)在地图中央打开信息窗口:map.openInfoWindow(infoWindow,map.getCenter());

 

3.折线:Polyline

 

(1)var polyline = new BMap.Polyline([new BMap.Point(X1,Y1),new BMap.Point(X2,Y2),new BMap.Point(X3,Y3)],{strokeColor:"blue", strokeWeight:6, strokeOpacity:0.5});

(2)map.addOverlay(polyline);

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<html>
     <head>
         <!--引用百度地图-->
         <script type= "text/javascript" src= "http://api.map.baidu.com/api?v=1.3" >
         </script>
         <title>
             如何调用API
         </title>
                 <!--
         设计样式
             container容器:占50%大小
         -->
         <style type= "text/css" >
         #container{
             width:50%;
             height:50%;
         }
         </style>
     </head>
     <body>
         <div id= "container" ></div>
         <script type= "text/javascript" >
             var map =  new BMap.Map( "container" ); //在container容器中创建一个地图,参数container为div的id属性;
             var point =  new BMap.Point(116.404, 39.915); //定位
             map.centerAndZoom(point,15);                 //将point移到浏览器中心,并且地图大小调整为15;
             
             //标注
             var marker =  new BMap.Marker(point);
             map.addOverlay(marker);
             marker.addEventListener( "click" , function (){  //点击标注时出发事件
                 alert( "您点击了标注" );
             });
             marker.enableDragging();     //标注可拖拽
             
             //创建信息窗口
             var opts = { 
                 width : 250,      // 信息窗口宽度 
                 height: 100,      // 信息窗口高度 
                 title :  "Hello"  // 信息窗口标题 
            
             var infoWindow =  new BMap.InfoWindow( "World" , opts);   // 创建信息窗口对象 
             map.openInfoWindow(infoWindow, map.getCenter());       // 打开信息窗口
             
             //折线
             var polyline =  new BMap.Polyline([ 
                     new BMap.Point(116.399, 39.910), 
                     new BMap.Point(116.405, 39.920),
                     new BMap.Point(117.321,40.321) 
                         ], 
                         {strokeColor: "blue" , strokeWeight:6, strokeOpacity:0.5}   //蓝色、宽度为6
             ); 
             map.addOverlay(polyline); 
             
         </script>
     </body>
</html>


 

 

四、常见问题

 

1.怎么获得我想查找的地理位置的经度和纬度?

 

http://dev.baidu.com/wiki/static/map/API/tool/creatMap/

 

2.哪里有学习百度地图API的示例代码?

 

http://dev.baidu.com/wiki/static/map/API/examples/index.html



3、html5定位获取当前位置并在百度地图上显示

在开发移动端 web 或者webapp时,使用百度地图 API 的过程中,经常需要通过手机定位获取当前位置并在地图上居中显示出来,这就需要用到html5的地理定位功能。 

复制代码
代码如下:

navigator.geolocation.getCurrentPosition(callback); 

在获取坐标成功之后会执行回调函数 callback; callback 方法的参数就是获取到的坐标点;然后可以初始化地图,设置控件、中心点、缩放等级,然后给地图添加point的overlay: 

复制代码
代码如下:

var map = new BMap.Map("mapDiv");//mapDiv为放地图的 div 的 id 
map.addControl(new BMap.NavigationControl()); 
map.addControl(new BMap.ScaleControl()); 
map.addControl(new BMap.OverviewMapControl()); 
map.centerAndZoom(point, 15);//point为坐标点,15为地图缩放级别,最大级别是 18 
var pointMarker = new BMap.Marker(point); 
map.addOverlay(pointMarker); 

然而事实上这样还不够,显示出来的结果并不准,这是因为 getCurrentPosition 获取到的坐标是 GPS 经纬度坐标,而百度地图的坐标是经过特殊转换的,所以,在获取定位坐标和初始化地图之间需要进行一步坐标转换工作,该转换方法百度API里面已经提供了,转换一个点或者批量装换的方法均有提供:单个点转换需引用 http://developer.baidu.com/map/jsdemo/demo/convertor.js,批量转换需引用 http://developer.baidu.com/map/jsdemo/demo/changeMore.js,这里只需要前者即可: 

复制代码
代码如下:

BMap.Convertor.translate(gpsPoint, 0, callback); 
//gpsPoint:转换前坐标,第二个参数为转换方法,0表示gps坐标转换成百度坐标,callback回调函数,参数为新坐标点 

例子的详细代码如下:(引用中的ak是申请的key) 

复制代码
代码如下:

<!DOCTYPE html> 
<html lang="zh-cn"> 
<head> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title></title> 
<style type="text/css"> 
*{ 
height: 100%; //设置高度,不然会显示不出来 

</style> 
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=··············"></script> 
<script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script> 
<script> 
$(function(){ 
navigator.geolocation.getCurrentPosition(translatePoint); //定位 
}); 
function translatePoint(position){ 
var currentLat = position.coords.latitude; 
var currentLon = position.coords.longitude; 
var gpsPoint = new BMap.Point(currentLon, currentLat); 
BMap.Convertor.translate(gpsPoint, 0, initMap); //转换坐标 

function initMap(point){ 
//初始化地图 
map = new BMap.Map("map"); 
map.addControl(new BMap.NavigationControl()); 
map.addControl(new BMap.ScaleControl()); 
map.addControl(new BMap.OverviewMapControl()); 
map.centerAndZoom(point, 15); 
map.addOverlay(new BMap.Marker(point)) 

</script> 
</head> 
<body> 
<div id="map"></div> 
</body> 
</html> 

本人开发过程中觉得电脑的定位速度有点慢,经常无法获取坐标导致地图无法显示,建议用手机测试,定位较快。 

当然了,如果仅是开发移动端的网页,就不需要使用jQuery,框架太大,可以换用其他轻量级的移动端的 js 框架。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值