/
GettingStarted.java
150 lines (134 loc) · 6.33 KB
/
GettingStarted.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright 2018-2020 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.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
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.FileInputStream;
/**
* A very basic Android app example.
* <p>
* You'll need a map with filename berlin.map from download.mapsforge.org in device storage.
*/
public class GettingStarted extends Activity {
// Request code for selecting a map file
private static final int SELECT_MAP_FILE = 0;
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);
/*
* Open map.
*/
Intent intent = new Intent(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? Intent.ACTION_OPEN_DOCUMENT : Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
startActivityForResult(intent, SELECT_MAP_FILE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SELECT_MAP_FILE && resultCode == Activity.RESULT_OK) {
if (data != null) {
Uri uri = data.getData();
openMap(uri);
}
}
}
private void openMap(Uri uri) {
try {
/*
* We then make some simple adjustments, such as showing a scale bar and zoom controls.
*/
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.
*/
FileInputStream fis = (FileInputStream) getContentResolver().openInputStream(uri);
MapDataStore mapDataStore = new MapFile(fis);
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();
}
}