Permalink
Browse files

Getting Started example in Samples #724

  • Loading branch information...
devemux86 committed Feb 21, 2018
1 parent 5744f12 commit 3c335fb71d3594b0419e54c07ba5fec7976e90d9
@@ -10,7 +10,7 @@ If you have any questions or problems, don't hesitate to ask our public [mailing
- Mapsforge project uses a [compact file format](docs/Specification-Binary-Map-File.md) for fast ad-hoc rendering of OpenStreetMap data.
- We provide tools to [compile your own maps](docs/Getting-Started-Map-Writer.md) with detailed [instructions](docs/MapCreation.md) and also [precompiled maps](http://download.mapsforge.org).
- It provides [simple boilerplate code](docs/Getting-Started-Android-App.md) to build applications for Android that display OpenStreetMap-based maps.
- It provides [simple boilerplate code](mapsforge-samples-android/src/main/java/org/mapsforge/samples/android/GettingStarted.java) to build applications for Android that display OpenStreetMap-based maps.
- It provides a library to build standalone applications in Java ([instructions](docs/Desktop.md)).
- Mapsforge maps can be flexibly styled with XML style files ([render themes](docs/Rendertheme.md)).
- We provide [tools & library](docs/POI.md) for POI search and also [precompiled POI](http://download.mapsforge.org).

This file was deleted.

Oops, something went wrong.
@@ -76,6 +76,9 @@
<activity
android:name=".DualOverviewMapViewer"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name=".GettingStarted"
android:configChanges="keyboardHidden|orientation|screenSize" />
<activity
android:name=".GridMapViewer"
android:configChanges="keyboardHidden|orientation|screenSize" />
@@ -0,0 +1,131 @@
/*
* Copyright 2018 devemux86
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mapsforge.samples.android;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.android.util.AndroidUtil;
import org.mapsforge.map.android.view.MapView;
import org.mapsforge.map.datastore.MapDataStore;
import org.mapsforge.map.layer.cache.TileCache;
import org.mapsforge.map.layer.renderer.TileRendererLayer;
import org.mapsforge.map.reader.MapFile;
import org.mapsforge.map.rendertheme.InternalRenderTheme;
import java.io.File;
/**
* A very basic Android app example.
* <p>
* You'll need a map with filename germany.map from download.mapsforge.org in device storage.
* Can be berlin.map renamed as germany.map because of smaller size.
*/
public class GettingStarted extends Activity {
// Name of the map file in device storage
private static final String MAP_FILE = "germany.map";
private MapView mapView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Before you make any calls on the mapsforge library, you need to initialize the
* AndroidGraphicFactory. Behind the scenes, this initialization process gathers a bit of
* information on your device, such as the screen resolution, that allows mapsforge to
* automatically adapt the rendering for the device.
* If you forget this step, your app will crash. You can place this code, like in the
* Samples app, in the Android Application class. This ensures it is created before any
* specific activity. But it can also be created in the onCreate() method in your activity.
*/
AndroidGraphicFactory.createInstance(getApplication());
/*
* A MapView is an Android View (or ViewGroup) that displays a mapsforge map. You can have
* multiple MapViews in your app or even a single Activity. Have a look at the mapviewer.xml
* on how to create a MapView using the Android XML Layout definitions. Here we create a
* MapView on the fly and make the content view of the activity the MapView. This means
* that no other elements make up the content of this activity.
*/
mapView = new MapView(this);
setContentView(mapView);
try {
/*
* We then make some simple adjustments, such as showing a scale bar and zoom controls.
*/
mapView.setClickable(true);
mapView.getMapScaleBar().setVisible(true);
mapView.setBuiltInZoomControls(true);
/*
* To avoid redrawing all the tiles all the time, we need to set up a tile cache with an
* utility method.
*/
TileCache tileCache = AndroidUtil.createTileCache(this, "mapcache",
mapView.getModel().displayModel.getTileSize(), 1f,
mapView.getModel().frameBufferModel.getOverdrawFactor());
/*
* Now we need to set up the process of displaying a map. A map can have several layers,
* stacked on top of each other. A layer can be a map or some visual elements, such as
* markers. Here we only show a map based on a mapsforge map file. For this we need a
* TileRendererLayer. A TileRendererLayer needs a TileCache to hold the generated map
* tiles, a map file from which the tiles are generated and Rendertheme that defines the
* appearance of the map.
*/
File mapFile = new File(Environment.getExternalStorageDirectory(), MAP_FILE);
MapDataStore mapDataStore = new MapFile(mapFile);
TileRendererLayer tileRendererLayer = new TileRendererLayer(tileCache, mapDataStore,
mapView.getModel().mapViewPosition, AndroidGraphicFactory.INSTANCE);
tileRendererLayer.setXmlRenderTheme(InternalRenderTheme.DEFAULT);
/*
* On its own a tileRendererLayer does not know where to display the map, so we need to
* associate it with our mapView.
*/
mapView.getLayerManager().getLayers().add(tileRendererLayer);
/*
* The map also needs to know which area to display and at what zoom level.
* Note: this map position is specific to Berlin area.
*/
mapView.setCenter(new LatLong(52.517037, 13.38886));
mapView.setZoomLevel((byte) 12);
} catch (Exception e) {
/*
* In case of map file errors avoid crash, but developers should handle these cases!
*/
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
/*
* Whenever your activity exits, some cleanup operations have to be performed lest your app
* runs out of memory.
*/
mapView.destroyAll();
AndroidGraphicFactory.clearResourceMemoryCache();
super.onDestroy();
}
}
@@ -90,6 +90,8 @@ protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_samples);
LinearLayout linearLayout = findViewById(R.id.samples);
linearLayout.addView(createButton(GettingStarted.class));
linearLayout.addView(createLabel(null));
linearLayout.addView(createButton(DefaultTheme.class));
linearLayout.addView(createButton(DiagnosticsMapViewer.class));
linearLayout.addView(createButton(SimplestMapViewer.class));

0 comments on commit 3c335fb

Please sign in to comment.