Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add feature id to annotations; prepare showcase of callout annotations #994

Merged
merged 4 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1112,6 +1112,18 @@
android:name="android.support.PARENT_ACTIVITY"
android:value=".ExampleOverviewActivity" />
</activity>
<activity
android:name=".examples.markersandcallouts.AnnotationWithCalloutActivity"
android:description="@string/description_view_annotation_basic"
android:exported="true"
android:label="@string/activity_view_annotations_basic">
<meta-data
android:name="@string/category"
android:value="@string/category_markers_and_callouts" />
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ExampleOverviewActivity" />
</activity>
<activity
android:name=".TestMapActivity"
android:exported="true" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.mapbox.maps.testapp.examples.markersandcallouts

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.mapbox.geojson.Point
import com.mapbox.maps.Style
import com.mapbox.maps.ViewAnnotationAnchor
import com.mapbox.maps.extension.style.layers.properties.generated.IconAnchor
import com.mapbox.maps.plugin.annotation.annotations
import com.mapbox.maps.plugin.annotation.generated.PointAnnotationOptions
import com.mapbox.maps.plugin.annotation.generated.createPointAnnotationManager
import com.mapbox.maps.testapp.R
import com.mapbox.maps.testapp.databinding.ActivityViewAnnotationShowcaseBinding
import com.mapbox.maps.testapp.databinding.ItemCalloutViewBinding
import com.mapbox.maps.testapp.utils.BitmapUtils
import com.mapbox.maps.viewannotation.viewAnnotationOptions

/**
* Example how to add view annotation to the point annotation.
*/
class AnnotationWithCalloutActivity : AppCompatActivity() {

@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityViewAnnotationShowcaseBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.mapView.getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS) {
val viewAnnotationManager = binding.mapView.viewAnnotationManager
val point = Point.fromLngLat(
AIRPORT_LONGITUDE,
AIRPORT_LATITUDE
)
val iconBitmap = BitmapUtils.bitmapFromDrawableRes(
this@AnnotationWithCalloutActivity,
R.drawable.ic_airplanemode_active_black_24dp
)!!

val annotationPlugin = binding.mapView.annotations
val pointAnnotationOptions: PointAnnotationOptions = PointAnnotationOptions()
.withPoint(point)
.withIconImage(iconBitmap)
.withIconAnchor(IconAnchor.BOTTOM)

val annotationManager = annotationPlugin.createPointAnnotationManager()
val annotation = annotationManager.create(pointAnnotationOptions)

val viewAnnotation = viewAnnotationManager.addViewAnnotation(
resId = R.layout.item_callout_view,
options = viewAnnotationOptions {
geometry(point)
associatedFeatureId(annotation.featureIdentifier)
anchor(ViewAnnotationAnchor.BOTTOM)
offsetY((annotation.iconImageBitmap?.height!!).toInt())
}
)
ItemCalloutViewBinding.bind(viewAnnotation).apply {
textNativeView.text = "lat=%.2f\nlon=%.2f".format(point.latitude(), point.longitude())
closeNativeView.setOnClickListener {
viewAnnotationManager.removeViewAnnotation(viewAnnotation)
}
selectButton.setOnClickListener { b ->
val button = b as Button
val isSelected = button.text.toString().equals("SELECT", true)
val pxDelta = if (isSelected) SELECTED_ADD_COEF_PX else -SELECTED_ADD_COEF_PX
button.text = if (isSelected) "DESELECT" else "SELECT"
viewAnnotationManager.updateViewAnnotation(
viewAnnotation,
viewAnnotationOptions {
width(viewAnnotationManager.getViewAnnotationOptionsByView(viewAnnotation)?.width!! + pxDelta)
height(viewAnnotationManager.getViewAnnotationOptionsByView(viewAnnotation)?.height!! + pxDelta)
selected(isSelected)
}
)
}
}

binding.fabStyleToggle.setOnClickListener {
annotation.iconImageBitmap = if (annotation.iconImage == null) iconBitmap else null
annotationManager.update(annotation)
}
}
}

private companion object {
const val SELECTED_ADD_COEF_PX = 50
private const val AIRPORT_LONGITUDE = 0.381457
private const val AIRPORT_LATITUDE = 6.687337
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ abstract class AnnotationManagerImpl<G : Geometry, T : Annotation<G>, S : Annota
private fun convertAnnotationsToFeatures(annotations: Collection<T>): List<Feature> =
annotations.map {
it.setUsedDataDrivenProperties()
Feature.fromGeometry(it.geometry, it.getJsonObjectCopy())
Feature.fromGeometry(it.geometry, it.getJsonObjectCopy(), it.featureIdentifier)
kiryldz marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ abstract class Annotation<T : Geometry>(
*/
var isSelected: Boolean = false

/**
* Unique feature identifier.
*/
val featureIdentifier: String = "annotation_feature_${(FEATURE_IDENTIFIER++)}"
kiryldz marked this conversation as resolved.
Show resolved Hide resolved

/**
* Static variables and methods.
*/
Expand All @@ -87,5 +92,10 @@ abstract class Annotation<T : Geometry>(
* Minimum latitude value in Mercator projection.
*/
const val MIN_MERCATOR_LATITUDE = -85.05112877980659

/**
* Unique feature identifier incremental counter.
*/
private var FEATURE_IDENTIFIER = 5234
kiryldz marked this conversation as resolved.
Show resolved Hide resolved
}
}