<div id="container"></div>
<script>
var map = new BMap.Map("container");
var points = [@ViewBag.trackpos];
var times = '@ViewBag.times';
times = times.split(",");
var list = [];
for (var i = 0; i < points.length; i += 2) list.push(new BMap.Point(points[i], points[i + 1])); if (list.length > 0)
{
map.centerAndZoom(list[0], 15);
map.addControl(new BMap.NavigationControl({ anchor: BMAP_ANCHOR_TOP_LEFT }));
map.addControl(new BMap.ScaleControl());
map.addControl(new BMap.OverviewMapControl({ isOpen: true }));
map.addControl(new BMap.MapTypeControl());
var panorama = new BMap.PanoramaControl();
panorama.setOffset(new BMap.Size(10, 60));
map.addControl(panorama);
map.enableScrollWheelZoom(true);
for (c = 0; c < list.length; c++) {
var marker = new BMap.Marker(list[c]);
var content = times[c];
map.addOverlay(marker);
marker.setAnimation(BMAP_ANIMATION_BOUNCE);
var label = new BMap.Label(c + 1, { offset: new BMap.Size(20, -10) });
marker.setLabel(label);
addClickHandler(content, marker);
}
var driving = new BMap.DrivingRoute(map, {
renderOptions: { map: map, autoViewport: true },
onPolylinesSet: function (routes) {
searchRoute = routes[0].getPolyline();
map.addOverlay(searchRoute);
},
onMarkersSet: function (routes) {
map.removeOverlay(routes[0].marker);
map.removeOverlay(routes[1].marker);
}
});
driving.setSearchCompleteCallback(function (results) {
if (driving.getStatus() == BMAP_STATUS_SUCCESS) {
var path, k;
var plan = driving.getResults().getPlan(0);
var pts = [];
var ptsArr = [];
for (var j = 0; j < plan.getNumRoutes(); j++) {
path = plan.getRoute(j).getPath();
for (k = 0; k < path.length; k++)
pts.push(path[k]);
var polyline = new BMap.Polyline(path);
ptsArr = ptsArr.concat(polyline);
polyline.setStrokeColor("blue");
polyline.setStrokeStyle("dashed");
map.addOverlay(polyline);
}
var marker = new BMap.Marker(pts[0], { icon: new BMap.Icon("http://developer.baidu.com/map/jsdemo/img/Mario.png", new BMap.Size(32, 70), { imageOffset: new BMap.Size(0, 0) }) });
map.addOverlay(marker);
var n = 0;
function move() {
if (n < pts.length) {
marker.setPosition(pts[n]);
n++;
setTimeout(move, 500);
}
}
setTimeout(move, 500);
}
var m1 = new BMap.Marker(list[0]);
var m2 = new BMap.Marker(list[list.length - 1]);
var lab1 = new BMap.Label("起点", { position: list[0]});
var lab2 = new BMap.Label("终点", { position: list[list.length - 1]}); map.addOverlay(m1);
map.addOverlay(m2);
map.addOverlay(lab1);
map.addOverlay(lab2);
});
var group = Math.floor(list.length / 11);
var mode = list.length % 11;
for (var i = 0; i < group; i++) {
var waypoints = list.slice(i * 11 + 1, (i + 1) * 11);
//这里的终点如果是11的倍数的时候,数组可是取不到最后一位的,所以要注意终点-1
driving.search(list[i * 11], list[(i + 1) * 11 - 1], { waypoints: waypoints });
}
if (mode != 0) {
var waypoints = list.slice(group * 11, list.length - 1);
driving.search(list[group * 11], list[list.length - 1], { waypoints: waypoints });
}
var opts = {
width: 60,
height: 20,
title: "定位时间",
enableMessage: true
};
function addClickHandler(content, marker) {
marker.addEventListener("click", function (e) {
openInfo(content, e)
}
);
}
function openInfo(content, e) {
var p = e.target;
var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
var infoWindow = new BMap.InfoWindow(content, opts);
map.openInfoWindow(infoWindow, point);
}
}
else
alert("没有数据!"); </script>
添加import android.support.v7.app.AppCompatActivity 添加上海为途径点
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章