实现功能:调用微信获取当前位置接口,转换为百度坐标,进行导航
(1)net.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <!DOCTYPE html> <html> <head> <title>LBS导航</title> <s:param name="appId">${appId}</s:param> </s:include> <script type="text/javascript"> /*从url获取值*/ var icon =
'<s:url value='/custom/tydc/other/images/location.png'></s:url>'; var aimIcon =
'<s:url value='/custom/tydc/other/images/location_blue.png'></s:url>'; </script> <script type="text/javascript"
src="<s:url value="/custom/tydc/other/js/net.js"></s:url>"></script> <script type="text/javascript"
src="http://api.map.baidu.com/api?v=2.0&ak=l1nCuDjLYBOSV6ZRxEXfMQFN"></script> </head> <body> <div id="location_map"></div> <div class="map_card"> <div class="network_list"> <div class="text"> <span class="title" id="name"></span> <span class="address" id="address">
地址:昌平区龙腾苑六区</span> <span class="phone">
<a id="tel" href="tel:05378676883">
<span class="urlphone" id="linkphone">
电话:0537-8676883
</span></a></span> </div> </div> </div> </body> </html>
(2)net.js //目标点 var aimPoint; var map; $(function() { $(document).ready(function() { var h = $(window).height(), h2; $("#location_map").css("height", h*0.8); $(window).resize(function() { h2 = $(this).height(); $("#location_map").css("height", h2); }); }); //调用微信接口,获取地理位置 wxReady(); //点击信息框 $(".address").on("click", "", function() { map.centerAndZoom(new BMap.Point(aimPoint.lng, aimPoint.lat), 15); //map.setViewport([ aimPoint ]); }); }); function wxReady(){ wx.ready(function(){ wx.getLocation({ success: function (res) { var xx = res.longitude; // 经度,浮点数,范围为180 ~ -180。 var yy = res.latitude; // 纬度,浮点数,范围为90 ~ -90 findLocation(xx,yy); } }); }); } function findLocation(xx,yy){ //将微信坐标转换为百度经纬度 bd_encrypt(yy,xx); //计算后得到百度坐标 longitude latitude var points=[]; //赋值操作 //$("#name").html("爱乐活"); //$("#address").html("地址:北京大兴区葛家庄"); //$("#linkphone").html("电话:06326878663"); //$("#tel").attr("href","tel:06326878663"); //$("#phone").html(""); //目的经纬度 var destLng = 141.26601; var destLat = 30.478838; aimPoint=new BMap.Point(destLng,destLat); points.push( aimPoint ); // 百度地图API功能 map = new BMap.Map("location_map");// 创建Map实例 map.centerAndZoom(new BMap.Point(longitude,latitude ), 11); map.enableScrollWheelZoom(); map.enableContinuousZoom(); pt = new BMap.Point(longitude,latitude); points.push(pt); map.setViewport(points); var driving = new BMap.DrivingRoute(map,
{renderOptions:{map: map,autoViewport: false}}); driving.search(pt, aimPoint); } var longitude,latitude; //处理微信坐标 function bd_encrypt(gg_lat, gg_lon) { var x_pi = 3.14159265358979324 * 3000.0 / 180.0; var x = gg_lon; var y = gg_lat; var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi); var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi); longitude = z * Math.cos(theta) + 0.0065; latitude = z * Math.sin(theta) + 0.006; }