


HTML5 method to call Baidu Map API to obtain the current location and directly navigate to the destination
This article mainly introduces the implementation code of HTML5 page to directly call Baidu Map API to obtain the current location and direct navigation destination. Friends who need it can refer to it. I hope it can help everyone.
You can save the need to send the location information first, click OK and then exit the navigation, saving one step,
<!DOCTYPE html> <html lang="zh-cmn-Hans"> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0"> <title>HTML5页面直接调用百度地图API,获取当前位置,直接导航目的地</title> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=wDYEcxgRRheZwyC9jpN1Tt7fzr2zjosZ"></script> <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script><!--调用jQuery--> <style type="text/css"> body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";} </style> </head> <body> <p id="allmap"></p> </body> </html> <script type="text/javascript"> var map = new BMap.Map("allmap"); var point = new BMap.Point(116.709684,39.89778); map.centerAndZoom(point, 16); map.enableScrollWheelZoom(); var myIcon = new BMap.Icon("myicon.png",new BMap.Size(30,30),{ anchor: new BMap.Size(10,10) }); var marker=new BMap.Marker(point,{icon: myIcon}); map.addOverlay(marker); var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ var mk = new BMap.Marker(r.point); map.addOverlay(mk); //map.panTo(r.point);//地图中心点移到当前位置 var latCurrent = r.point.lat; var lngCurrent = r.point.lng; //alert('我的位置:'+ latCurrent + ',' + lngCurrent); location.href="http://api.map.baidu.com/direction?origin="+latCurrent+","+lngCurrent+"&destination=39.89778,116.709684&mode=driving®ion=北京&output=html"; } else { alert('failed'+this.getStatus()); } },{enableHighAccuracy: true}) map.addOverlay(marker); var licontent="<b>健龙森羽毛球馆</b><br>"; licontent+="<span><strong>地址:</strong>北京市通州区滨河中路108号</span><br>"; licontent+="<span><strong>电话:</strong>(010)81556565 / 6969</span><br>"; var opts = { width : 200, height: 80, }; var infoWindow = new BMap.InfoWindow(licontent, opts); marker.openInfoWindow(infoWindow); marker.addEventListener('click',function(){ marker.openInfoWindow(infoWindow); }); </script>
Related recommendations:
js Call Baidu map api and add points and annotations on the map_javascript skills
The above is the detailed content of HTML5 method to call Baidu Map API to obtain the current location and directly navigate to the destination. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.
