Draw a polygon
Draw a vector polygon on a map.
package com.mapbox.maps.testapp.examples.linesandpolygons import android.graphics.Colorimport android.os.Bundleimport androidx.appcompat.app.AppCompatActivityimport androidx.core.content.ContextCompatimport androidx.core.graphics.drawable.toBitmapimport com.mapbox.geojson.Pointimport com.mapbox.maps.Styleimport com.mapbox.maps.dsl.cameraOptionsimport com.mapbox.maps.extension.style.layers.generated.FillLayerimport com.mapbox.maps.extension.style.layers.generated.fillLayerimport com.mapbox.maps.extension.style.layers.generated.lineLayerimport com.mapbox.maps.extension.style.layers.getLayerimport com.mapbox.maps.extension.style.sources.generated.geoJsonSourceimport com.mapbox.maps.extension.style.styleimport com.mapbox.maps.testapp.Rimport com.mapbox.maps.testapp.databinding.ActivityDdsDrawPolygonBinding /*** Draw a vector polygon on a map with the Mapbox Android SDK.*/class DrawPolygonActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)val binding = ActivityDdsDrawPolygonBinding.inflate(layoutInflater)setContentView(binding.root)binding.mapView.mapboxMap.setCamera(START_CAMERA_POSITION)binding.mapView.mapboxMap.loadStyle(style(style = Style.LIGHT) {+geoJsonSource(SOURCE_ID) {url(SOURCE_URL)}+layerAtPosition(fillLayer(LAYER_ID, SOURCE_ID) {fillColor(Color.parseColor("#0080ff")).fillOpacity(0.5)},below = SETTLEMENT_LABEL)+lineLayer(TOP_LAYER_ID, SOURCE_ID) {lineColor(ContextCompat.getColor(this@DrawPolygonActivity, R.color.black))lineWidth(3.0)}})binding.patternFab.setOnClickListener {binding.mapView.mapboxMap.getStyle { style ->val bitmap = ContextCompat.getDrawable(this@DrawPolygonActivity, R.drawable.pattern)?.toBitmap(128, 128)!!style.addImage(IMAGE_ID, bitmap)val layer = style.getLayer(LAYER_ID) as FillLayerlayer.fillPattern(IMAGE_ID)layer.fillOpacity(0.7)}}} companion object {private const val IMAGE_ID = "stripe-pattern"private const val LAYER_ID = "layer-id"private const val SOURCE_ID = "source-id"private const val TOP_LAYER_ID = "line-layer"private const val SETTLEMENT_LABEL = "settlement-major-label"private const val SOURCE_URL = "asset://maine_polygon.geojson"private val START_CAMERA_POSITION = cameraOptions {center(Point.fromLngLat(-68.137343, 45.137451))zoom(5.0)bearing(0.0)pitch(0.0)}}}
Was this example helpful?