Skip to content

Commit

Permalink
Add example activity of creating MapView
Browse files Browse the repository at this point in the history
  • Loading branch information
kiryldz committed Feb 8, 2021
1 parent dc6c6a4 commit 5f8302b
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 2 deletions.
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -919,5 +919,17 @@
android:name="android.allow_multiple_resumed_activities"
android:value="true" />
</activity>
<activity
android:name=".examples.MapViewCustomizationActivity"
android:description="@string/description_map_view_customization"
android:exported="true"
android:label="@string/activity_map_view_customization">
<meta-data
android:name="@string/category"
android:value="@string/category_basic" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ExampleOverviewActivity" />
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.mapbox.maps.testapp.examples

import android.os.Bundle
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.Point
import com.mapbox.maps.*
import com.mapbox.maps.testapp.R
import kotlinx.android.synthetic.main.activity_map_view_customization.*

/**
* Example of customizing your [MapView].
* This example will show how to create [MapView] both programmatically and from xml
* and apply various options.
*/
class MapViewCustomizationActivity : AppCompatActivity() {

private lateinit var customMapView: MapView

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_map_view_customization)
configureMapViewFromXml()
configureMapViewFromCode()
}

private fun configureMapViewFromCode() {
val mapboxMapOptions = MapboxMapOptions(this).apply {
// set initial camera position
cameraOptions = CameraOptions.Builder()
.center(Point.fromLngLat(-122.4194, 37.7749))
.zoom(9.0)
.build()
// use texture view renderer
textureView = true
// set other map options
mapOptions = MapOptions.Builder()
.constrainMode(ConstrainMode.HEIGHT_ONLY)
.glyphsRasterizationOptions(
GlyphsRasterizationOptions.Builder()
.rasterizationMode(GlyphsRasterizationMode.IDEOGRAPHS_RASTERIZED_LOCALLY)
.build()
)
.build()
// set token and cache size for this particular map view
resourceOptions = ResourceOptions.Builder()
.accessToken(getString(R.string.mapbox_access_token))
.cacheSize(75_000L)
.build()
}
// create view programmatically and add to root layout
customMapView = MapView(this, mapboxMapOptions)
val params = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
0,
1.0f
)
root.addView(customMapView, params)
// load style to map view
customMapView.getMapboxMap().loadStyleUri(Style.SATELLITE)
}

private fun configureMapViewFromXml() {
// let's set `custom` token to MapView from code (however it will be same token from resources so that map will work)
MapboxOptions.setDefaultResourceOptions(this, getString(R.string.mapbox_access_token))
// all options provided in xml file - so we just load style
mapView.getMapboxMap().loadStyleUri(Style.DARK)
}

override fun onStart() {
super.onStart()
mapView.onStart()
customMapView.onStart()
}

override fun onStop() {
super.onStop()
mapView.onStop()
customMapView.onStop()
}

override fun onLowMemory() {
super.onLowMemory()
mapView.onLowMemory()
customMapView.onLowMemory()
}

override fun onDestroy() {
super.onDestroy()
mapView.onDestroy()
customMapView.onDestroy()
}
}
23 changes: 23 additions & 0 deletions app/src/main/res/layout/activity_map_view_customization.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/root"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.mapbox.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:mapbox_cameraTargetLat="52.52"
app:mapbox_cameraTargetLng="13.4050"
app:mapbox_cameraPitch="65"
app:mapbox_cameraZoom="15"
app:mapbox_mapOrientation="downwards"
app:mapbox_mapSurface="surface_view"
tools:context=".examples.SimpleMapActivity" />

</LinearLayout>
1 change: 1 addition & 0 deletions app/src/main/res/values/example_descriptions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@
<string name="description_location_component_animation">Animates puck with custom location provider updates</string>
<string name="description_santa_catalina">Santa Catalina Walking Route</string>
<string name="description_multiple_display">Display the map on a secondary display</string>
<string name="description_map_view_customization">Customize your map view</string>
</resources>
1 change: 1 addition & 0 deletions app/src/main/res/values/example_titles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,5 @@
<string name="activity_location_component_animation">Location component animation</string>
<string name="activity_santa_catalina">Santa Catalina Island</string>
<string name="activity_multiple_display">Multi display</string>
<string name="activity_map_view_customization">Creating a map view</string>
</resources>
1 change: 0 additions & 1 deletion sdk/src/main/res-public/values/public.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
<public name="mapbox_cameraTargetLng" type="attr"/>
<public name="mapbox_cameraZoom" type="attr"/>
<public name="mapbox_cameraBearing" type="attr"/>
<public name="mapbox_cameraTilt" type="attr"/>
<public name="mapbox_cameraPitch" type="attr"/>
<public name="mapbox_cameraAnchorX" type="attr"/>
<public name="mapbox_cameraAnchorY" type="attr"/>
Expand Down
1 change: 0 additions & 1 deletion sdk/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
<attr name="mapbox_cameraTargetLng" format="float" />
<attr name="mapbox_cameraZoom" format="float" />
<attr name="mapbox_cameraBearing" format="float" />
<attr name="mapbox_cameraTilt" format="float" />
<attr name="mapbox_cameraPitch" format="float" />
<attr name="mapbox_cameraAnchorX" format="float" />
<attr name="mapbox_cameraAnchorY" format="float" />
Expand Down

0 comments on commit 5f8302b

Please sign in to comment.