以上两个为该项目的基础,首先,先进行简单的结合,
<!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 以及地图是否创建即可