diff --git a/js/index.js b/js/index.js index 0c7e86f..e70527e 100644 --- a/js/index.js +++ b/js/index.js @@ -312,7 +312,7 @@ BR.stravaSegments(map, layersControl); } - BR.tracksLoader(map, layersControl, routing); + BR.tracksLoader(map, layersControl, routing, pois); BR.routeLoader(map, layersControl, routing, pois); diff --git a/js/plugin/RouteLoaderConverter.js b/js/plugin/RouteLoaderConverter.js index ce3c278..9ff518e 100644 --- a/js/plugin/RouteLoaderConverter.js +++ b/js/plugin/RouteLoaderConverter.js @@ -257,11 +257,7 @@ BR.routeLoader = function(map, layersControl, routing, pois) { }, addTrackOverlay: function(geoJSON) { - this._trackLayer = L.geoJSON(geoJSON, { - filter: function(geoJsonFeature) { - return !(geoJsonFeature.type == 'Feature' && geoJsonFeature.geometry.type == 'Point'); - } - }).addTo(map); + this._trackLayer = L.geoJSON(geoJSON, BR.Track.getGeoJsonOptions(layersControl)).addTo(map); layersControl.addOverlay(this._trackLayer, this._layerName); @@ -320,15 +316,7 @@ BR.routeLoader = function(map, layersControl, routing, pois) { if (!this._bounds) map.fitBounds(this._bounds); if (this._options.showPointAsPoi) { - turf.featureEach(this._currentGeoJSON, function(feature, idx) { - if (turf.getType(feature) == 'Point') { - var coord = turf.getCoord(feature); - var latlng = L.GeoJSON.coordsToLatLng(coord); - var name = ''; - if (feature.properties && feature.properties.name) name = feature.properties.name; - pois.addMarker(latlng, name); - } - }); + BR.Track.addPoiMarkers(pois, this._currentGeoJSON); } }, diff --git a/js/plugin/TracksLoader.js b/js/plugin/TracksLoader.js index ab84422..d78c888 100644 --- a/js/plugin/TracksLoader.js +++ b/js/plugin/TracksLoader.js @@ -1,25 +1,16 @@ -BR.tracksLoader = function(map, layersControl, routing) { +BR.tracksLoader = function(map, layersControl, routing, pois) { + // proxy to L.geoJSON factory function, to get hold of raw GeoJSON object + var createGeoJsonLayer = function(geojson, options) { + BR.Track.addPoiMarkers(pois, geojson); + + return L.geoJSON(geojson, options); + }; + TracksLoader = L.Control.FileLayerLoad.extend({ options: { - layer: L.geoJson, - layerOptions: { - style: function(geoJsonFeature) { - var currentLayerId = layersControl.getActiveBaseLayer().layer.id; - return { - color: currentLayerId === 'cyclosm' ? 'yellow' : 'blue', - weight: 4 - }; - }, - interactive: false, - pointToLayer: function(geoJsonPoint, latlng) { - return L.marker(latlng, { - interactive: false, - opacity: 0.7, - // prevent being on top of route markers - zIndexOffset: -1000 - }); - } - }, + // `layer` allows to use a customized version of `L.geoJson` with the same signature + layer: createGeoJsonLayer, + layerOptions: BR.Track.getGeoJsonOptions(layersControl), addToMap: false, // File size limit in kb (default: 1024) ? fileSizeLimit: 1024, @@ -117,6 +108,7 @@ BR.tracksLoader = function(map, layersControl, routing) { error: err && err.message ? err.message : err }) ); + console.error(err); }); return tracksLoaderControl; diff --git a/js/util/Track.js b/js/util/Track.js new file mode 100644 index 0000000..f806131 --- /dev/null +++ b/js/util/Track.js @@ -0,0 +1,83 @@ +/** + * Track loading commons + */ +BR.Track = { + /** + * Returns common options for styling and appearance of tracks + * + * @param {BR.ControlLayers} layersControl Layers control instance + * + * @returns {Object} to pass as `options` parameter to `L.geoJson` + */ + getGeoJsonOptions: function(layersControl) { + return { + style: function(geoJsonFeature) { + var currentLayerId = layersControl.getActiveBaseLayer().layer.id; + return { + color: currentLayerId === 'cyclosm' ? 'yellow' : 'blue', + weight: 4 + }; + }, + interactive: false, + filter: function(geoJsonFeature) { + // remove POIs, added separately + return !BR.Track.isPoiPoint(geoJsonFeature); + }, + pointToLayer: function(geoJsonPoint, latlng) { + // route waypoint (type=from/via/to) + return L.marker(latlng, { + interactive: false, + opacity: 0.7, + // prevent being on top of route markers + zIndexOffset: -1000 + }); + } + }; + }, + + /** + * Add Points in the passed `geoJson` as POI markers, except route waypoints (type=from|via|to) + * + * @param {BR.PoiMarkers} pois POI control instance + * @param {Object} geoJson GeoJSON object + */ + addPoiMarkers: function(pois, geoJson) { + turf.featureEach(geoJson, function(feature, idx) { + if (BR.Track.isPoiPoint(feature)) { + var coord = turf.getCoord(feature); + var latlng = L.GeoJSON.coordsToLatLng(coord); + var name = ''; + if (feature.properties && feature.properties.name) name = feature.properties.name; + pois.addMarker(latlng, name); + } + }); + }, + + /** + * Checks if the passed GeoJSON Point feature is a route waypoint. + * + * Route points are exported e.g. as GPX `wpt` with a `type=from|via|to` property + * if the "waypoints" option is checked in the Export dialog. + * + * @param {Object} geoJsonPointFeature GeoJSON Point feature + */ + isRouteWaypoint: function(geoJsonPointFeature) { + var props = geoJsonPointFeature.properties; + if (props && props.type) { + var wptType = props.type; + if (wptType === 'from' || wptType === 'via' || wptType === 'to') { + return true; + } + } + return false; + }, + + /** + * Checks if the passed GeoJSON feature should be added as a POI + * + * @param {Object} geoJsonFeature GeoJSON feature + */ + isPoiPoint: function(geoJsonFeature) { + return turf.getType(geoJsonFeature) === 'Point' && !BR.Track.isRouteWaypoint(geoJsonFeature); + } +};