赞
踩
使用JavaScript技术,利用api接口开发百度地图简单方便。
1.设置整体页面的布局样式
- <style type="text/css">
- body, html{width:100%;height:100%;margin:0;font-family:"微软雅黑";}
- #point{width:980px;min-height:160px;margin-left:12px;margin-top:6px;margin:0 auto;padding-left:2px;}
- #map{height:1060px;width:1160px;margin:0 auto;margin-top:16px;margin-bottom:8px;}
- #r-result{width:1080px;min-height:80px;margin-top:10px;padding-left:2px;margin:0 auto;}
- #r-result table{width:100%;}
- </style>
2. 最重要的就是利用api接口获取地图数据,设置Maker标记,选择出行策略
- <!-- js技术 -->
- <script type="text/javascript">
- window.onload = loadScript;
- // 首次加载的函数
- function loadScript() {
- var script = document.createElement("script");
- // 申请的AK
- script.src = "http://api.map.baidu.com/api?v=2.0&ak=YBPGfONGeN2ENSn9YzACYCGzazCKsZsb&callback=initialize";//此为v2.0版本的引用方式
- // 百度地图API功能
- document.body.appendChild(script);
- // 自动定位启动
- autoLocation();
- }
- // 定义全局变量
- var map ;
- var startAddr,endAddr;
- var startCity = "天津",endCity="";
-
- // 初始化处理 : Called first
- function initialize()
- {
- map = new BMap.Map("map");
-
- //初始化地图,设置城市和地图级别
- // 默认定位点设置
- map.centerAndZoom(startCity,12);
- document.getElementById("start").value = startCity;
- if(endCity != ""){
-
- }
- map.enableScrollWheelZoom();
- map.enableInertialDragging();
- map.enableContinuousZoom();
-
- // 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)+' 米。'); //获取两点距离,保留小数点后两位
- //var polyline = new BMap.Polyline([pointA,pointB], {strokeColor:"red", strokeWeight:8, strokeOpacity:0.5}); //定义折线
- //map.addOverlay(polyline); //添加折线到地图上
-
- // 地图控制控件
- var bottom_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左下角,添加比例尺
- var bottom_left_navigation = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_LEFT}); //左下角,添加默认缩放平移控件
- /*缩放控件type有四种类型:
- BMAP_NAVIGATION_CONTROL_SMALL:仅包含平移和缩放按钮;BMAP_NAVIGATION_CONTROL_PAN:仅包含平移按钮;BMAP_NAVIGATION_CONTROL_ZOOM:仅包含缩放按钮
- */
- //添加控件和比例尺
- map.addControl(bottom_left_control);
- map.addControl(bottom_left_navigation);
-
- // 添加手动定位控件
- var geolocationControl = new BMap.GeolocationControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});
- geolocationControl.addEventListener("locationSuccess", function(e){
- // 定位成功事件
- var address = "";
- address += e.addressComponent.province;
- address += e.addressComponent.city;
- address += e.addressComponent.district;
- address += e.addressComponent.street;
- address += e.addressComponent.streetNumber;
- // 改变定位后的城市名
- startCity = address;
- document.getElementById("start").value = address;
- // 清空Maker
- map.clearOverlays();
- alert("当前定位地址为:" + address);
- initialize();
- });
- geolocationControl.addEventListener("locationError",function(e){
- // 定位失败事件
- alert(e.message);
- });
- map.addControl(geolocationControl);
-
- //2D图,卫星图
- var mapType1 = new BMap.MapTypeControl({anchor: BMAP_ANCHOR_TOP_RIGHT,mapTypes: [BMAP_NORMAL_MAP,BMAP_HYBRID_MAP]});
- map.addControl(mapType1);
-
- // 默认选择步行策略
- //showPolicy(1);
-
- }
- // 自动定位处理 : 根据浏览器的地址获取经纬度
- function autoLocation()
- {
- var geolocation = new BMap.Geolocation();
- geolocation.getCurrentPosition(function(r){
- if(this.getStatus() == BMAP_STATUS_INVALID_REQUEST){
- alert("非法请求");
- return;
- }
- if(this.getStatus() == BMAP_STATUS_PERMISSION_DENIED){
- alert("没有开启定位权限");
- return;
- }
- if(this.getStatus() == BMAP_STATUS_SERVICE_UNAVAILABLE){
- alert("服务不可用");
- return;
- }
- if(this.getStatus() == BMAP_STATUS_TIMEOUT){
- alert("网络超时");
- return;
- }
- if(this.getStatus() == BMAP_STATUS_SUCCESS){
- var geoc = new BMap.Geocoder();
- var startPoint = new BMap.Point(r.point.lng,r.point.lat);
- var mk = new BMap.Marker(startPoint);
- map.panTo(r.point);
- map.addOverlay(mk);
- geoc.getLocation(startPoint, function(rs){
- // 根据经纬度获取物理地址信息
- var addComp = rs.addressComponents;
- // 改变定位后的城市名
- startCity = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
- document.getElementById("start").value = startCity;
- alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
- });
- // alert('当前位置:'+r.point.lng+','+r.point.lat);
- }else {
- alert('failed'+this.getStatus());
- }
- },{enableHighAccuracy: true})
- //关于状态码
- //BMAP_STATUS_SUCCESS 检索成功。对应数值“0”。
- //BMAP_STATUS_CITY_LIST 城市列表。对应数值“1”。
- //BMAP_STATUS_UNKNOWN_LOCATION 位置结果未知。对应数值“2”。
- //BMAP_STATUS_UNKNOWN_ROUTE 导航结果未知。对应数值“3”。
- //BMAP_STATUS_INVALID_KEY 非法密钥。对应数值“4”。
- //BMAP_STATUS_INVALID_REQUEST 非法请求。对应数值“5”。
- //BMAP_STATUS_PERMISSION_DENIED 没有权限。对应数值“6”。(自 1.1 新增)
- //BMAP_STATUS_SERVICE_UNAVAILABLE 服务不可用。对应数值“7”。(自 1.1 新增)
- //BMAP_STATUS_TIMEOUT 超时。对应数值“8”。(自 1.1 新增)
- }
- // 地址解析
-
- // 逆地址解析 :输入框发生变化事件
- function onChangeS()
- {
- // 起始
- var start = document.getElementById("start").value;
- var myGeo = new BMap.Geocoder();
- // 将地址解析结果显示在地图上,并调整地图视野
- myGeo.getPoint(start, function(point){
- if (point) {
- startCity = start;
- map.clearOverlays();
- map.centerAndZoom(startCity,12);
- map.panTo(point);
- map.addOverlay(new BMap.Marker(point));
- document.getElementById("start").value = startCity;
- //initialize();
- }else{
- alert("您输入地址没有解析到结果!");
- }
- }, "");
- }
- function onChangeE()
- {
- // 终止
- var end = document.getElementById("end").value;
- var myGeo = new BMap.Geocoder();
- // 将地址解析结果显示在地图上,并调整地图视野
- myGeo.getPoint(end, function(point){
- if (point) {
- endCity = end;
- map.clearOverlays();
- map.centerAndZoom(endCity,12);
- map.panTo(point);
- map.addOverlay(new BMap.Marker(point));
- document.getElementById("end").value = endCity;
- //initialize();
- }else{
- alert("您输入地址没有解析到结果!");
- }
- }, "");
- }
- // 对起终点设置的处理函数
- function theLocation()
- {
- // 通过经纬度设置Maker标记
- if(document.getElementById("longitude").value != "" && document.getElementById("latitude").value != "")
- {
- map.clearOverlays();
- var new_point = new BMap.Point(document.getElementById("longitude").value,document.getElementById("latitude").value);
- var marker = new BMap.Marker(new_point); // 创建标注
- map.addOverlay(marker); // 将标注添加到地图中
- map.panTo(new_point);
- }
- }
- // 出行策略
- /*
- * 1. 步行 2. 驾车
- */
- function showPolicy(index)
- {
- if(startCity == ""){
- alert("请输入出发点");
- return;
- }
- if(endCity == ""){
- alert("请输入目的地");
- return;
- }
- if(index == 1){
- // 步行
- map.clearOverlays();
- map.centerAndZoom(startCity,12);
- var walking = new BMap.WalkingRoute(map, {renderOptions: {map: map, panel: "r-result",autoViewport: true }});
- walking.search(startCity, endCity);
- }
- if(index == 2){
- //公交
- map.clearOverlays();
- map.centerAndZoom(startCity,12);
- var routePolicy = [BMAP_TRANSIT_POLICY_LEAST_TIME,BMAP_TRANSIT_POLICY_LEAST_TRANSFER,BMAP_TRANSIT_POLICY_LEAST_WALKING,BMAP_TRANSIT_POLICY_AVOID_SUBWAYS];
- var transit = new BMap.TransitRoute(map, {renderOptions: {map: map, panel: "r-result",autoViewport: true}});
- transit.setPolicy(routePolicy[0]);
- transit.search(startCity, endCity);
- }
- if(index == 3){
- // 驾车
- map.clearOverlays();
- map.centerAndZoom(startCity,12);
- var driving = new BMap.DrivingRoute(map, {renderOptions: {map: map, panel: "r-result", autoViewport: true}});
- driving.search(startCity, endCity);
- }
- }
- // 获取搜索框输入的内容
- function submitSearch()
- {
- var value = document.getElementById("searchValue").value;
- if(value != ""){
- search("'"+value+"'");
- }else{
- alert("请输入搜索关键字");
- }
- }
- // 搜索内容并返回结果
- function search(str)
- {
- if(str != ""){
- map.clearOverlays();
- map.centerAndZoom(startCity,12);
- var local = new BMap.LocalSearch(map, {renderOptions: {map: map, panel: "r-result",autoViewport: true}});
- local.search(str);
- }else{
- alert("搜索内容错误");
- }
- }
- </script>
- <body>
- <div id=""
- style="width:1120px;height:30px;margin:0 auto;font-size:18px;background:#0000cc;color:#fff;text-align:left;padding-left:24px;padding-top:8px;padding-bottom:6px;">
- | 出行游玩,定制属于自我的Map
- </div>
-
- <div id="point">
- 我的位置: <input id="start" type="text" size="28px" style="width:136px; margin-right:10px;" οnchange="onChangeS()"/> ——>
- 目的地: <input id="end" type="text" size="28px" style="width:136px; margin-right:10px;" οnchange="onChangeE()"/>
- <input type="button" value="确定" style="width:54px; font-size:18px;margin-right:10px;margin-top:16px;background:#888;" οnclick="showPolicy(1)" />
- <br/><p>
- <div style="width:100%;height:30px;margin-bottom:2px;padding-bottom:0px;">
- <div style="width:188px;float:left;background:#066;color:#fff;margin-bottom:4px;margin-top:0px;padding-top:4px;padding-bottom:4px;text-align:center;font-size:18px;">
- 出行策略
- </div>
- <div style="float:left;margin-left:6px;padding-top:5px;">
- <input type="radio" value="policy_1" name="policy"
- style="font-size:18px;margin-right:10px;color:#fff;" οnclick="showPolicy(1)" >步行</input>
- <input type="radio" value="policy_2" name="policy"
- style="font-size:18px;margin-right:10px;color:#fff;" οnclick="showPolicy(2)" >公交出行</input>
- <input type="radio" value="policy_3" name="policy"
- style="font-size:18px;margin-right:10px;color:#fff;" οnclick="showPolicy(3)" >驾车出行</input>
- </div>
- </div>
- <br/>
- <div style="width:100%;height:30px;margin-bottom:4px;margin-top:0px;">
- <div style="width:188px;float:left;background:#066;color:#fff;margin-top:4px;padding-top:4px;padding-bottom:4px;text-align:center;font-size:18px;">搜索周边</div>
- <div style="width:24px;float:left;margin-left:28px;margin-top:8px;margin-right:8px;">
- <input type="text" id="searchValue" size="22px"/>
- </div>
- <div style="width:36px;float:left;margin-top:8px;margin-left:168px;">
- <input type="button" οnclick="submitSearch()" style="font-size:16px;border:solid 1;"
- onMouseOver="JavaScript:this.style.background='#00ce00'" onMouseOut="this.style.background=''" value="搜索" />
- </div>
- </div>
- <div style="width:100%;margin-top:16px;margin-left:26px;margin-bottom:8px;">
- <input type="button" value="餐饮" style="font-size:20px;margin-right:6px;margin-top:2px;border:0;background:#c33;color:#fff;"
- onMouseOver="JavaScript:this.style.color='#000'" onMouseOut="this.style.color='#fff'" οnclick="search('餐饮')"/>
- <input type="button" value="酒店" style="font-size:20px;margin-right:6px;margin-top:2px;border:0;background:#068;color:#fff;"
- onMouseOver="JavaScript:this.style.color='#000'" onMouseOut="this.style.color='#fff'" οnclick="search('酒店')"/>
- <input type="button" value="地铁" style="font-size:20px;margin-right:6px;margin-top:2px;border:0;background:#c36;color:#fff;"
- onMouseOver="JavaScript:this.style.color='#000'" onMouseOut="this.style.color='#fff'" οnclick="search('地铁')"/>
- <input type="button" value="公交" style="font-size:20px;margin-right:6px;margin-top:2px;border:0;background:#888;color:#fff;"
- onMouseOver="JavaScript:this.style.color='#000'" onMouseOut="this.style.color='#fff'" οnclick="search('公交')"/>
- <input type="button" value="周围景点" style="font-size:20px;margin-right:6px;margin-top:2px;border:0;background:#033;color:#fff;"
- onMouseOver="JavaScript:this.style.color='#000'" onMouseOut="this.style.color='#fff'" οnclick="search('景点')"/>
- <input type="button" value="公园" style="font-size:20px;margin-right:6px;margin-top:2px;border:0;background:#09c;color:#fff;"
- onMouseOver="JavaScript:this.style.color='#000'" onMouseOut="this.style.color='#fff'" οnclick="search('公园')"/>
- <input type="button" value="小吃街" style="font-size:20px;margin-right:6px;margin-top:2px;border:0;background:#906;color:#fff;"
- onMouseOver="JavaScript:this.style.color='#000'" onMouseOut="this.style.color='#fff'" οnclick="search('小吃')"/>
- <input type="button" value="游乐场" style="font-size:20px;margin-right:6px;margin-top:2px;border:0;background:#936;color:#fff;"
- onMouseOver="JavaScript:this.style.color='#000'" onMouseOut="this.style.color='#fff'" οnclick="search('游乐场')"/>
- <input type="button" value="KTV" style="font-size:20px;margin-right:6px;margin-top:2px;border:0;background:#c00;color:#fff;"
- onMouseOver="JavaScript:this.style.color='#000'" onMouseOut="this.style.color='#fff'" οnclick="search('KTV')"/>
- <input type="button" value="商业街巷" style="font-size:20px;margin-right:6px;margin-top:2px;border:0;background:#963;color:#fff;"
- onMouseOver="JavaScript:this.style.color='#000'" onMouseOut="this.style.color='#fff'" οnclick="search('商业街巷')"/>
- <input type="button" value="搜索更多>>" style="background:#fff;color:#000;border:0;clickable:false;">
- </div>
- </div>
-
- <div id="map"></div>
- <div id="r-result"></div>
- </body>
- </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。