Skip to content

Hello Map

In this guide you will learn how to render an interactive map centered on a desired location. The map is fully 3D, supporting pan, pinch-zoom, rotate and tilt.

Setup

First, get an API key token, see the Getting Started guide.
Download the Maps & Navigation SDK for Android archive file

Download the HelloMap project archive file or clone the project with Git

See the Configure Android Example guide.

Run the example

In Android Studio, from the File menu, select Sync Project with Gradle Files

Hello map example Android screenshot

An android device should be connected via USB cable.
Press SHIFT+F10 to compile, install and run the example on the android device.

How it works

Android example screenshot

You can open the MainActivity.kt file to see how the interactive map is rendered.

 1class MainActivity : AppCompatActivity()
 2{
 3 private var mainMapView: MapView? = null
 4 override fun onCreate(savedInstanceState: Bundle?)
 5 {
 6     super.onCreate(savedInstanceState)
 7     setContentView(R.layout.activity_main)
 8     val mapSurface = findViewById<GemSurfaceView>(R.id.gem_surface)
 9     mapSurface.onScreenCreated = { screen ->
10         // Defines an action that should be done after the screen is created.
11         SdkCall.execute
12         {
13             /*
14             Define a rectangle in which the map view will expand.
15             Predefined value of the offsets is 0.
16             Value 1 means the offset will take 100% of available space.
17              */
18             val mainViewRect = RectF(0.0f, 0.0f, 1.0f, 1.0f)
19             // Produce a map view and establish that it is the main map view.
20             val mapView = MapView.produce(screen, mainViewRect)
21             mainMapView = mapView
22         }
23     }
24     val app = packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)
25     val token = app.metaData.getString("com.generalmagic.sdk.token") ?: "YOUR_TOKEN"
26     if (!SdkInitHelper.init(this, token))
27     {
28         // The SDK initialization was not completed.
29         finish()
30     }
31 }
32 override fun onDestroy()
33 {
34     super.onDestroy()
35     // Deinitialize the SDK.
36     SdkInitHelper.deinit()
37 }
38 override fun onBackPressed()
39 {
40     terminateApp(this)
41 }
42}
The MainActivity overrides the onCreate function which defines the
mapSurface.onScreenCreated callback to create a MapView instance when a drawing surface is available:
val mapView = MapView.produce(screen, mainViewRect)
Then the SDK is initialized:
SdkInitHelper.init(this, token) and the map is rendered.

Android Examples

Maps SDK for Android Examples can be downloaded or cloned with Git