Skip to content

Commit

Permalink
Merge pull request #254 from hansemannn/android-show-annotations
Browse files Browse the repository at this point in the history
feat(android): add parity for „showAnnotations“, "zoomEnabled" and "scrollEnabled" + more
  • Loading branch information
garymathews authored Feb 5, 2019
2 parents 0d75a9f + ff29bf2 commit 3359e67
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 262 deletions.
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
buildModule {
// defaults:
// nodeVersion = '8.2.1' // Must have version set up on Jenkins master before it can be changed
sdkVersion = '7.4.0.v20180829013317' // use a master build with ARM64 support
sdkVersion = '7.4.2.v20181106131718' // use a master build with ARM64 support
// androidAPILevel = '23' // if changed, must install on build nodes
}
2 changes: 1 addition & 1 deletion android/manifest
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# this is your module manifest and used by Titanium
# during compilation, packaging, distribution, etc.
#
version: 4.3.3
version: 4.4.0
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86
description: External version of Map module to support new Google Map v2 sdk
Expand Down
2 changes: 2 additions & 0 deletions android/src/ti/map/MapModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class MapModule extends KrollModule
public static final String PROPERTY_SHOW_INFO_WINDOW = "showInfoWindow";
public static final String PROPERTY_USER_LOCATION_BUTTON = "userLocationButton";
public static final String PROPERTY_COMPASS_ENABLED = "compassEnabled";
public static final String PROPERTY_SCROLL_ENABLED = "scrollEnabled";
public static final String PROPERTY_ZOOM_ENABLED = "zoomEnabled";
public static final String PROPERTY_MAP_TOOLBAR_ENABLED = "mapToolbarEnabled";
public static final String PROPERTY_PADDING = "padding";
public static final String PROPERTY_TILT = "tilt";
Expand Down
4 changes: 2 additions & 2 deletions android/src/ti/map/PolygonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public boolean handleMessage(Message msg)
}
case MSG_SET_STROKE_WIDTH: {
result = (AsyncResult) msg.obj;
polygon.setStrokeWidth((Float) result.getArg());
polygon.setStrokeWidth((float) result.getArg());
result.setResult(null);
return true;
}
Expand All @@ -96,7 +96,7 @@ public boolean handleMessage(Message msg)
}
case MSG_SET_ZINDEX: {
result = (AsyncResult) msg.obj;
polygon.setZIndex((Float) result.getArg());
polygon.setZIndex((float) result.getArg());
result.setResult(null);
return true;
}
Expand Down
108 changes: 105 additions & 3 deletions android/src/ti/map/TiUIMapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,17 @@

package ti.map;

import org.json.JSONObject;
import org.json.JSONArray;
import org.json.JSONTokener;

import org.json.JSONException;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
Expand All @@ -19,6 +28,7 @@
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.view.TiUIFragment;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.io.TiFileFactory;

import ti.map.Shape.Boundary;
import ti.map.Shape.IShape;
Expand Down Expand Up @@ -280,6 +290,12 @@ public void processMapProperties(KrollDict d)
if (d.containsKey(MapModule.PROPERTY_COMPASS_ENABLED)) {
setCompassEnabled(TiConvert.toBoolean(d, MapModule.PROPERTY_COMPASS_ENABLED, true));
}
if (d.containsKey(MapModule.PROPERTY_SCROLL_ENABLED)) {
setScrollEnabled(TiConvert.toBoolean(d, MapModule.PROPERTY_SCROLL_ENABLED, true));
}
if (d.containsKey(MapModule.PROPERTY_ZOOM_ENABLED)) {
setZoomEnabled(TiConvert.toBoolean(d, MapModule.PROPERTY_ZOOM_ENABLED, true));
}
if (d.containsKey(TiC.PROPERTY_STYLE)) {
setStyle(d.getString(TiC.PROPERTY_STYLE));
}
Expand Down Expand Up @@ -314,6 +330,10 @@ public void propertyChanged(String key, Object oldValue, Object newValue, KrollP
updateAnnotations((Object[]) newValue);
} else if (key.equals(MapModule.PROPERTY_COMPASS_ENABLED)) {
setCompassEnabled(TiConvert.toBoolean(newValue, true));
} else if (key.equals(MapModule.PROPERTY_SCROLL_ENABLED)) {
setScrollEnabled(TiConvert.toBoolean(newValue, true));
} else if (key.equals(MapModule.PROPERTY_ZOOM_ENABLED)) {
setZoomEnabled(TiConvert.toBoolean(newValue, true));
} else if (key.equals(TiC.PROPERTY_ENABLE_ZOOM_CONTROLS)) {
setZoomControlsEnabled(TiConvert.toBoolean(newValue, true));
} else if (key.equals(TiC.PROPERTY_STYLE)) {
Expand All @@ -332,14 +352,31 @@ public GoogleMap getMap()

protected void setStyle(String style)
{
if (map != null && style != null && style != "") {
if (map != null && style != null && !style.isEmpty()) {
try {
// Handle .json files
if (style.endsWith(".json")) {
Object json = new JSONTokener(loadJSONFromAsset(style)).nextValue();

if (json instanceof JSONObject) {
style = ((JSONObject)json).toString();
} else if (json instanceof JSONArray) {
style = ((JSONArray)json).toString();
} else {
Log.e(TAG, "Invalid JSON style.");
}
}

// Handle raw JSON
boolean success = map.setMapStyle(new MapStyleOptions(style));

if (!success) {
Log.e("MapsActivityRaw", "Style parsing failed.");
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e("MapsActivityRaw", "Can't find style.", e);
Log.e(TAG, "Cannot find JSON style", e);
} catch (JSONException e) {
Log.e(TAG, "Cannot parse JSON", e);
}
}
}
Expand Down Expand Up @@ -423,13 +460,53 @@ protected void setPadding(int left, int top, int right, int bottom)
}
}

protected void showAnnotations(Object[] annotations) {
ArrayList<TiMarker> markers = new ArrayList<TiMarker>();

// Use supplied annotations first. If none available, select all (parity with iOS)
if (annotations != null) {
for (int i = 0; i < annotations.length; i++) {
Object annotation = annotations[i];
if (annotation instanceof AnnotationProxy) {
markers.add(((AnnotationProxy) annotation).getTiMarker());
}
}
} else {
markers = timarkers;
}

LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (TiMarker marker : markers) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();

int padding = 30;
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
map.animateCamera(cu);
}

protected void setZoomControlsEnabled(boolean enabled)
{
if (map != null) {
map.getUiSettings().setZoomControlsEnabled(enabled);
}
}

protected void setScrollEnabled(boolean enabled)
{
if (map != null) {
map.getUiSettings().setScrollGesturesEnabled(enabled);
}
}

protected void setZoomEnabled(boolean enabled)
{
if (map != null) {
map.getUiSettings().setZoomGesturesEnabled(enabled);
}
}

public void updateCamera(HashMap<String, Object> dict)
{
double longitude = 0;
Expand Down Expand Up @@ -936,6 +1013,31 @@ public void firePinChangeDragStateEvent(Marker marker, AnnotationProxy annoProxy
}
}

private String loadJSONFromAsset(String filename)
{
String json = null;

try {
String url = proxy.resolveUrl(null, filename);
InputStream inputStream = TiFileFactory.createTitaniumFile(new String[] { url }, false).getInputStream();
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int length;

while ((length = inputStream.read(buffer)) != -1) {
result.write(buffer, 0, length);
}

json = result.toString("UTF-8");
inputStream.close();
result.close();
} catch (IOException ex) {
Log.e(TAG, "Error opening file: " + ex.getMessage());
}

return json;
}

@Override
public boolean onMarkerClick(Marker marker)
{
Expand Down
35 changes: 34 additions & 1 deletion android/src/ti/map/ViewProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
propertyAccessors = { TiC.PROPERTY_USER_LOCATION, MapModule.PROPERTY_USER_LOCATION_BUTTON, TiC.PROPERTY_MAP_TYPE,
TiC.PROPERTY_REGION, TiC.PROPERTY_ANNOTATIONS, TiC.PROPERTY_ANIMATE,
MapModule.PROPERTY_TRAFFIC, TiC.PROPERTY_STYLE, TiC.PROPERTY_ENABLE_ZOOM_CONTROLS,
MapModule.PROPERTY_COMPASS_ENABLED, MapModule.PROPERTY_POLYLINES })
MapModule.PROPERTY_COMPASS_ENABLED, MapModule.PROPERTY_SCROLL_ENABLED, MapModule.PROPERTY_ZOOM_ENABLED,
MapModule.PROPERTY_POLYLINES })
public class ViewProxy extends TiViewProxy implements AnnotationDelegate
{
private static final String TAG = "MapViewProxy";
Expand All @@ -53,6 +54,7 @@ public class ViewProxy extends TiViewProxy implements AnnotationDelegate
private static final int MSG_SNAP_SHOT = MSG_FIRST_ID + 513;
private static final int MSG_SET_PADDING = MSG_FIRST_ID + 514;
private static final int MSG_ZOOM = MSG_FIRST_ID + 515;
private static final int MSG_SHOW_ANNOTATIONS = MSG_FIRST_ID + 516;

private static final int MSG_ADD_POLYGON = MSG_FIRST_ID + 901;
private static final int MSG_REMOVE_POLYGON = MSG_FIRST_ID + 902;
Expand Down Expand Up @@ -82,6 +84,8 @@ public ViewProxy()
super();
preloadRoutes = new ArrayList<RouteProxy>();
defaultValues.put(MapModule.PROPERTY_COMPASS_ENABLED, true);
defaultValues.put(MapModule.PROPERTY_SCROLL_ENABLED, true);
defaultValues.put(MapModule.PROPERTY_ZOOM_ENABLED, true);
preloadPolygons = new ArrayList<PolygonProxy>();
preloadPolylines = new ArrayList<PolylineProxy>();
preloadCircles = new ArrayList<CircleProxy>();
Expand Down Expand Up @@ -304,6 +308,13 @@ public boolean handleMessage(Message msg)
return true;
}

case MSG_SHOW_ANNOTATIONS: {
result = ((AsyncResult) msg.obj);
handleShowAnnotations((Object[]) result.getArg());
result.setResult(null);
return true;
}

default: {
return super.handleMessage(msg);
}
Expand Down Expand Up @@ -404,6 +415,28 @@ private void handleSnapshot()
}
}

@Kroll.method
public void showAnnotations(Object annotations) {
if (TiApplication.isUIThread()) {
handleShowAnnotations(annotations);
} else {
getMainHandler().obtainMessage(MSG_SHOW_ANNOTATIONS).sendToTarget();
}
}

private void handleShowAnnotations(Object annotations) {
if (!(annotations instanceof Object[])) {
Log.e(TAG, "Invalid argument to addAnnotations", Log.DEBUG_MODE);
return;
}
Object[] annos = (Object[]) annotations;

TiUIMapView mapView = (TiUIMapView) peekView();
if (mapView.getMap() != null) {
mapView.showAnnotations(annos);
}
}

@Kroll.method
public void removeAllAnnotations()
{
Expand Down
62 changes: 0 additions & 62 deletions anvil/configSet/Resources/hammer.js

This file was deleted.

Loading

0 comments on commit 3359e67

Please sign in to comment.