HBuilderX 内置高德地图定位以及构建路径

定位

构建驾车路径

以上两个为该项目的基础,首先,先进行简单的结合,
 

<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>Map Example</title>
		<script type="text/javascript">
			var em = null,
				map = null,
				start_point=null,end_point=null;
			// H5 plus事件处理
			function plusReady() {
				// 确保DOM解析完成
				if (!em || !window.plus || map) {
					return
				};
				map = new plus.maps.Map("map");
				map.centerAndZoom(new plus.maps.Point(116.3977, 39.906016), 12);
			}
			if (window.plus) {
				plusReady();
			} else {
				document.addEventListener("plusready", plusReady, false);
			}
			// DOMContentloaded事件处理
			document.addEventListener("DOMContentLoaded", function() {
				em = document.getElementById("map");
				plusReady();
			}, false);
			// 获取用户的当前位置信息
			function getUserLocation() {
				map.getUserLocation(function(state, point) {
					if (0 == state) {
						// alert( JSON.stringify(point) );
						map.centerAndZoom(point, 18);
						start_point = new plus.maps.Point(point.getLng(), point.getLat());
					} else {
						alert("Failed!");
					}
				});
			}
			function drivingSearch() {
				// 检索从北京天安门到大钟寺的驾车线路
				var searchObj = new plus.maps.Search(map);
				searchObj.onRouteSearchComplete = function(state, result) {
					console.log("onRouteSearchComplete: " + state + " , " + result.routeNumber);
					if (state == 0) {
						if (result.routeNumber <= 0) {
							alert("没有检索到结果");
						}
						for (var i = 0; i < result.routeNumber; i++) {
							map.addOverlay(result.getRoute(i));
						}
					} else {
						alert("检索失败");
					}
				}
				getUserLocation();
				end_point = new plus.maps.Point(116.3977, 39.906016);
				searchObj.drivingSearch(start_point, "", end_point, "");
			}
		</script>
		<style type="text/css">
			* {
				-webkit-user-select: none;
			}

			html,
			body {
				margin: 0px;
				padding: 0px;
				height: 100%;
			}
			#map {
				width: 100%;
				position: fixed;
				top: 100px;
				bottom: 0px;
				line-height: 200px;
				text-align: center;
				background: #FFFFFF;
			}
		</style>
	</head>
	<body>
		<button type="button" onclick="drivingSearch()">导航</button>
		<div id="map">地图加载中...</div>

	</body>
</html>

这里我是把getUserLocation塞到drivingSearch里面,即先获取位置再构建路径,但运行之后,你需要点击两次按钮才能拿构建路径,这里的原因是js并不会按照程序所给的顺序执行命令,而是多线程,这段代码中,程序先构建了路径,之后再获取了起点,第二次点击才能构建成功,这里用延时函数setTimeout来解决顺序问题,代码如下

<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>Map Example</title>
		<script type="text/javascript">
			var em = null,
				map = null,
				start_point=null,end_point=null;
			// H5 plus事件处理
			function plusReady() {
				// 确保DOM解析完成
				if (!em || !window.plus || map) {
					return
				};
				map = new plus.maps.Map("map");
				map.centerAndZoom(new plus.maps.Point(116.3977, 39.906016), 12);
			}
			if (window.plus) {
				plusReady();
			} else {
				document.addEventListener("plusready", plusReady, false);
			}
			// DOMContentloaded事件处理
			document.addEventListener("DOMContentLoaded", function() {
				em = document.getElementById("map");
				plusReady();
			}, false);
			// 获取用户的当前位置信息
			function getUserLocation() {
				map.getUserLocation(function(state, point) {
					if (0 == state) {
						// alert( JSON.stringify(point) );
						map.centerAndZoom(point, 18);
						start_point = new plus.maps.Point(point.getLng(), point.getLat());
					} else {
						alert("Failed!");
					}
				});
			}
			function drivingSearch() {
				if(start_point)
				{
					var searchObj = new plus.maps.Search(map);
				searchObj.onRouteSearchComplete = function(state, result) {
					console.log("onRouteSearchComplete: " + state + " , " + result.routeNumber);
					if (state == 0) {
						if (result.routeNumber <= 0) {
							alert("没有检索到结果");
						}
						for (var i = 0; i < result.routeNumber; i++) {
							map.addOverlay(result.getRoute(i));
						}
					} else {
						alert("检索失败");
					}
				}
				end_point = new plus.maps.Point(116.3977, 39.906016);
				searchObj.drivingSearch(start_point, "", end_point, "");
				}
				else 
				{
					setTimeout(drivingSearch,500);
				}
				
			}
			function daoahng(){
				getUserLocation();
				drivingSearch();
			}
		</script>
		<style type="text/css">
			* {
				-webkit-user-select: none;
			}

			html,
			body {
				margin: 0px;
				padding: 0px;
				height: 100%;
			}
			#map {
				width: 100%;
				position: fixed;
				top: 100px;
				bottom: 0px;
				line-height: 200px;
				text-align: center;
				background: #FFFFFF;
			}
		</style>
	</head>
	<body>
		<button type="button" onclick="daoahng()">导航</button>
		<div id="map">地图加载中...</div>

	</body>
</html>

那么进一步的,让地图自动加载定位以及规划路径的呢?代码如下

<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8" />
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>Map Example</title>
		<script type="text/javascript">
			var map = null,
				start_point = null,
				end_point = null;

			function drivingSearch() {
				if (start_point) {

					var searchObj = new plus.maps.Search(map);
					end_point = new plus.maps.Point(116.3977, 39.906016);

					searchObj.onRouteSearchComplete = function(state, result) {
						console.log("onRouteSearchComplete: " + state + " , " + result.routeNumber);
						if (state == 0) {
							if (result.routeNumber <= 0) {}
							for (var i = 0; i < result.routeNumber; i++) {
								map.addOverlay(result.getRoute(i));

							}
						} else {
							// alert("检索失败");
						}
					}
					searchObj.drivingSearch(start_point, "", end_point, "")

				}
				else 
				{
					setTimeout(drivingSearch,500);
				}
				

			}

			function getUserLocation() {
				map.getUserLocation(function(state, point) {
					if (0 == state) {
						start_point = new plus.maps.Point(point.getLng(), point.getLat());

					} else {
						alert("定位失败,请确保GPS已经开启"); //没开定位
					}
				});
			}
			// 创建地图控件
			function createMap() {
				if (window.plus && !map) {
					map = new plus.maps.Map("map");
					plus.webview.currentWebview().append(map);
					getUserLocation();
					drivingSearch();
				} else {
					setTimeout(createMap, 500);
				}
			}
			window.load=createMap();
		</script>
		<style type="text/css">
			* {
				-webkit-user-select: none;
			}

			html,
			body {
				margin: 0px;
				padding: 0px;
				height: 100%;
			}
			#map {
				width: 100%;
				position: fixed;
				top: 100px;
				bottom: 0px;
				line-height: 200px;
				text-align: center;
				background: #FFFFFF;
			}
		</style>
	</head>
	<body>
		<div id="map">地图加载中...</div>

	</body>
</html>

判断游戏啊window.plus 以及地图是否创建即可

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值