Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Create PropertyIconDeterminationActivity.kt #1392

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package com.mapbox.mapboxandroiddemo.examples.dds

import android.graphics.BitmapFactory
import com.mapbox.mapboxandroiddemo.R
import android.graphics.PointF
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.Feature
import com.mapbox.geojson.FeatureCollection
import com.mapbox.geojson.Point
import com.mapbox.mapboxsdk.Mapbox
import com.mapbox.mapboxsdk.geometry.LatLng
import com.mapbox.mapboxsdk.maps.MapView
import com.mapbox.mapboxsdk.maps.MapboxMap
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.style.expressions.Expression.*
import com.mapbox.mapboxsdk.style.layers.Property
import com.mapbox.mapboxsdk.style.layers.PropertyFactory.*
import com.mapbox.mapboxsdk.style.layers.SymbolLayer
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource


class PropertyIconDeterminationActivity : AppCompatActivity(), MapboxMap.OnMapClickListener {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add class level documentation that explains what this example is doing



private val SOURCE_ID = "SOURCE_ID"
private val RED_ICON_ID = "RED_ICON_ID"
private val YELLOW_ICON_ID = "YELLOW_ICON_ID"
private val LAYER_ID = "LAYER_ID"
private val ICON_PROPERTY = "ICON_PROPERTY"
Comment on lines +28 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

constants should be put into the companion object

private var mapView: MapView? = null
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don';t need to rely on nullable views and using findViewById is you use android-kotlin-extensions: https://antonioleiva.com/kotlin-android-extensions/

Copy link

@alishari alishari Jan 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private lateinit var mapboxMap: MapboxMap
private lateinit var featureInfoTextView: TextView


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Mapbox.getInstance(this, getString(R.string.access_token))
setContentView(R.layout.activity_dds_property_icon_switch)

mapView = findViewById(R.id.mapView)
featureInfoTextView = findViewById(R.id.feature_info)
mapView?.onCreate(savedInstanceState)
mapView?.getMapAsync { mapboxMap ->

mapboxMap.setStyle(Style.Builder().fromUri("mapbox://styles/mapbox/cj44mfrt20f082snokim4ungi") // Add the SymbolLayer icon image to the map style
.withImage(RED_ICON_ID, BitmapFactory.decodeResource(
[email protected](), R.drawable.red_marker))
.withImage(YELLOW_ICON_ID, BitmapFactory.decodeResource(
[email protected](), R.drawable.yellow_marker)) // Adding a GeoJson source for the SymbolLayer icons.
.withSource(GeoJsonSource(SOURCE_ID,
FeatureCollection.fromFeatures(initCoordinateData()!!))) // Adding the actual SymbolLayer to the map style. The match expression will check the
// ICON_PROPERTY property key and then use the partner value for the actual icon id.
.withLayer(SymbolLayer(LAYER_ID, SOURCE_ID)
.withProperties(iconImage(match(
get(ICON_PROPERTY), literal(RED_ICON_ID),
stop(YELLOW_ICON_ID, YELLOW_ICON_ID),
stop(RED_ICON_ID, RED_ICON_ID))),
iconAllowOverlap(true),
iconAnchor(Property.ICON_ANCHOR_BOTTOM))
)) { // Map is set up and the style has loaded. Now you can add additional data or make other map adjustments.
[email protected] = mapboxMap
mapboxMap.addOnMapClickListener(this@PropertyIconDeterminationActivity)
Toast.makeText(this@PropertyIconDeterminationActivity, "marker tapped",
Toast.LENGTH_SHORT).show()
}
}

}

override fun onMapClick(point: LatLng): Boolean {
return handleClickIcon(mapboxMap.projection.toScreenLocation(point))
}


/**
* This method handles click events for SymbolLayer symbols.
*
* @param screenPoint the point on screen clicked
*/
private fun handleClickIcon(screenPoint: PointF): Boolean {
val features = mapboxMap.queryRenderedFeatures(screenPoint, LAYER_ID)
return if (!features.isEmpty()) {
// Show the Feature in the TextView to show that the icon is based on the ICON_PROPERTY key/value

featureInfoTextView.text = features[0].toJson()
true
} else {
false
}
}

private fun initCoordinateData(): List<Feature>? {
val singleFeatureOne = Feature.fromGeometry(
Point.fromLngLat(72.88055419921875,
19.05822387777432))
singleFeatureOne.addStringProperty(ICON_PROPERTY, RED_ICON_ID)
val singleFeatureTwo = Feature.fromGeometry(
Point.fromLngLat(77.22015380859375,
28.549544699103865))
singleFeatureTwo.addStringProperty(ICON_PROPERTY, YELLOW_ICON_ID)
val singleFeatureThree = Feature.fromGeometry(
Point.fromLngLat(88.36647033691406,
22.52016858599439))
singleFeatureThree.addStringProperty(ICON_PROPERTY, RED_ICON_ID)

// Not adding a ICON_PROPERTY property to fourth and fifth features in order to show off the default
// nature of the match expression used in the example up above
val singleFeatureFour = Feature.fromGeometry(
Point.fromLngLat(78.42315673828125,
17.43320034474222))
val singleFeatureFive = Feature.fromGeometry(
Point.fromLngLat(80.16448974609375,
12.988500396985364))
val symbolLayerIconFeatureList: MutableList<Feature> = ArrayList()
symbolLayerIconFeatureList.add(singleFeatureOne)
symbolLayerIconFeatureList.add(singleFeatureTwo)
symbolLayerIconFeatureList.add(singleFeatureThree)
symbolLayerIconFeatureList.add(singleFeatureFour)
symbolLayerIconFeatureList.add(singleFeatureFive)
return symbolLayerIconFeatureList
}


override fun onResume() {
super.onResume()
mapView!!.onResume()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid using !! (with updating remark above, this won't be needed anymore)

}

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

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

override fun onPause() {
super.onPause()
mapView!!.onPause()
}

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

override fun onDestroy() {
super.onDestroy()
if (mapboxMap != null) {
mapboxMap.removeOnMapClickListener(this)
}
mapView!!.onDestroy()
}


}