var map = new BMap.Map("map"); /**采集到的GPS点**/ var Points = [ {"id": "1", "lng": 121.554586, "lat": 29.813444}, {"id": "2", "lng": 121.564567, "lat": 29.811347}, {"id": "3", "lng": 121.575346, "lat": 29.820528}, {"id": "4", "lng": 121.553464, "lat": 29.82363}, {"id": "5", "lng": 121.54078, "lat": 29.820371}, ] var p = Math.ceil(Points.length / 2); map.centerAndZoom(new BMap.Point(Points[p].lng, Points[p].lat), 15); map.enableScrollWheelZoom(); //路线; var driving; for (var i in PointArr) { //起点 if (i == 0) { addMarker(new BMap.Point(Points[i].lng, Points[i].lat), "起点"); continue; } //线路; driving = new BMap.DrivingRoute(map, { renderOptions: {map: map, autoViewport: false}, onMarkersSet: function (routes) { map.removeOverlay(routes[0].marker); //删除API自带起点 map.removeOverlay(routes[1].marker); //删除API自带终点 } }); //终点 driving.search(new BMap.Point(Points[i - 1].lng, Points[i - 1].lat), new BMap.Point(Points[i].lng, Points[i].lat)); if (i == Points.length - 1) { addMarker(new BMap.Point(Points[i].lng, Points[i].lat), "终点"); } } /** * 标记 * @param {Object} point */ function addMarker(point, name) { var marker = new BMap.Marker(point); var label = new BMap.Label(name, { offset: new BMap.Size(20, -10) }); marker.setLabel(label); map.addOverlay(marker); }
Done!