2018-03-28 07:18:25 +08:00
|
|
|
|
|
|
|
const leaflet = require('leaflet');
|
2018-03-13 06:30:52 +08:00
|
|
|
|
2018-03-14 22:07:41 +08:00
|
|
|
import draw_base_map from './base_map'
|
2018-04-07 06:10:45 +08:00
|
|
|
import { addWMSLayer } from './helpers';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ACTUAL_ROUTE_COLOR,
|
|
|
|
PLAN_ROUTE_COLOR,
|
|
|
|
CIRCLE_COLOR
|
|
|
|
} from './config'
|
2018-03-13 06:30:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show some popup text when a feature is clicked on
|
|
|
|
* @param feature
|
|
|
|
* @param layer
|
|
|
|
*/
|
|
|
|
export const onFeaturePointClick = (feature, layer) => {
|
2018-03-28 07:18:25 +08:00
|
|
|
let popup_html = '';
|
2018-03-14 22:07:41 +08:00
|
|
|
if (feature.properties && feature.properties.popup) {
|
|
|
|
popup_html += feature.properties.popup
|
|
|
|
}
|
2018-03-13 06:30:52 +08:00
|
|
|
|
2018-03-14 22:07:41 +08:00
|
|
|
layer.bindPopup(popup_html)
|
2018-03-28 07:18:25 +08:00
|
|
|
};
|
2018-03-13 06:30:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show each point as a marker
|
|
|
|
* @param feature
|
|
|
|
* @param latlng
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
export const pointToLayer = (feature, latlng) => {
|
2018-03-14 22:07:41 +08:00
|
|
|
return leaflet.circleMarker(latlng, {
|
2018-04-07 06:10:45 +08:00
|
|
|
radius: 5,
|
|
|
|
fillColor: CIRCLE_COLOR,
|
2018-03-14 22:07:41 +08:00
|
|
|
color: '#000',
|
|
|
|
weight: 1,
|
|
|
|
opacity: 1,
|
|
|
|
fillOpacity: 0.8
|
|
|
|
})
|
|
|
|
}
|
2018-03-13 06:30:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param opts
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
export default (opts) => {
|
|
|
|
|
2018-03-28 07:18:25 +08:00
|
|
|
opts = Object.assign({
|
2018-03-14 22:07:41 +08:00
|
|
|
route_points: null,
|
|
|
|
planned_route_line: null,
|
|
|
|
actual_route_points: null,
|
|
|
|
actual_route_line: null,
|
|
|
|
render_elem: 'map',
|
2018-04-01 04:57:30 +08:00
|
|
|
metar_wms: {
|
|
|
|
url: '',
|
|
|
|
params: {}
|
|
|
|
},
|
2018-03-28 07:18:25 +08:00
|
|
|
}, opts);
|
2018-03-14 22:07:41 +08:00
|
|
|
|
2018-04-01 04:57:30 +08:00
|
|
|
console.log(opts);
|
2018-03-14 22:07:41 +08:00
|
|
|
|
2018-04-01 04:57:30 +08:00
|
|
|
let map = draw_base_map(opts);
|
|
|
|
|
|
|
|
if (opts.metar_wms.url !== '') {
|
|
|
|
addWMSLayer(map, opts.metar_wms);
|
|
|
|
}
|
2018-03-14 22:07:41 +08:00
|
|
|
|
|
|
|
let geodesicLayer = leaflet.geodesic([], {
|
2018-04-07 06:10:45 +08:00
|
|
|
weight: 4,
|
2018-03-14 22:07:41 +08:00
|
|
|
opacity: 0.9,
|
|
|
|
color: PLAN_ROUTE_COLOR,
|
|
|
|
steps: 50,
|
|
|
|
wrap: false,
|
2018-04-07 06:10:45 +08:00
|
|
|
}).addTo(map);
|
2018-03-14 22:07:41 +08:00
|
|
|
|
2018-04-07 06:10:45 +08:00
|
|
|
geodesicLayer.geoJson(opts.planned_route_line);
|
2018-03-14 22:07:41 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
map.fitBounds(geodesicLayer.getBounds())
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the route points after
|
|
|
|
if (opts.route_points !== null) {
|
|
|
|
let route_points = leaflet.geoJSON(opts.route_points, {
|
|
|
|
onEachFeature: onFeaturePointClick,
|
|
|
|
pointToLayer: pointToLayer,
|
|
|
|
style: {
|
|
|
|
'color': PLAN_ROUTE_COLOR,
|
2018-04-07 06:10:45 +08:00
|
|
|
'weight': 3,
|
2018-03-14 22:07:41 +08:00
|
|
|
'opacity': 0.65,
|
|
|
|
},
|
2018-04-07 06:10:45 +08:00
|
|
|
});
|
2018-03-14 22:07:41 +08:00
|
|
|
|
2018-04-07 06:10:45 +08:00
|
|
|
route_points.addTo(map);
|
2018-03-14 22:07:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* draw the actual route
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (opts.actual_route_line !== null && opts.actual_route_line.features.length > 0) {
|
2018-03-13 06:30:52 +08:00
|
|
|
let geodesicLayer = leaflet.geodesic([], {
|
2018-04-07 06:10:45 +08:00
|
|
|
weight: 3,
|
2018-03-14 22:07:41 +08:00
|
|
|
opacity: 0.9,
|
|
|
|
color: ACTUAL_ROUTE_COLOR,
|
|
|
|
steps: 50,
|
|
|
|
wrap: false,
|
2018-04-07 06:10:45 +08:00
|
|
|
}).addTo(map);
|
2018-03-13 06:30:52 +08:00
|
|
|
|
2018-04-07 06:10:45 +08:00
|
|
|
geodesicLayer.geoJson(opts.actual_route_line);
|
2018-03-13 06:30:52 +08:00
|
|
|
|
|
|
|
try {
|
2018-03-14 22:07:41 +08:00
|
|
|
map.fitBounds(geodesicLayer.getBounds())
|
2018-03-13 06:30:52 +08:00
|
|
|
} catch (e) {
|
2018-03-14 22:07:41 +08:00
|
|
|
console.log(e)
|
2018-03-13 06:30:52 +08:00
|
|
|
}
|
2018-03-14 22:07:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.actual_route_points !== null && opts.actual_route_points.features.length > 0) {
|
|
|
|
let route_points = leaflet.geoJSON(opts.actual_route_points, {
|
|
|
|
onEachFeature: onFeaturePointClick,
|
|
|
|
pointToLayer: pointToLayer,
|
|
|
|
style: {
|
|
|
|
'color': ACTUAL_ROUTE_COLOR,
|
2018-04-07 06:10:45 +08:00
|
|
|
'weight': 3,
|
2018-03-14 22:07:41 +08:00
|
|
|
'opacity': 0.65,
|
|
|
|
},
|
2018-04-07 06:10:45 +08:00
|
|
|
});
|
2018-03-14 22:07:41 +08:00
|
|
|
|
|
|
|
route_points.addTo(map)
|
|
|
|
}
|
2018-03-13 06:30:52 +08:00
|
|
|
};
|