Skip to content

Commit

Permalink
#4 title tap event handler (Android)
Browse files Browse the repository at this point in the history
  • Loading branch information
EddyVerbruggen committed Oct 13, 2015
1 parent 8cc22c5 commit da4697a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
7 changes: 6 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,14 @@
<resource-file src="src/android/libs/okhttp-2.4.0.jar" target="libs/okhttp-2.4.0.jar" />
<resource-file src="src/android/libs/okio-1.4.0.jar" target="libs/okio-1.4.0.jar" />

<source-file src="src/android/res/values/mapboxstrings.xml" target-dir="res/values" />
<!-- This leads to trouble in AppBuilder when compiling for Cordova-Android 4 -->
<!--source-file src="src/android/res/values/mapboxstrings.xml" target-dir="res/values" />
<config-file target="res/values/mapboxstrings.xml" parent="/*">
<string name="mapbox_accesstoken">$ACCESS_TOKEN</string>
</config-file-->

<config-file target="res/values/strings.xml" parent="/*">
<string name="mapbox_accesstoken">$ACCESS_TOKEN</string>
</config-file>
</platform>

Expand Down
60 changes: 55 additions & 5 deletions src/android/Mapbox.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -265,6 +271,9 @@ public void run() {
}
});

} else if (ACTION_ADD_MARKER_CALLBACK.equals(action)) {
this.markerCallbackContext = callbackContext;

} else {
return false;
}
Expand All @@ -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;
}
}

Expand Down

0 comments on commit da4697a

Please sign in to comment.