From 0899898c9b618162e7ae24b4f15a4a8427325b25 Mon Sep 17 00:00:00 2001 From: brunesto Date: Wed, 21 Oct 2020 09:39:05 +0200 Subject: [PATCH 01/11] added copyright label + sample mapbox provider --- .../java/com/gluonhq/impl/maps/BaseMap.java | 6 +- .../java/com/gluonhq/impl/maps/MapTile.java | 6 +- .../maps/tile/osm/CachedOsmTileRetriever.java | 18 +-- .../impl/maps/tile/osm/OsmTileRetriever.java | 13 +- .../main/java/com/gluonhq/maps/MapView.java | 20 ++- .../com/gluonhq/maps/tile/TileRetriever.java | 1 + .../maps/tile/TileRetrieverProvider.java | 118 +++++++++--------- maps/src/main/java/module-info.java | 1 + .../maps/samples/mobile/MobileSample.java | 31 ++++- 9 files changed, 133 insertions(+), 81 deletions(-) diff --git a/maps/src/main/java/com/gluonhq/impl/maps/BaseMap.java b/maps/src/main/java/com/gluonhq/impl/maps/BaseMap.java index 79125d4..eb95907 100644 --- a/maps/src/main/java/com/gluonhq/impl/maps/BaseMap.java +++ b/maps/src/main/java/com/gluonhq/impl/maps/BaseMap.java @@ -29,6 +29,7 @@ import com.gluonhq.maps.MapPoint; import com.gluonhq.maps.MapView; +import com.gluonhq.maps.tile.TileRetriever; import javafx.application.Platform; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; @@ -95,7 +96,10 @@ public class BaseMap extends Group { private final ChangeListener resizeListener = (o, oldValue, newValue) -> markDirty(); private ChangeListener sceneListener; - public BaseMap() { + public TileRetriever tileRetriever; + + public BaseMap(TileRetriever tileRetriever) { + this.tileRetriever=tileRetriever; for (int i = 0; i < tiles.length; i++) { tiles[i] = new HashMap<>(); } diff --git a/maps/src/main/java/com/gluonhq/impl/maps/MapTile.java b/maps/src/main/java/com/gluonhq/impl/maps/MapTile.java index 1a32b03..db3e484 100644 --- a/maps/src/main/java/com/gluonhq/impl/maps/MapTile.java +++ b/maps/src/main/java/com/gluonhq/impl/maps/MapTile.java @@ -27,7 +27,7 @@ */ package com.gluonhq.impl.maps; -import com.gluonhq.maps.tile.TileRetrieverProvider; + import com.gluonhq.maps.tile.TileRetriever; import javafx.beans.InvalidationListener; import javafx.beans.Observable; @@ -50,7 +50,7 @@ class MapTile extends Region { private static final Logger logger = Logger.getLogger( MapTile.class.getName() ); - private static final TileRetriever TILE_RETRIEVER = TileRetrieverProvider.getInstance().load(); +// private static final TileRetriever TILE_RETRIEVER = TileRetrieverProvider.getInstance().load(); final int myZoom; final long i, j; @@ -83,7 +83,7 @@ public boolean isCovering() { ImageView imageView = new ImageView(); imageView.setMouseTransparent(true); - Image tile = TILE_RETRIEVER.loadTile(myZoom, i, j); + Image tile = baseMap.tileRetriever.loadTile(myZoom, i, j); imageView.setImage(tile); this.progress = tile.progressProperty(); diff --git a/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java b/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java index 73996d1..237222a 100644 --- a/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java +++ b/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java @@ -46,16 +46,16 @@ import java.util.logging.Level; import java.util.logging.Logger; -public class CachedOsmTileRetriever extends OsmTileRetriever { +public abstract class CachedOsmTileRetriever extends OsmTileRetriever { private static final Logger logger = Logger.getLogger(CachedOsmTileRetriever.class.getName()); - private static final int TIMEOUT = 5000; + private static final int TIMEOUT = 5000; - static File cacheRoot; - static boolean hasFileCache; - static CacheThread cacheThread = null; + File cacheRoot; + boolean hasFileCache; + CacheThread cacheThread = null; - static { + { try { File storageRoot = StorageService.create() .flatMap(StorageService::getPrivateStorage) @@ -79,7 +79,7 @@ public class CachedOsmTileRetriever extends OsmTileRetriever { } } - private final static Executor EXECUTOR = Executors.newFixedThreadPool(2, runnable -> { + private final Executor EXECUTOR = Executors.newFixedThreadPool(2, runnable -> { Thread thread = new Thread(runnable); thread.setDaemon(true); return thread; @@ -112,7 +112,7 @@ public Image loadTile(int zoom, long i, long j) { * @param j * @return */ - static private Image fromFileCache(int zoom, long i, long j) { + private Image fromFileCache(int zoom, long i, long j) { if (!hasFileCache) { return null; } @@ -125,7 +125,7 @@ static private Image fromFileCache(int zoom, long i, long j) { return null; } - private static class CacheThread extends Thread { + private class CacheThread extends Thread { private boolean active = true; private String basePath; diff --git a/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/OsmTileRetriever.java b/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/OsmTileRetriever.java index 4cada08..fa4495d 100644 --- a/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/OsmTileRetriever.java +++ b/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/OsmTileRetriever.java @@ -30,9 +30,10 @@ import com.gluonhq.maps.tile.TileRetriever; import javafx.scene.image.Image; -public class OsmTileRetriever implements TileRetriever { +public abstract class OsmTileRetriever implements TileRetriever { - private static final String host = "http://tile.openstreetmap.org/"; + + static final String httpAgent; static { @@ -44,10 +45,10 @@ public class OsmTileRetriever implements TileRetriever { System.setProperty("http.agent", httpAgent); } - static String buildImageUrlString(int zoom, long i, long j) { - return host + zoom + "/" + i + "/" + j + ".png"; - } - + public abstract String buildImageUrlString(int zoom, long i, long j); + + + @Override public Image loadTile(int zoom, long i, long j) { String urlString = buildImageUrlString(zoom, i, j); diff --git a/maps/src/main/java/com/gluonhq/maps/MapView.java b/maps/src/main/java/com/gluonhq/maps/MapView.java index 27fe64c..f686086 100644 --- a/maps/src/main/java/com/gluonhq/maps/MapView.java +++ b/maps/src/main/java/com/gluonhq/maps/MapView.java @@ -29,15 +29,21 @@ import com.gluonhq.attach.util.Platform; import com.gluonhq.impl.maps.BaseMap; +import com.gluonhq.maps.tile.TileRetriever; import javafx.animation.Animation.Status; import javafx.animation.Interpolator; import javafx.animation.KeyFrame; import javafx.animation.KeyValue; import javafx.animation.Timeline; import javafx.geometry.Point2D; +import javafx.scene.control.Label; import javafx.scene.layout.Region; import javafx.scene.shape.Rectangle; import javafx.util.Duration; +import javafx.scene.layout.BackgroundFill; +import javafx.scene.paint.Color; +import javafx.scene.layout.Background; + import java.util.LinkedList; import java.util.List; @@ -58,12 +64,18 @@ public class MapView extends Region { private boolean zooming = false; private boolean enableDragging = false; + private Label label; + /** * Create a MapView component. */ - public MapView() { - baseMap = new BaseMap(); + public MapView(TileRetriever tileRetriever) { + baseMap = new BaseMap( tileRetriever); getChildren().add(baseMap); + label=new Label(tileRetriever.copyright()); + label.setBackground(new Background(new BackgroundFill(Color.rgb(126, 126, 126, 0.5),null,null))); + getChildren().add(label); + registerInputListeners(); baseMap.centerLat().addListener(o -> markDirty()); @@ -239,6 +251,10 @@ protected void layoutChildren() { } } super.layoutChildren(); + + label.setLayoutX(w-label.getWidth()); + label.setLayoutY(h-label.getHeight()); + dirty = false; // we need to get these values or we won't be notified on new changes diff --git a/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java b/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java index 6363f4a..08a7987 100644 --- a/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java +++ b/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java @@ -41,4 +41,5 @@ public interface TileRetriever { * @return an image representing the tile */ Image loadTile(int zoom, long i, long j); + String copyright(); } diff --git a/maps/src/main/java/com/gluonhq/maps/tile/TileRetrieverProvider.java b/maps/src/main/java/com/gluonhq/maps/tile/TileRetrieverProvider.java index 810dd84..2a0a0b7 100644 --- a/maps/src/main/java/com/gluonhq/maps/tile/TileRetrieverProvider.java +++ b/maps/src/main/java/com/gluonhq/maps/tile/TileRetrieverProvider.java @@ -1,59 +1,59 @@ -/* - * Copyright (c) 2018, Gluon - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package com.gluonhq.maps.tile; - -import com.gluonhq.impl.maps.tile.osm.CachedOsmTileRetriever; - -import java.util.Iterator; -import java.util.ServiceLoader; - -public class TileRetrieverProvider { - - private static TileRetrieverProvider provider; - public static synchronized TileRetrieverProvider getInstance() { - if (provider == null) { - provider = new TileRetrieverProvider(); - } - return provider; - } - - private final ServiceLoader loader; - - private TileRetrieverProvider() { - loader = ServiceLoader.load(TileRetriever.class); - } - - public TileRetriever load() { - Iterator tileRetrievers = loader.iterator(); - if (tileRetrievers.hasNext()) { - return tileRetrievers.next(); - } else { - return new CachedOsmTileRetriever(); - } - } -} +///* +// * Copyright (c) 2018, Gluon +// * +// * This program is free software: you can redistribute it and/or modify +// * it under the terms of the GNU General Public License as published by +// * the Free Software Foundation, either version 3 of the License, or +// * (at your option) any later version. +// * +// * This program is distributed in the hope that it will be useful, +// * but WITHOUT ANY WARRANTY; without even the implied warranty of +// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// * GNU General Public License for more details. +// * +// * You should have received a copy of the GNU General Public License +// * along with this program. If not, see . +// * +// * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +// * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +// * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +// * DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY +// * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES +// * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; +// * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +// * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// */ +//package com.gluonhq.maps.tile; +// +//import com.gluonhq.impl.maps.tile.osm.CachedOsmTileRetriever; +// +//import java.util.Iterator; +//import java.util.ServiceLoader; +// +//public class TileRetrieverProvider { +// +// private static TileRetrieverProvider provider; +// public static synchronized TileRetrieverProvider getInstance() { +// if (provider == null) { +// provider = new TileRetrieverProvider(); +// } +// return provider; +// } +// +// private final ServiceLoader loader; +// +// private TileRetrieverProvider() { +// loader = ServiceLoader.load(TileRetriever.class); +// } +// +// public TileRetriever load() { +// Iterator tileRetrievers = loader.iterator(); +// if (tileRetrievers.hasNext()) { +// return tileRetrievers.next(); +// } else { +// return new CachedOsmTileRetriever(); +// } +// } +//} diff --git a/maps/src/main/java/module-info.java b/maps/src/main/java/module-info.java index 1851bdd..48f603a 100644 --- a/maps/src/main/java/module-info.java +++ b/maps/src/main/java/module-info.java @@ -37,4 +37,5 @@ exports com.gluonhq.maps; exports com.gluonhq.maps.tile; + exports com.gluonhq.impl.maps.tile.osm; } \ No newline at end of file diff --git a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java index f98fa90..8434d73 100644 --- a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java +++ b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java @@ -52,6 +52,8 @@ import javafx.scene.paint.Color; import javafx.scene.shape.Circle; +import com.gluonhq.impl.maps.tile.osm.CachedOsmTileRetriever; +import com.gluonhq.maps.tile.TileRetriever; public class MobileSample extends Application { private static final Logger LOGGER = Logger.getLogger(MobileSample.class.getName()); @@ -68,7 +70,34 @@ public class MobileSample extends Application { @Override public void start(Stage stage) { - MapView view = new MapView(); + TileRetriever osm=new CachedOsmTileRetriever() { + public String buildImageUrlString(int zoom, long i, long j) { + return "http://tile.openstreetmap.org/" +zoom+"/"+ i + "/" + j + ".png"; + + } + public String copyright() { + return "Map © OpenStreetMap contributors CC-BY-SA -- Please switch to another tile provider "; + } + }; + + TileRetriever mapBox=new CachedOsmTileRetriever() { + public String buildImageUrlString(int zoom, long i, long j) { + String url= "https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token=XXXXXX"; + return url; + } + public String copyright() { + return "Map data © OpenStreetMap contributors CC-BY-SA, Imagery © Mapbox"; + } + }; + + + MapView view = new MapView(osm); + + + + + + view.addLayer(positionLayer()); view.setZoom(3); Scene scene; From a411ebd6684212d46a63276e4715220f0b8c9b72 Mon Sep 17 00:00:00 2001 From: brunesto Date: Wed, 21 Oct 2020 09:50:44 +0200 Subject: [PATCH 02/11] rm --- .../maps/tile/TileRetrieverProvider.java | 59 ------------------- 1 file changed, 59 deletions(-) delete mode 100644 maps/src/main/java/com/gluonhq/maps/tile/TileRetrieverProvider.java diff --git a/maps/src/main/java/com/gluonhq/maps/tile/TileRetrieverProvider.java b/maps/src/main/java/com/gluonhq/maps/tile/TileRetrieverProvider.java deleted file mode 100644 index 2a0a0b7..0000000 --- a/maps/src/main/java/com/gluonhq/maps/tile/TileRetrieverProvider.java +++ /dev/null @@ -1,59 +0,0 @@ -///* -// * Copyright (c) 2018, Gluon -// * -// * This program is free software: you can redistribute it and/or modify -// * it under the terms of the GNU General Public License as published by -// * the Free Software Foundation, either version 3 of the License, or -// * (at your option) any later version. -// * -// * This program is distributed in the hope that it will be useful, -// * but WITHOUT ANY WARRANTY; without even the implied warranty of -// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// * GNU General Public License for more details. -// * -// * You should have received a copy of the GNU General Public License -// * along with this program. If not, see . -// * -// * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// * DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY -// * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// */ -//package com.gluonhq.maps.tile; -// -//import com.gluonhq.impl.maps.tile.osm.CachedOsmTileRetriever; -// -//import java.util.Iterator; -//import java.util.ServiceLoader; -// -//public class TileRetrieverProvider { -// -// private static TileRetrieverProvider provider; -// public static synchronized TileRetrieverProvider getInstance() { -// if (provider == null) { -// provider = new TileRetrieverProvider(); -// } -// return provider; -// } -// -// private final ServiceLoader loader; -// -// private TileRetrieverProvider() { -// loader = ServiceLoader.load(TileRetriever.class); -// } -// -// public TileRetriever load() { -// Iterator tileRetrievers = loader.iterator(); -// if (tileRetrievers.hasNext()) { -// return tileRetrievers.next(); -// } else { -// return new CachedOsmTileRetriever(); -// } -// } -//} From a05df7d55ce0199493bf4c8596d1f0c26c4f360c Mon Sep 17 00:00:00 2001 From: brunesto Date: Wed, 21 Oct 2020 22:50:05 +0200 Subject: [PATCH 03/11] format code + L&F --- .../maps/tile/osm/CachedOsmTileRetriever.java | 10 ++-- .../main/java/com/gluonhq/maps/MapView.java | 2 + .../maps/samples/mobile/MobileSample.java | 46 ++++++++----------- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java b/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java index 237222a..3a41ed1 100644 --- a/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java +++ b/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java @@ -51,11 +51,11 @@ public abstract class CachedOsmTileRetriever extends OsmTileRetriever { private static final Logger logger = Logger.getLogger(CachedOsmTileRetriever.class.getName()); private static final int TIMEOUT = 5000; - File cacheRoot; - boolean hasFileCache; - CacheThread cacheThread = null; - - { + File cacheRoot; + boolean hasFileCache; + CacheThread cacheThread = null; + + public CachedOsmTileRetriever() { try { File storageRoot = StorageService.create() .flatMap(StorageService::getPrivateStorage) diff --git a/maps/src/main/java/com/gluonhq/maps/MapView.java b/maps/src/main/java/com/gluonhq/maps/MapView.java index f686086..edb2c81 100644 --- a/maps/src/main/java/com/gluonhq/maps/MapView.java +++ b/maps/src/main/java/com/gluonhq/maps/MapView.java @@ -39,6 +39,7 @@ import javafx.scene.control.Label; import javafx.scene.layout.Region; import javafx.scene.shape.Rectangle; +import javafx.scene.text.Font; import javafx.util.Duration; import javafx.scene.layout.BackgroundFill; import javafx.scene.paint.Color; @@ -74,6 +75,7 @@ public MapView(TileRetriever tileRetriever) { getChildren().add(baseMap); label=new Label(tileRetriever.copyright()); label.setBackground(new Background(new BackgroundFill(Color.rgb(126, 126, 126, 0.5),null,null))); + label.setFont(Font.font(11)); getChildren().add(label); registerInputListeners(); diff --git a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java index 8434d73..8d21457 100644 --- a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java +++ b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java @@ -70,33 +70,27 @@ public class MobileSample extends Application { @Override public void start(Stage stage) { - TileRetriever osm=new CachedOsmTileRetriever() { - public String buildImageUrlString(int zoom, long i, long j) { - return "http://tile.openstreetmap.org/" +zoom+"/"+ i + "/" + j + ".png"; - - } - public String copyright() { - return "Map © OpenStreetMap contributors CC-BY-SA -- Please switch to another tile provider "; - } - }; - - TileRetriever mapBox=new CachedOsmTileRetriever() { - public String buildImageUrlString(int zoom, long i, long j) { - String url= "https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token=XXXXXX"; - return url; - } - public String copyright() { - return "Map data © OpenStreetMap contributors CC-BY-SA, Imagery © Mapbox"; - } - }; - - - MapView view = new MapView(osm); - - - - + TileRetriever osm=new CachedOsmTileRetriever() { + public String buildImageUrlString(int zoom, long i, long j) { + return "http://tile.openstreetmap.org/" +zoom+"/"+ i + "/" + j + ".png"; + + } + public String copyright() { + return "Map data © OpenStreetMap contributors, CC-BY-SA. Imagery © OpenStreetMap, for demo only."; + } + }; + TileRetriever mapBox=new CachedOsmTileRetriever() { + public String buildImageUrlString(int zoom, long i, long j) { + String url= "https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token=XXXXXX"; + return url; + } + public String copyright() { + return "Map data © OpenStreetMap contributors CC-BY-SA, Imagery © Mapbox"; + } + }; + + MapView view = new MapView(osm); view.addLayer(positionLayer()); view.setZoom(3); From f6c71273fc8354eb5a7a08b256310fbf9a618fc8 Mon Sep 17 00:00:00 2001 From: brunesto Date: Wed, 21 Oct 2020 23:19:01 +0200 Subject: [PATCH 04/11] CachedOsmTileRetriever uses name as suffix for cache directory --- .../gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java | 7 ++++--- .../java/com/gluonhq/maps/samples/mobile/MobileSample.java | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java b/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java index 3a41ed1..20926a2 100644 --- a/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java +++ b/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java @@ -54,14 +54,15 @@ public abstract class CachedOsmTileRetriever extends OsmTileRetriever { File cacheRoot; boolean hasFileCache; CacheThread cacheThread = null; - - public CachedOsmTileRetriever() { + String cacheName; + public CachedOsmTileRetriever(String cacheName) { + this.cacheName=cacheName; try { File storageRoot = StorageService.create() .flatMap(StorageService::getPrivateStorage) .orElseThrow(() -> new IOException("Storage Service is not available")); - cacheRoot = new File(storageRoot, ".gluonmaps"); + cacheRoot = new File(storageRoot, ".gluonmaps-"+cacheName); logger.fine("[JVDBG] cacheroot = " + cacheRoot); if (!cacheRoot.isDirectory()) { hasFileCache = cacheRoot.mkdirs(); diff --git a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java index 8d21457..9f3cd93 100644 --- a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java +++ b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java @@ -70,7 +70,7 @@ public class MobileSample extends Application { @Override public void start(Stage stage) { - TileRetriever osm=new CachedOsmTileRetriever() { + TileRetriever osm=new CachedOsmTileRetriever("osm") { public String buildImageUrlString(int zoom, long i, long j) { return "http://tile.openstreetmap.org/" +zoom+"/"+ i + "/" + j + ".png"; @@ -80,7 +80,7 @@ public String copyright() { } }; - TileRetriever mapBox=new CachedOsmTileRetriever() { + TileRetriever mapBox=new CachedOsmTileRetriever("mapBox") { public String buildImageUrlString(int zoom, long i, long j) { String url= "https://api.mapbox.com/styles/v1/mapbox/streets-v11/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token=XXXXXX"; return url; From b66d923603d3ddfa677712098ef368666d1ef802 Mon Sep 17 00:00:00 2001 From: brunesto Date: Thu, 29 Oct 2020 16:54:09 +0100 Subject: [PATCH 05/11] merged --- .../main/java/com/gluonhq/maps/samples/mobile/MobileSample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java index 948cbc9..6278e74 100644 --- a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java +++ b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java @@ -82,7 +82,7 @@ public String copyright() { TileRetriever mapBox=new CachedOsmTileRetriever("mapBox") { public String buildImageUrlString(int zoom, long i, long j) { - String url= "https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token=pk.eyJ1IjoiYnJ1bmVzdG8iLCJhIjoiY2lvNGowMmx4MDAycXZ5a3A0aXdqZTZjbCJ9.0kHXvJmsETs_QzfXfQv9mw"; + String url= "https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token=xxxx"; return url; } public String copyright() { From 347599afa5b49688a659efad8b57288819318aac Mon Sep 17 00:00:00 2001 From: brunesto Date: Mon, 30 Nov 2020 13:47:18 +0100 Subject: [PATCH 06/11] Revert "CachedOsmTileRetriever uses name as suffix for cache directory" This reverts commit f6c71273fc8354eb5a7a08b256310fbf9a618fc8. --- .../gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java | 7 +++---- .../java/com/gluonhq/maps/samples/mobile/MobileSample.java | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java b/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java index 157a799..49ce589 100644 --- a/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java +++ b/maps/src/main/java/com/gluonhq/impl/maps/tile/osm/CachedOsmTileRetriever.java @@ -53,15 +53,14 @@ public abstract class CachedOsmTileRetriever extends OsmTileRetriever { File cacheRoot; boolean hasFileCache; CacheThread cacheThread = null; - String cacheName; - public CachedOsmTileRetriever(String cacheName) { - this.cacheName=cacheName; + + public CachedOsmTileRetriever() { try { File storageRoot = StorageService.create() .flatMap(StorageService::getPrivateStorage) .orElseThrow(() -> new IOException("Storage Service is not available")); - cacheRoot = new File(storageRoot, ".gluonmaps-"+cacheName); + cacheRoot = new File(storageRoot, ".gluonmaps"); logger.fine("[JVDBG] cacheroot = " + cacheRoot); if (!cacheRoot.isDirectory()) { hasFileCache = cacheRoot.mkdirs(); diff --git a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java index 6278e74..257e68c 100644 --- a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java +++ b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java @@ -70,7 +70,7 @@ public class MobileSample extends Application { @Override public void start(Stage stage) { - TileRetriever osm=new CachedOsmTileRetriever("osm") { + TileRetriever osm=new CachedOsmTileRetriever() { public String buildImageUrlString(int zoom, long i, long j) { return "http://tile.openstreetmap.org/" +zoom+"/"+ i + "/" + j + ".png"; @@ -80,7 +80,7 @@ public String copyright() { } }; - TileRetriever mapBox=new CachedOsmTileRetriever("mapBox") { + TileRetriever mapBox=new CachedOsmTileRetriever() { public String buildImageUrlString(int zoom, long i, long j) { String url= "https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token=xxxx"; return url; From ee59b35c5c770a4581370ff2dc41cd2ccf1cb0ed Mon Sep 17 00:00:00 2001 From: brunesto Date: Fri, 4 Dec 2020 19:50:10 +0100 Subject: [PATCH 07/11] spacing + javadoc --- maps/src/main/java/com/gluonhq/impl/maps/BaseMap.java | 2 +- maps/src/main/java/com/gluonhq/maps/MapView.java | 10 ++++------ .../main/java/com/gluonhq/maps/tile/TileRetriever.java | 7 +++++-- .../com/gluonhq/maps/samples/mobile/MobileSample.java | 6 +++--- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/maps/src/main/java/com/gluonhq/impl/maps/BaseMap.java b/maps/src/main/java/com/gluonhq/impl/maps/BaseMap.java index eb95907..aa5e4c5 100644 --- a/maps/src/main/java/com/gluonhq/impl/maps/BaseMap.java +++ b/maps/src/main/java/com/gluonhq/impl/maps/BaseMap.java @@ -99,7 +99,7 @@ public class BaseMap extends Group { public TileRetriever tileRetriever; public BaseMap(TileRetriever tileRetriever) { - this.tileRetriever=tileRetriever; + this.tileRetriever = tileRetriever; for (int i = 0; i < tiles.length; i++) { tiles[i] = new HashMap<>(); } diff --git a/maps/src/main/java/com/gluonhq/maps/MapView.java b/maps/src/main/java/com/gluonhq/maps/MapView.java index c5a4a32..48e1990 100644 --- a/maps/src/main/java/com/gluonhq/maps/MapView.java +++ b/maps/src/main/java/com/gluonhq/maps/MapView.java @@ -46,8 +46,6 @@ import javafx.scene.layout.BackgroundFill; import javafx.scene.paint.Color; import javafx.scene.layout.Background; - - import java.util.LinkedList; import java.util.List; import java.util.function.Supplier; @@ -74,9 +72,9 @@ public class MapView extends Region { * Create a MapView component. */ public MapView(TileRetriever tileRetriever) { - baseMap = new BaseMap( tileRetriever); + baseMap = new BaseMap(tileRetriever); getChildren().add(baseMap); - label=new Label(tileRetriever.copyright()); + label = new Label(tileRetriever.copyright()); label.setBackground(new Background(new BackgroundFill(Color.rgb(126, 126, 126, 0.5),null,null))); label.setFont(Font.font(11)); getChildren().add(label); @@ -267,8 +265,8 @@ protected void layoutChildren() { } super.layoutChildren(); - label.setLayoutX(w-label.getWidth()); - label.setLayoutY(h-label.getHeight()); + label.setLayoutX(w - label.getWidth()); + label.setLayoutY(h - label.getHeight()); dirty = false; diff --git a/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java b/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java index e2e559c..f692b3d 100644 --- a/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java +++ b/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java @@ -32,7 +32,10 @@ import java.util.concurrent.CompletableFuture; public interface TileRetriever { - + + /** + * @return copyright info to be overlayed on the map + */ String copyright(); /** @@ -43,6 +46,6 @@ public interface TileRetriever { * @param i the horizontal position of the tile to load * @param j the vertical position of the tile to load * @return a completableFuture with the image representing the tile - */ + */ CompletableFuture loadTile(int zoom, long i, long j); } diff --git a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java index 257e68c..6f4a518 100644 --- a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java +++ b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java @@ -70,7 +70,7 @@ public class MobileSample extends Application { @Override public void start(Stage stage) { - TileRetriever osm=new CachedOsmTileRetriever() { + TileRetriever osm = new CachedOsmTileRetriever() { public String buildImageUrlString(int zoom, long i, long j) { return "http://tile.openstreetmap.org/" +zoom+"/"+ i + "/" + j + ".png"; @@ -80,9 +80,9 @@ public String copyright() { } }; - TileRetriever mapBox=new CachedOsmTileRetriever() { + TileRetriever mapBox = new CachedOsmTileRetriever() { public String buildImageUrlString(int zoom, long i, long j) { - String url= "https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token=xxxx"; + String url = "https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token=xxxx"; return url; } public String copyright() { From 417e9c9bfce942686b90ea2df5a8e216a4efc91a Mon Sep 17 00:00:00 2001 From: brunesto Date: Fri, 4 Dec 2020 19:57:42 +0100 Subject: [PATCH 08/11] map label uses css --- maps/src/main/java/com/gluonhq/maps/MapView.java | 4 ++-- maps/src/main/resources/com/gluonhq/maps/maps.css | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 maps/src/main/resources/com/gluonhq/maps/maps.css diff --git a/maps/src/main/java/com/gluonhq/maps/MapView.java b/maps/src/main/java/com/gluonhq/maps/MapView.java index 48e1990..7887c4f 100644 --- a/maps/src/main/java/com/gluonhq/maps/MapView.java +++ b/maps/src/main/java/com/gluonhq/maps/MapView.java @@ -75,9 +75,9 @@ public MapView(TileRetriever tileRetriever) { baseMap = new BaseMap(tileRetriever); getChildren().add(baseMap); label = new Label(tileRetriever.copyright()); - label.setBackground(new Background(new BackgroundFill(Color.rgb(126, 126, 126, 0.5),null,null))); - label.setFont(Font.font(11)); + label.getStyleClass().add("label-license"); getChildren().add(label); + getStylesheets().add(MapView.class.getResource("maps.css").toExternalForm()); registerInputListeners(); diff --git a/maps/src/main/resources/com/gluonhq/maps/maps.css b/maps/src/main/resources/com/gluonhq/maps/maps.css new file mode 100644 index 0000000..9cb026a --- /dev/null +++ b/maps/src/main/resources/com/gluonhq/maps/maps.css @@ -0,0 +1,4 @@ +.label-license { + -fx-background-color: rgba(126, 126, 126, 0.5); + -fx-font-size: 0.9em; +} From 5a01dc355d697a8156992690ce9eacda031da10f Mon Sep 17 00:00:00 2001 From: brunesto Date: Sat, 5 Dec 2020 04:24:35 +0100 Subject: [PATCH 09/11] comments + added my own token --- .../maps/samples/mobile/MobileSample.java | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java index 6f4a518..5e66e7a 100644 --- a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java +++ b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java @@ -70,19 +70,40 @@ public class MobileSample extends Application { @Override public void start(Stage stage) { + + /* + * In order to display OSM tiles you have to choose a tile provider. Here are some lists: + * + * -http://leaflet-extras.github.io/leaflet-providers/preview/ (fairly complete list with previews) + * -https://wiki.openstreetmap.org/wiki/Tile_servers + * -https://switch2osm.org/providers/#tile-hosting (both free tiers and commercial only) + */ + + /* + * Here is an example for accessing OpenStreetMap server's tiles. + * Please only use for "very limited testing purposes" - @see https://operations.osmfoundation.org/policies/tiles/ + * + */ TileRetriever osm = new CachedOsmTileRetriever() { public String buildImageUrlString(int zoom, long i, long j) { return "http://tile.openstreetmap.org/" +zoom+"/"+ i + "/" + j + ".png"; - } public String copyright() { return "Map data © OpenStreetMap contributors, CC-BY-SA. Imagery © OpenStreetMap, for demo only."; } }; - + /* + * Another example, using MapBox tiles free tier. + * To access MapBox you will need to create an account at https://www.mapbox.com/maps/ and generate an access token. + * + * Please note that the access token here is for demo only and will be replaced at some point - you need to get your own + * + * Styles known to work: satellite-v9,streets-v8 + */ TileRetriever mapBox = new CachedOsmTileRetriever() { public String buildImageUrlString(int zoom, long i, long j) { - String url = "https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token=xxxx"; + String token="pk.eyJ1IjoiYnJ1bmVzdG8iLCJhIjoiY2tpYjRpcWVrMDk3bDJ5azBibGZmYjJ2NyJ9.5mKw_JV1w9-VoAxjn2f9LA"; + String url = "https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token="+token; return url; } public String copyright() { From 17398a9f6477d28d0787b123346a0607016ecf28 Mon Sep 17 00:00:00 2001 From: brunesto Date: Sat, 5 Dec 2020 04:26:45 +0100 Subject: [PATCH 10/11] spacing --- .../main/java/com/gluonhq/maps/samples/mobile/MobileSample.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java index 5e66e7a..88e8b2e 100644 --- a/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java +++ b/samples/mobile/src/main/java/com/gluonhq/maps/samples/mobile/MobileSample.java @@ -102,7 +102,7 @@ public String copyright() { */ TileRetriever mapBox = new CachedOsmTileRetriever() { public String buildImageUrlString(int zoom, long i, long j) { - String token="pk.eyJ1IjoiYnJ1bmVzdG8iLCJhIjoiY2tpYjRpcWVrMDk3bDJ5azBibGZmYjJ2NyJ9.5mKw_JV1w9-VoAxjn2f9LA"; + String token = "pk.eyJ1IjoiYnJ1bmVzdG8iLCJhIjoiY2tpYjRpcWVrMDk3bDJ5azBibGZmYjJ2NyJ9.5mKw_JV1w9-VoAxjn2f9LA"; String url = "https://api.mapbox.com/styles/v1/mapbox/satellite-v9/tiles/256/"+zoom+"/"+i+"/"+j+"?access_token="+token; return url; } From 9d1c40d3108210a48a283539b4be80c26d6db8b2 Mon Sep 17 00:00:00 2001 From: brunesto Date: Sat, 5 Dec 2020 04:29:22 +0100 Subject: [PATCH 11/11] javadoc --- maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java b/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java index f692b3d..23937e7 100644 --- a/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java +++ b/maps/src/main/java/com/gluonhq/maps/tile/TileRetriever.java @@ -34,7 +34,7 @@ public interface TileRetriever { /** - * @return copyright info to be overlayed on the map + * @return Copyright/Attribution info to be overlayed on the map */ String copyright();