Skip to main content
GuidesAPI ReferenceExamples

Maps Perspective Change

|

This example demonstrates how to toggle the map view angle between 2D (vertical look-down at the map) and 3D (perspective, tilted map, looking towards the horizon).

Displaying the map in 2D
Displaying the map in 3D

UI and Map Integration

The MainActivity overrides the onCreate() function, which defines a button to toggle between 2D (vertical) and 3D (perspective/tilted) map modes.

The map starts in 2D mode (EViewPerspective.TwoDimensional), and the button shows "3D" to indicate that pressing it will switch the map to 3D mode (EViewPerspective.ThreeDimensional).

MainActivity.kt
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

surfaceView = findViewById(R.id.gem_surface)
button = findViewById(R.id.button)

val twoDimensionalText = resources.getString(R.string.two_dimensional)
val threeDimensionalText = resources.getString(R.string.three_dimensional)

button.setOnClickListener {
// Get the map view.
...
}
}

When the user presses the button, the map perspective is changed based on the currentPerspective and using the method setMapViewPerspective from the Map View preferences.

The Animation(EAnimation.Linear, 300) defines the animation type and duration of the perspective change.

Every functionality using Maps SDK must be executed on the same thread as the map engine runs using SdkCall.execute to place the desired functionality on the correct thread.

MainActivity.kt
surfaceView.mapView?.let { mapView ->
// Establish the current map view perspective.
currentPerspective = if (currentPerspective == EMapViewPerspective.TwoDimensional) {
button.text = twoDimensionalText
EMapViewPerspective.ThreeDimensional
} else {
button.text = threeDimensionalText
EMapViewPerspective.TwoDimensional
}
SdkCall.execute {
// Change the map view perspective.
mapView.preferences?.setMapViewPerspective(
currentPerspective,
Animation(EAnimation.Linear, 300)
)
}
}

Not using SdkCall.execute will result in an exception and the program will stop.