diff options
Diffstat (limited to 'platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/infowindow')
2 files changed, 58 insertions, 43 deletions
diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/infowindow/DynamicInfoWindowAdapterActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/infowindow/DynamicInfoWindowAdapterActivity.java index cca87bd0fb..31efe80091 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/infowindow/DynamicInfoWindowAdapterActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/infowindow/DynamicInfoWindowAdapterActivity.java @@ -2,6 +2,7 @@ package com.mapbox.mapboxsdk.testapp.activity.infowindow; import android.graphics.Color; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.v4.content.res.ResourcesCompat; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; @@ -28,13 +29,39 @@ public class DynamicInfoWindowAdapterActivity extends AppCompatActivity implemen private MapboxMap mapboxMap; private MapView mapView; + private MarkerView marker; + + private MapboxMap.OnMapClickListener mapClickListener = new MapboxMap.OnMapClickListener() { + @Override + public boolean onMapClick(@NonNull LatLng point) { + if (marker == null) { + return false; + } + + // Distance from click to marker + double distanceKm = marker.getPosition().distanceTo(point) / 1000; + + // Get the info window + final InfoWindow infoWindow = marker.getInfoWindow(); + + // Get the view from the info window + if (infoWindow != null && infoWindow.getView() != null) { + // Set the new text on the text view in the info window + TextView textView = (TextView) infoWindow.getView(); + textView.setText(String.format(Locale.getDefault(), "%.2fkm", distanceKm)); + // Update the info window position (as the text length changes) + textView.post(infoWindow::update); + } + return true; + } + }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_infowindow_adapter); - mapView = (MapView) findViewById(R.id.mapView); + mapView = findViewById(R.id.mapView); mapView.onCreate(savedInstanceState); mapView.getMapAsync(this); } @@ -50,30 +77,11 @@ public class DynamicInfoWindowAdapterActivity extends AppCompatActivity implemen mapboxMap.getUiSettings().setDeselectMarkersOnTap(false); // Add a marker - final MarkerView marker = addMarker(mapboxMap); + marker = addMarker(mapboxMap); mapboxMap.selectMarker(marker); // On map click, change the info window contents - mapboxMap.setOnMapClickListener(point -> { - // Distance from click to marker - double distanceKm = marker.getPosition().distanceTo(point) / 1000; - - // Get the info window - final InfoWindow infoWindow = marker.getInfoWindow(); - - // Get the view from the info window - if (infoWindow != null && infoWindow.getView() != null) { - // Set the new text on the text view in the info window - TextView textView = (TextView) infoWindow.getView(); - textView.setText(String.format(Locale.getDefault(), "%.2fkm", distanceKm)); - textView.post(() -> { - // Update the info window position (as the text length changes) - infoWindow.update(); - }); - } - - return false; - }); + mapboxMap.addOnMapClickListener(mapClickListener); // Focus on Paris mapboxMap.animateCamera(CameraUpdateFactory.newLatLng(PARIS)); @@ -133,6 +141,9 @@ public class DynamicInfoWindowAdapterActivity extends AppCompatActivity implemen @Override protected void onDestroy() { super.onDestroy(); + if (mapboxMap != null) { + mapboxMap.removeOnMapClickListener(mapClickListener); + } mapView.onDestroy(); } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/infowindow/InfoWindowActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/infowindow/InfoWindowActivity.java index 57a1412b55..95bb0d2324 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/infowindow/InfoWindowActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/infowindow/InfoWindowActivity.java @@ -24,13 +24,32 @@ import java.text.DecimalFormat; * </p> */ public class InfoWindowActivity extends AppCompatActivity - implements OnMapReadyCallback, MapboxMap.OnInfoWindowCloseListener, MapboxMap.OnMapLongClickListener, - MapboxMap.OnInfoWindowClickListener, MapboxMap.OnInfoWindowLongClickListener { + implements OnMapReadyCallback, MapboxMap.OnInfoWindowCloseListener, MapboxMap.OnInfoWindowClickListener, + MapboxMap.OnInfoWindowLongClickListener { private MapboxMap mapboxMap; private MapView mapView; private Marker customMarker; + private MapboxMap.OnMapLongClickListener mapLongClickListener = new MapboxMap.OnMapLongClickListener() { + @Override + public boolean onMapLongClick(@NonNull LatLng point) { + if (customMarker != null) { + // Remove previous added marker + mapboxMap.removeAnnotation(customMarker); + customMarker = null; + } + + // Add marker on long click location with default marker image + customMarker = mapboxMap.addMarker(new MarkerOptions() + .title("Custom Marker") + .snippet(new DecimalFormat("#.#####").format(point.getLatitude()) + ", " + + new DecimalFormat("#.#####").format(point.getLongitude())) + .position(point)); + return true; + } + }; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -77,7 +96,7 @@ public class InfoWindowActivity extends AppCompatActivity private void addInfoWindowListeners() { mapboxMap.setOnInfoWindowCloseListener(this); - mapboxMap.setOnMapLongClickListener(this); + mapboxMap.addOnMapLongClickListener(mapLongClickListener); mapboxMap.setOnInfoWindowClickListener(this); mapboxMap.setOnInfoWindowLongClickListener(this); } @@ -109,24 +128,6 @@ public class InfoWindowActivity extends AppCompatActivity } @Override - public boolean onMapLongClick(@NonNull LatLng point) { - if (customMarker != null) { - // Remove previous added marker - mapboxMap.removeAnnotation(customMarker); - customMarker = null; - } - - // Add marker on long click location with default marker image - customMarker = mapboxMap.addMarker(new MarkerOptions() - .title("Custom Marker") - .snippet(new DecimalFormat("#.#####").format(point.getLatitude()) + ", " - + new DecimalFormat("#.#####").format(point.getLongitude())) - .position(point)); - - return false; - } - - @Override protected void onStart() { super.onStart(); mapView.onStart(); @@ -159,6 +160,9 @@ public class InfoWindowActivity extends AppCompatActivity @Override protected void onDestroy() { super.onDestroy(); + if (mapboxMap != null) { + mapboxMap.removeOnMapLongClickListener(mapLongClickListener); + } mapView.onDestroy(); } |