From e3cebcf8d4f0191989ce7444c5a27a2c70b6da7a Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Fri, 10 Jun 2016 14:39:25 -0500 Subject: [PATCH] [android] #5282 - Displaying Marker on map with geocoded address in an opened InfoWindow --- .../navigation/LocationPickerActivity.java | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/navigation/LocationPickerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/navigation/LocationPickerActivity.java index 6f3279193ad..48d6860024d 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/navigation/LocationPickerActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/navigation/LocationPickerActivity.java @@ -13,6 +13,8 @@ import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageView; +import com.mapbox.mapboxsdk.annotations.Marker; +import com.mapbox.mapboxsdk.annotations.MarkerOptions; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; @@ -20,6 +22,7 @@ import com.mapbox.mapboxsdk.testapp.R; import com.mapbox.services.commons.ServicesException; import com.mapbox.services.commons.models.Position; +import com.mapbox.services.commons.utils.TextUtils; import com.mapbox.services.geocoding.v5.GeocodingCriteria; import com.mapbox.services.geocoding.v5.MapboxGeocoding; import com.mapbox.services.geocoding.v5.models.GeocodingFeature; @@ -36,6 +39,9 @@ public class LocationPickerActivity extends AppCompatActivity { private MapView mapView; private MapboxMap mapboxMap; + private ImageView dropPinView = null; + private Marker resultsPin = null; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -60,7 +66,7 @@ public void onMapReady(MapboxMap map) { } }); - final ImageView dropPinView = new ImageView(this); + dropPinView = new ImageView(this); dropPinView.setImageResource(R.drawable.default_marker); FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER); dropPinView.setLayoutParams(params); @@ -74,10 +80,9 @@ public void onClick(View v) { Log.i(TAG, "Location Selected!"); if (mapboxMap != null) { Log.i(TAG, "dropPinView: height = " + dropPinView.getHeight() + "; width = " + dropPinView.getWidth() + "; left = " + dropPinView.getLeft() + "; bottom = " + dropPinView.getBottom()); - int x = dropPinView.getLeft() + (dropPinView.getWidth() / 2); - int y = dropPinView.getBottom(); - LatLng latLng = mapboxMap.getProjection().fromScreenLocation(new PointF(x, y)); - Log.i(TAG, "location: x = " + x + "; y = " + y + "; latLng = " + latLng); + float[] coords = getDropPinTipCoordinates(); + LatLng latLng = mapboxMap.getProjection().fromScreenLocation(new PointF(coords[0], coords[1])); + Log.i(TAG, "location: x = " + coords[0] + "; y = " + coords[1] + "; latLng = " + latLng); geocode(latLng); } } @@ -85,6 +90,13 @@ public void onClick(View v) { ); } + private float[] getDropPinTipCoordinates() { + float x = dropPinView.getLeft() + (dropPinView.getWidth() / 2); + float y = dropPinView.getBottom(); + + return new float[] {x, y}; + } + private void geocode(final LatLng point) { try { MapboxGeocoding client = new MapboxGeocoding.Builder() @@ -101,14 +113,17 @@ public void onResponse(Call call, Response if (results.size() > 0) { String address = results.get(0).getPlaceName(); Log.i(TAG, "address " + address); + showAddressPin(address); } else { Log.i(TAG, "No results for search."); + showAddressPin(null); } } @Override public void onFailure(Call call, Throwable t) { Log.e(TAG, "Geocoding Failure: " + t.getMessage()); + showAddressPin(null); } }); } catch (ServicesException e) { @@ -117,6 +132,29 @@ public void onFailure(Call call, Throwable t) { } } + private void showAddressPin(final String address) { + + if (resultsPin != null) { + mapboxMap.removeAnnotation(resultsPin); + } + + if (TextUtils.isEmpty(address)) { + // Set back to search mode + resultsPin = null; + dropPinView.setVisibility(View.VISIBLE); + + return; + } + + dropPinView.setVisibility(View.GONE); + + float[] coords = getDropPinTipCoordinates(); + LatLng latLng = mapboxMap.getProjection().fromScreenLocation(new PointF(coords[0], coords[1])); + + MarkerOptions markerOptions = new MarkerOptions().title(address).position(latLng); + resultsPin = mapboxMap.addMarker(markerOptions); + mapboxMap.selectMarker(resultsPin); + } @Override public void onResume() {