Skip to content

Commit

Permalink
Feat/npm ol (#3683)
Browse files Browse the repository at this point in the history
feat: use `npm` version of open layers map
  • Loading branch information
chmelevskij authored Dec 20, 2023
1 parent daf7e4d commit f8f9a78
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 146 deletions.
6 changes: 4 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,10 @@ function dist_src() {
}

function dist_node_modules_css() {
return gulp.src('./**/*.min.css')
.pipe(gulp.dest(DIST_DIR));
return gulp.src([
'./**/*.min.css',
'./node_modules/ol/ol.css',
], { base: 'node_modules' }).pipe(gulp.dest(`${DIST_DIR}node_modules/`));
}

function dist_less() {
Expand Down
2 changes: 0 additions & 2 deletions libraries/openlayers/ol.css

This file was deleted.

1 change: 0 additions & 1 deletion libraries/openlayers/ol.css.map

This file was deleted.

8 changes: 0 additions & 8 deletions libraries/openlayers/ol.js

This file was deleted.

1 change: 0 additions & 1 deletion libraries/openlayers/ol.js.map

This file was deleted.

17 changes: 1 addition & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,6 @@
"type": "git",
"url": "github.com/betaflight/betaflight-configurator"
},
"webview": {
"partitions": [
{
"name": "map",
"accessible_resources": [
"tabs/map.html",
"js/tabs/map.js",
"/js/libraries/openlayers/ol.css",
"/js/libraries/openlayers/ol.js",
"/images/icons/cf_icon_position.png",
"/images/icons/cf_icon_position_mag.png",
"/images/icons/cf_icon_position_nofix.png"
]
}
]
},
"author": "The Betaflight open source project.",
"license": "GPL-3.0",
"engines": {
Expand Down Expand Up @@ -78,6 +62,7 @@
"multicast-dns": "^7.2.5",
"multiple-select": "^1.6.0",
"nw-vue-devtools-prebuilt": "^0.0.10",
"ol": "^8.2.0",
"select2": "^4.0.13",
"semver-min": "^0.7.2",
"short-unique-id": "^4.4.4",
Expand Down
8 changes: 8 additions & 0 deletions src/css/tabs/gps.less
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
@import '/node_modules/ol/ol.css';

#map {
height: 100%;
margin: 0px;
padding: 0px;
}

.tab-gps {
.GPS_signal_strength {
table {
Expand Down
49 changes: 23 additions & 26 deletions src/js/tabs/gps.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import $ from 'jquery';
import { have_sensor } from "../sensor_helpers";
import { mspHelper } from '../msp/MSPHelper';
import { updateTabList } from '../utils/updateTabList';
import { initMap } from './map';
import { fromLonLat } from "ol/proj";

const gps = {};

Expand Down Expand Up @@ -204,6 +206,15 @@ gps.initialize = async function (callback) {
$('div.mag_declination').hide();
}

const {
mapView,
iconStyleMag,
iconStyleGPS,
iconStyleNoFix,
iconFeature,
iconGeometry,
} = initMap();

// End GPS Configuration

function update_ui() {
Expand Down Expand Up @@ -323,42 +334,34 @@ gps.initialize = async function (callback) {
}
}

const message = {
action: 'center',
lat: lat,
lon: lon,
heading: imuHeadingRadians,
};

frame = document.getElementById('map');
if (navigator.onLine) {
$('#connect').hide();

if (FC.GPS_DATA.fix) {
gpsWasFixed = true;
message.action = hasMag ? 'centerMag' : 'center';
if (!!frame.contentWindow) {
frame.contentWindow.postMessage(message, '*');
}

(hasMag ? iconStyleMag : iconStyleGPS)
.getImage()
.setRotation(imuHeadingRadians);
iconFeature.setStyle(hasMag ? iconStyleMag : iconStyleGPS);
const center = fromLonLat([lon, lat]);
mapView.setCenter(center);
iconGeometry.setCoordinates(center);

$('#loadmap').show();
$('#waiting').hide();
} else if (!gpsWasFixed) {
$('#loadmap').hide();
$('#waiting').show();
} else {
message.action = 'nofix';
if (!!frame.contentWindow) {
frame.contentWindow.postMessage(message, '*');
}
iconFeature.setStyle(iconStyleNoFix);
}
} else {
gpsWasFixed = false;
set_offline();
}
}

let frame = document.getElementById('map');

// enable data pulling
GUI.interval_add('gps_pull', function gps_update() {
get_raw_gps_data();
Expand All @@ -385,18 +388,12 @@ gps.initialize = async function (callback) {

$('#zoom_in').click(function() {
console.log('zoom in');
const message = {
action: 'zoom_in',
};
frame.contentWindow.postMessage(message, '*');
mapView.setZoom(mapView.getZoom() + 1);
});

$('#zoom_out').click(function() {
console.log('zoom out');
const message = {
action: 'zoom_out',
};
frame.contentWindow.postMessage(message, '*');
mapView.setZoom(mapView.getZoom() - 1);
});

$('a.save').on('click', async function() {
Expand Down
100 changes: 36 additions & 64 deletions src/js/tabs/map.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,93 @@
import { View, Map, Feature } from "ol";
import { fromLonLat } from "ol/proj";
import { Tile, Vector as LayerVector } from "ol/layer";
import { OSM, Vector as SourceVector } from "ol/source";
import { Icon, Style } from "ol/style";
import { Point } from "ol/geom";

const DEFAULT_ZOOM = 16,
DEFAULT_LON = 0,
DEFAULT_LAT = 0,
ICON_IMAGE_GPS = '/images/icons/cf_icon_position.png',
ICON_IMAGE_MAG = '/images/icons/cf_icon_position_mag.png',
ICON_IMAGE_NOFIX = '/images/icons/cf_icon_position_nofix.png';
DEFAULT_LON = 0,
DEFAULT_LAT = 0,
ICON_IMAGE_GPS = "/images/icons/cf_icon_position.png",
ICON_IMAGE_MAG = "/images/icons/cf_icon_position_mag.png",
ICON_IMAGE_NOFIX = "/images/icons/cf_icon_position_nofix.png";

let iconGeometry,
map,
mapView,
iconStyleGPS,
iconStyleMag,
iconStyleNoFix,
iconFeature;
export function initMap() {
const lonLat = fromLonLat([DEFAULT_LON, DEFAULT_LAT]);

window.onload = initializeMap;

function initializeMap() {

const lonLat = ol.proj.fromLonLat([DEFAULT_LON, DEFAULT_LAT]);

mapView = new ol.View({
const mapView = new View({
center: lonLat,
zoom: DEFAULT_ZOOM,
});

map = new ol.Map({
target: 'map-canvas',
const map = new Map({
target: "map",
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
new Tile({
source: new OSM(),
}),
],
view: mapView,
controls: [],
});

const iconGPS = new ol.style.Icon({
const iconGPS = new Icon({
anchor: [0.5, 1],
opacity: 1,
scale: 0.5,
src: ICON_IMAGE_GPS,
});

const iconMag = new ol.style.Icon({
const iconMag = new Icon({
anchor: [0.5, 1],
opacity: 1,
scale: 0.5,
src: ICON_IMAGE_MAG,
});

const iconNoFix = new ol.style.Icon({
const iconNoFix = new Icon({
anchor: [0.5, 1],
opacity: 1,
scale: 0.5,
src: ICON_IMAGE_NOFIX,
});

iconStyleGPS = new ol.style.Style({
const iconStyleGPS = new Style({
image: iconGPS,
});

iconStyleMag = new ol.style.Style({
const iconStyleMag = new Style({
image: iconMag,
});

iconStyleNoFix = new ol.style.Style({
const iconStyleNoFix = new Style({
image: iconNoFix,
});

iconGeometry = new ol.geom.Point(lonLat);
const iconGeometry = new Point(lonLat);

iconFeature = new ol.Feature({
const iconFeature = new Feature({
geometry: iconGeometry,
});

iconFeature.setStyle(iconStyleGPS);

const vectorSource = new ol.source.Vector({
const vectorSource = new SourceVector({
features: [iconFeature],
});

const currentPositionLayer = new ol.layer.Vector({
const currentPositionLayer = new LayerVector({
source: vectorSource,
});

map.addLayer(currentPositionLayer);

window.addEventListener('message', processMapEvents);
}

function processMapEvents(e) {
try {
switch (e.data.action) {
case 'zoom_in':
mapView.setZoom(mapView.getZoom() + 1);
break;

case 'zoom_out':
mapView.setZoom(mapView.getZoom() - 1);
break;

case 'center':
case 'centerMag':
const hasMag = e.data.action == 'centerMag';
(hasMag ? iconStyleMag : iconStyleGPS).getImage().setRotation(e.data.heading);
iconFeature.setStyle(hasMag ? iconStyleMag : iconStyleGPS);
const center = ol.proj.fromLonLat([e.data.lon, e.data.lat]);
mapView.setCenter(center);
iconGeometry.setCoordinates(center);
break;

case 'nofix':
iconFeature.setStyle(iconStyleNoFix);
break;
}
} catch (err) {
console.error('Map error', err);
}
return {
mapView,
iconStyleMag,
iconStyleGPS,
iconStyleNoFix,
iconFeature,
iconGeometry,
};
}
2 changes: 1 addition & 1 deletion src/tabs/gps.html
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
<div class="info" i18n="gpsMapMessage2"></div>
</div>
<div id="loadmap">
<webview id="map" class="map" src="tabs/map.html" partition="persist:map"></webview>
<div id="map" class="map"> </div>
<div class="controls">
<a href="#" id="zoom_in">+</a>
<a href="#" id="zoom_out"></a>
Expand Down
23 changes: 0 additions & 23 deletions src/tabs/map.html

This file was deleted.

Loading

0 comments on commit f8f9a78

Please sign in to comment.