是应用在微信中,自定义菜单,菜单直接链接到这个HTML5页面,获取当前位置后,页面中定好目的地,这样打开页面后直接进入导航页面
可以省下先发送位置信息后,点确定再出导航,省一步,
<!DOCTYPEhtml> <htmllang="zh-cmn-Hans"> <metacharset="UTF-8"> <metaname="viewport"content="width=device-width,initial-scale=1,user-scalable=0"> <title>HTML5页面直接调用百度地图API,获取当前位置,直接导航目的地</title> <scripttype="text/javascript"src="http://api.map.baidu.com/api?v=2.0&ak=wDYEcxgRRheZwyC9jpN1Tt7fzr2zjosZ"></script> <scriptsrc="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script><!--调用jQuery--> <styletype="text/css"> body,html,#allmap{width:100%;height:100%;overflow:hidden;margin:0;font-family:"微软雅黑";} </style> </head> <body> <divid="allmap"></div> </body> </html> <scripttype="text/javascript"> varmap=newBMap.Map("allmap"); varpoint=newBMap.Point(116.709684,39.89778); map.centerAndZoom(point,16); map.enableScrollWheelZoom(); varmyIcon=newBMap.Icon("myicon.png",newBMap.Size(30,30),{ anchor:newBMap.Size(10,10) }); varmarker=newBMap.Marker(point,{icon:myIcon}); map.addOverlay(marker); vargeolocation=newBMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus()==BMAP_STATUS_SUCCESS){ varmk=newBMap.Marker(r.point); map.addOverlay(mk); //map.panTo(r.point);//地图中心点移到当前位置 varlatCurrent=r.point.lat; varlngCurrent=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); varlicontent="<b>健龙森羽毛球馆</b><br>"; licontent+="<span><strong>地址:</strong>北京市通州区滨河中路108号</span><br>"; licontent+="<span><strong>电话:</strong>(010)81556565/6969</span><br>"; varopts={ width:200, height:80, }; varinfoWindow=newBMap.InfoWindow(licontent,opts); marker.openInfoWindow(infoWindow); marker.addEventListener('click',function(){ marker.openInfoWindow(infoWindow); }); </script>