-
Everything works fine. What can it be? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
It seems the MapView / OpenGL is initialized many times(?). |
Beta Was this translation helpful? Give feedback.
-
I followed exactly the GettingStarted example. The problem only occurs when I open a file with the Intent.ACTION_OPEN_DOCUMENT. This in my onCreate(): // ------------------- //
// MapsForge - VTM //
// ------------------- //
// init MapView, Map and Preferences
mapView = findViewById(R.id.mapView);
map = mapView.map();
mapPrefs = new MapPreferences(MainActivity.class.getName(), getApplicationContext());
openMaps();
// Register the Intent for open PICKER to select track file GPX
intentACTION_IMPORT_TRACK_FILE = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intentACTION_IMPORT_TRACK_FILE.setType("*/*");
intentACTION_IMPORT_TRACK_FILE.addCategory(Intent.CATEGORY_OPENABLE);
resultActivityLauncherACTION_IMPORT_TRACK_FILE = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
//grant persistent read permissions
Intent intent = result.getData();
assert intent != null;
Uri trackFileUri = intent.getData();
DocumentFile trackFile = DocumentFile.fromSingleUri(getApplicationContext(), trackFileUri);
assert trackFile != null;
Log.i("mapsforge", "Track File: " + trackFile.getName());
cleanTracksLayers();
// Check file size (<3MB)
if ((trackFile.length()/1024) > 3000) {
Toasty.error(mapView.getContext(), "Track file is too big! Must be less 3 Mbyte.", Toasty.LENGTH_LONG).show();
}
else {
if (Objects.requireNonNull(trackFile.getName()).endsWith(".gpx"))
parseGPXfiles(trackFile);
else
Toasty.error(mapView.getContext(), "No track file!", Toasty.LENGTH_LONG).show();
}
}
}); private void openMaps() {
// Load map files from default folder
tileSources = new MultiMapFileTileSource();
checkMapFilesDownloaded();
ArrayList<FileInputStream> mapFiles = loadMapFiles();
if (mapFiles != null)
for (FileInputStream fis : mapFiles)
{
tileSource = new MapFileTileSource();
tileSource.setMapFileInputStream(fis);
tileSources.add(tileSource);
}
// Set map language
String language = Locale.getDefault().getLanguage();
Log.i("mapsforge", "Device local language: " + language);
if (language.equals("en") || language.equals("de") || language.equals("fr") || language.equals("es"))
tileSources.setPreferredLanguage(language);
else
tileSources.setPreferredLanguage("en");
// Vector layer
VectorTileLayer tileLayer = map.setBaseMap(tileSources);
// Building layer
map.layers().add(new BuildingLayer(map, tileLayer));
// Label layer
map.layers().add(new LabelLayer(map, tileLayer));
// Render theme
theme = map.setTheme(VtmThemes.DEFAULT);
//------------ MapEventsReceiver implementation to handle SingleTap and LongTap on Map
map.layers().add(new MapEventsReceiver(map));
// Scale bar
MapScaleBar mapScaleBar = new DefaultMapScaleBar(map);
MapScaleBarLayer mapScaleBarLayer = new MapScaleBarLayer(map, mapScaleBar);
mapScaleBarLayer.getRenderer().setPosition(GLViewport.Position.TOP_CENTER);
mapScaleBarLayer.getRenderer().setOffset(5 * CanvasAdapter.getScale(), 0);
map.layers().add(mapScaleBarLayer);
// Set position
GeoPoint mapCenter = tileSources.getBoundingBox().getCenterPoint();
mapPosition = new MapPosition();
mapPosition.setPosition(prefs.getFloat("lastMapPosition_lat", (float) mapCenter.getLatitude()), prefs.getFloat("lastMapPosition_log", (float) mapCenter.getLongitude()));
map.setMapPosition(mapPosition);
// init Style for Tracks layer (GPX)
int colorTrack_line = ContextCompat.getColor(getApplicationContext(), R.color.color_Nav_Track_line1);
styleTrack = Style.builder()
.strokeWidth(3)
.strokeColor(colorTrack_line)
.build();
// init track path layers (GPX)
trackPathLayers = new ArrayList<>();
// init tracks point layer (GPX)
pointMarkers = new ItemizedLayer(map, new ArrayList<>(), (MarkerSymbol) null, this);
map.layers().add(pointMarkers);
// init POI markers layer
poiMarkers = new ItemizedLayer(map, new ArrayList<>(), (MarkerSymbol) null, this);
map.layers().add(poiMarkers);
// init MainRoad path layer
pathLayers = new ArrayList<>(2);
int colorMainRoad_line = ContextCompat.getColor(getApplicationContext(), R.color.color_Nav_MainRoad_line);
styleMainRoad = Style.builder()
.strokeWidth(4)
.strokeColor(colorMainRoad_line)
.build();
PathLayer pathLayer = new PathLayer(map, styleMainRoad);
map.layers().add(pathLayer);
pathLayers.add(pathLayer);
// init Alternative Roads path layer
int colorAlternativeRoads_line = ContextCompat.getColor(getApplicationContext(), R.color.color_Nav_AlternativeRoads_line);
styleAlternativeRoads = Style.builder()
.strokeWidth(2.5f)
.strokeColor(colorAlternativeRoads_line)
.build();
pathLayer = new PathLayer(map, styleAlternativeRoads);
map.layers().add(pathLayer);
pathLayers.add(pathLayer);
// init Destination marker layer
destinationMarker = new ItemizedLayer(map, new ArrayList<>(), (MarkerSymbol) null, this);
map.layers().add(destinationMarker);
// init ViaPoints marker layer
viaPointMarkers = new ItemizedLayer(map, new ArrayList<>(), (MarkerSymbol) null, this);
map.layers().add(viaPointMarkers);
// Location layer
locationLayer = new LocationTextureLayer(map);
Bitmap bitmapIconNavArrow = new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_nav_arrow));
locationLayer.locationRenderer.setBitmapArrow(bitmapIconNavArrow);
locationLayer.locationRenderer.setBitmapMarker(bitmapIconNavArrow);
locationLayer.locationRenderer.setCallback(new LocationCallback()
{
@Override
public boolean hasRotation() {
return locationGps != null && locationGps.hasBearing();
}
@Override
public float getRotation() {
return (locationGps != null && locationGps.hasBearing()) ? locationGps.getBearing() : 0;
}
});
locationLayer.setEnabled(false);
map.layers().add(locationLayer);
} |
Beta Was this translation helpful? Give feedback.
-
Don't ask me how, but it works now :-) Mysteries! |
Beta Was this translation helpful? Give feedback.
Don't ask me how, but it works now :-)
Mysteries!