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

feat(android): expose ImageOverlay image setter #702

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 5.6.1
version: 5.6.2
apiversion: 4
architectures: arm64-v8a armeabi-v7a x86 x86_64
description: External version of Map module using native Google Maps library
Expand Down
54 changes: 48 additions & 6 deletions android/src/ti/map/ImageOverlayProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@

package ti.map;

import com.google.android.gms.maps.model.BitmapDescriptor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.GroundOverlay;
import com.google.android.gms.maps.model.GroundOverlayOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import java.net.URL;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.view.TiDrawableReference;

@Kroll.proxy(creatableInModule = MapModule.class)
public class ImageOverlayProxy extends KrollProxy
{

private static final String TAG = "ImageOverlayProxy";

private static final String PROPERTY_BOUNDS_COORDINATE = "boundsCoordinate";
private static final String PROPERTY_IMAGE = "image";
private static final String PROPERTY_TOP_LEFT = "topLeft";
Expand All @@ -46,6 +49,12 @@ public void handleCreationDict(KrollDict dict)
}
}

@Kroll.setProperty
public void setImage(Object image)
{
handleImage(image);
}

public GroundOverlay getGroundOverlay()
{
return groundOverlay;
Expand All @@ -72,10 +81,28 @@ private void handleBoundsCoordinate(KrollDict boundsCoordinateDict)

private void handleImage(Object image)
{
TiDrawableReference source = TiDrawableReference.fromObject(this, image);
if (!source.isTypeNull()) {
BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromBitmap(source.getBitmap());
groundOverlayOptions.image(bitmapDescriptor);
if (image.toString().contains("https://") || image.toString().contains("http://")) {
// remote image
try {
Bitmap bmp = new DownloadImage().execute(image.toString()).get();
if (groundOverlay != null) {
groundOverlay.setImage(BitmapDescriptorFactory.fromBitmap(bmp));
} else {
groundOverlayOptions.image(BitmapDescriptorFactory.fromBitmap(bmp));
}
} catch (Exception ex) {
Log.e(TAG, "Error: " + ex.toString());
}
} else {
// local image
TiDrawableReference source = TiDrawableReference.fromObject(this, image);
if (!source.isTypeNull()) {
if (groundOverlay != null) {
groundOverlay.setImage(BitmapDescriptorFactory.fromBitmap(source.getBitmap()));
} else {
groundOverlayOptions.image(BitmapDescriptorFactory.fromBitmap(source.getBitmap()));
}
}
}
}

Expand All @@ -89,4 +116,19 @@ public String getApiName()
{
return "Ti.Map.ImageOverlay";
}

class DownloadImage extends AsyncTask<String, Void, Bitmap>
{
protected Bitmap doInBackground(String... urls)
{
try {
URL url = new URL(urls[0]);
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
return bmp;
} catch (Exception ex) {
Log.e(TAG, "Download error: " + ex.toString());
return null;
}
}
}
}
Loading