Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-9403] Add centerOffset-property handling for Android map annotations. #79

Merged
45 changes: 31 additions & 14 deletions android/src/ti/map/AnnotationProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package ti.map;

import java.util.HashMap;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
Expand All @@ -17,6 +19,7 @@
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.TiDimension;
import org.appcelerator.titanium.TiPoint;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.view.TiDrawableReference;
Expand Down Expand Up @@ -45,7 +48,8 @@
TiC.PROPERTY_LEFT_VIEW,
TiC.PROPERTY_RIGHT_BUTTON,
TiC.PROPERTY_RIGHT_VIEW,
MapModule.PROPERTY_SHOW_INFO_WINDOW
MapModule.PROPERTY_SHOW_INFO_WINDOW,
MapModule.PROPERTY_CENTER_OFFSET
})
public class AnnotationProxy extends KrollProxy
{
Expand All @@ -59,9 +63,11 @@ public interface AnnotationDelegate {
private TiMarker marker;
private TiMapInfoWindow infoWindow = null;
private static final String defaultIconImageHeight = "40dip"; //The height of the default marker icon
private static final String defaultIconImageWidth = "36dip"; //The width of the default marker icon
// The height of the marker icon in the unit of "px". Will use it to analyze the touch event to find out
// the correct clicksource for the click event.
private int iconImageHeight = 0;
private int iconImageWidth = 0;
private String annoTitle;
private AnnotationDelegate delegate = null;

Expand Down Expand Up @@ -203,9 +209,18 @@ public void processOptions()
handleImage(getProperty(TiC.PROPERTY_IMAGE));
} else if (hasProperty(TiC.PROPERTY_PINCOLOR)) {
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(TiConvert.toFloat(getProperty(TiC.PROPERTY_PINCOLOR))));
setIconImageHeight(-1);
setIconImageDimensions(-1, -1);
} else {
setIconImageHeight(-1);
setIconImageDimensions(-1, -1);
}

if (hasProperty(MapModule.PROPERTY_CENTER_OFFSET)) {
HashMap centerOffsetProperty = (HashMap) getProperty(MapModule.PROPERTY_CENTER_OFFSET);
TiPoint centerOffset = new TiPoint(centerOffsetProperty, 0.0, 0.0);

float offsetX = 0.5f - ((float) centerOffset.getX().getValue() / (float) iconImageWidth);
float offsetY = 0.5f - ((float) centerOffset.getY().getValue() / (float) iconImageHeight);
markerOptions.anchor(offsetX, offsetY);
}
}

Expand All @@ -216,13 +231,12 @@ private void handleCustomView(Object obj)
Bitmap image = imageBlob.getImage();
if (image != null) {
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(image));
setIconImageHeight(image.getHeight());
return;
setIconImageDimensions(image.getWidth(), image.getHeight());
}

return;
}
Log.w(TAG, "Unable to get the image from the custom view: " + obj);
setIconImageHeight(-1);
setIconImageDimensions(-1, -1);
}

private void handleImage(Object image)
Expand All @@ -233,7 +247,7 @@ private void handleImage(Object image)
Bitmap bitmap = imageref.getBitmap();
if (bitmap != null) {
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
setIconImageHeight(bitmap.getHeight());
setIconImageDimensions(bitmap.getWidth(), bitmap.getHeight());
return;
}
}
Expand All @@ -243,13 +257,13 @@ private void handleImage(Object image)
Bitmap bitmap = ((TiBlob) image).getImage();
if (bitmap != null) {
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(bitmap));
setIconImageHeight(bitmap.getHeight());
setIconImageDimensions(bitmap.getWidth(), bitmap.getHeight());
return;
}
}

Log.w(TAG, "Unable to get the image from the path: " + image);
setIconImageHeight(-1);
setIconImageDimensions(-1, -1);
}

public MarkerOptions getMarkerOptions()
Expand Down Expand Up @@ -294,15 +308,18 @@ public TiMapInfoWindow getMapInfoWindow()
return infoWindow;
}

private void setIconImageHeight(int h)
private void setIconImageDimensions(int w, int h)
{
if (h >= 0) {
if (w >= 0 && h >= 0) {
iconImageWidth = w;
iconImageHeight = h;
} else { // default maker icon
TiDimension dimension = new TiDimension(defaultIconImageHeight, TiDimension.TYPE_UNDEFINED);
TiDimension widthDimension = new TiDimension(defaultIconImageWidth, TiDimension.TYPE_UNDEFINED);
TiDimension heightDimension = new TiDimension(defaultIconImageHeight, TiDimension.TYPE_UNDEFINED);
// TiDimension needs a view to grab the window manager, so we'll just use the decorview of the current window
View view = TiApplication.getAppCurrentActivity().getWindow().getDecorView();
iconImageHeight = dimension.getAsPixels(view);
iconImageWidth = widthDimension.getAsPixels(view);
iconImageHeight = heightDimension.getAsPixels(view);
}
}

Expand Down
2 changes: 1 addition & 1 deletion android/src/ti/map/MapModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public class MapModule extends KrollModule
public static final String PROPERTY_BEARING = "bearing";
public static final String PROPERTY_ZOOM = "zoom";
public static final String PROPERTY_ZORDER_ON_TOP = "zOrderOnTop";
public static final String PROPERTY_CENTER_OFFSET = "centerOffset";
public static final String PROPERTY_PANNING = "panning";
public static final String PROPERTY_STREET_NAMES = "streetNames";
public static final String PROPERTY_USER_NAVIGATION = "userNavigation";

public static final String EVENT_PIN_CHANGE_DRAG_STATE = "pinchangedragstate";
public static final String EVENT_ON_SNAPSHOT_READY = "onsnapshotready";

Expand Down
2 changes: 1 addition & 1 deletion apidoc/Annotation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ properties:
Positive offset values move the annotation view down and right, while negative values
move it up and left.
type: Point
platforms: [iphone, ipad]
platforms: [android, iphone, ipad]

- name: customView
summary: Defines a custom view to be used by the annotation.
Expand Down