From da4697a582a24fca6d985f43cae04d31039e1588 Mon Sep 17 00:00:00 2001 From: EddyVerbruggen Date: Tue, 13 Oct 2015 16:43:53 +0200 Subject: [PATCH] #4 title tap event handler (Android) --- plugin.xml | 7 ++++- src/android/Mapbox.java | 60 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/plugin.xml b/plugin.xml index bb7f35d..1ce6558 100755 --- a/plugin.xml +++ b/plugin.xml @@ -53,9 +53,14 @@ - + + + + + $ACCESS_TOKEN diff --git a/src/android/Mapbox.java b/src/android/Mapbox.java index 6388c14..f5a279c 100644 --- a/src/android/Mapbox.java +++ b/src/android/Mapbox.java @@ -2,20 +2,25 @@ import android.content.res.Resources; import android.util.DisplayMetrics; +import android.view.MotionEvent; +import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; -import com.mapbox.mapboxsdk.annotations.MarkerOptions; -import com.mapbox.mapboxsdk.annotations.PolygonOptions; -import com.mapbox.mapboxsdk.geometry.LatLng; -import com.mapbox.mapboxsdk.geometry.LatLngZoom; -import com.mapbox.mapboxsdk.views.MapView; +import com.mapbox.mapboxgl.annotations.Annotation; +import com.mapbox.mapboxgl.annotations.Marker; +import com.mapbox.mapboxgl.annotations.MarkerOptions; +import com.mapbox.mapboxgl.annotations.PolygonOptions; +import com.mapbox.mapboxgl.geometry.LatLng; +import com.mapbox.mapboxgl.geometry.LatLngZoom; +import com.mapbox.mapboxgl.views.MapView; import org.apache.cordova.CallbackContext; import org.apache.cordova.CordovaArgs; import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaWebView; +import org.apache.cordova.PluginResult; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -45,6 +50,7 @@ public class Mapbox extends CordovaPlugin { public static MapView mapView; private static float retinaFactor; private String accessToken; + private CallbackContext markerCallbackContext; @Override public void initialize(CordovaInterface cordova, CordovaWebView webView) { @@ -265,6 +271,9 @@ public void run() { } }); + } else if (ACTION_ADD_MARKER_CALLBACK.equals(action)) { + this.markerCallbackContext = callbackContext; + } else { return false; } @@ -283,6 +292,47 @@ private void addMarkers(JSONArray markers) throws JSONException { mo.snippet(marker.isNull("subtitle") ? null : marker.getString("subtitle")); mo.position(new LatLng(marker.getDouble("lat"), marker.getDouble("lng"))); mapView.addMarker(mo); + mo.getMarker().setInfoWindowOnTouchListener(new MarkerTouchListener(mo.getMarker().getId())); + } + } + + private class MarkerTouchListener implements View.OnTouchListener { + + public long markerId; + + public MarkerTouchListener(long markerId) { + this.markerId = markerId; + } + + @Override + public boolean onTouch(View v, MotionEvent event) { + if (event.getAction() == MotionEvent.ACTION_DOWN) { + // callback + if (markerCallbackContext != null) { + for (Annotation annotation : mapView.getAnnotations()) { + if (annotation.getId() == this.markerId) { + final Marker marker = (Marker) annotation; + final JSONObject json = new JSONObject(); + try { + json.put("title", marker.getTitle()); + json.put("subtitle", marker.getSnippet()); + json.put("lat", marker.getPosition().getLatitude()); + json.put("lng", marker.getPosition().getLongitude()); + } catch (JSONException e) { + PluginResult pluginResult = new PluginResult(PluginResult.Status.ERROR, + "Error in callback of " + ACTION_ADD_MARKER_CALLBACK + ": " + e.getMessage()); + pluginResult.setKeepCallback(true); + markerCallbackContext.sendPluginResult(pluginResult); + } + PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, json); + pluginResult.setKeepCallback(true); + markerCallbackContext.sendPluginResult(pluginResult); + return true; + } + } + } + } + return false; } }