Skip to content
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
22 changes: 22 additions & 0 deletions library/src/main/assets/google_map.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
var infoWindowContent = {};
var polylines = {};
var polygons = {};
var groundOverlays = {};
var moveTimeout;
var GeoMarker = null;
var markerIconPrefix;
Expand Down Expand Up @@ -226,6 +227,27 @@
}
}

function addGroundOverlay(id, north, south, east, west, image) {
var imageBounds = {
north: north,
south: south,
east: east,
west: west
};

var overlay = new google.maps.GroundOverlay(image, imageBounds);
overlay.setMap(map);

groundOverlays[id] = overlay;
}

function removeGroundOverlay(id) {
var overlay = groundOverlays[id];
if (overlay != null) {
overlay.setMap(null);
}
}

function addCircle(lat, lng, radius, strokeColor, strokeWeight, fillColor) {
var position = new google.maps.LatLng(lat, lng);
var populationOptions = {
Expand Down
17 changes: 17 additions & 0 deletions library/src/main/assets/mapbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
var markers = {};
var polylines = {};
var polygons = {};
var groundOverlays = {};

L.mapbox.accessToken = 'MAPBOX_ACCESS_TOKEN';
map = L.mapbox.map('map', 'MAPBOX_MAPID').setView([0, 0], 10);
Expand Down Expand Up @@ -177,6 +178,22 @@
}
}

function addGroundOverlay(id, north, south, east, west, image) {
var imageBounds = [[south, west], [north, east]];

var overlay = L.imageOverlay(image, imageBounds);
overlay.addTo(map);

groundOverlays[id] = overlay;
}

function removeGroundOverlay(id) {
var overlay = groundOverlays[id];
if (overlay != null) {
map.removeLayer(overlay);
}
}

function addCircle(lat, lng, radius, strokeColor, strokeWeight, fillColor) {
strokeColor = formatColor(strokeColor);
fillColor = formatColor(fillColor);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.airbnb.android.airmapview;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.DrawableRes;

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.LatLngBounds;

public class AirMapGroundOverlay {

private LatLngBounds bounds;

private String imageUrl;
private Bitmap bitmap;
private @DrawableRes int imageId;

private final float bearing;
private final float zIndex;
private final boolean visible;
private GroundOverlay googleOverlay;
private long id;

private AirMapGroundOverlay(long id, LatLngBounds bounds, String imageUrl, @DrawableRes int imageId, Bitmap bitmap, float bearing, float zIndex, boolean visible) {
this.id = id;
this.bounds = bounds;
this.imageUrl = imageUrl;
this.imageId = imageId;
this.bitmap = bitmap;
this.bearing = bearing;
this.zIndex = zIndex;
this.visible = visible;
}

public GroundOverlayOptions getOverlayOptions() {
return new GroundOverlayOptions()
.positionFromBounds(bounds)
.image(imageId != 0 ? BitmapDescriptorFactory.fromResource(imageId) : BitmapDescriptorFactory.fromBitmap(bitmap))
.bearing(bearing)
.zIndex(zIndex)
.visible(visible);
}



void setGoogleOverlay(GroundOverlay googleOverlay) {
this.googleOverlay = googleOverlay;
}

GroundOverlay getGoogleOverlay() {
return googleOverlay;
}

public long getId() {
return id;
}

public LatLngBounds getBounds() {
return bounds;
}

public String getImageUrl() {
return imageUrl;
}

public Bitmap getBitmap(Resources resources) {
return imageId != 0 ? BitmapFactory.decodeResource(resources, imageId) : bitmap;
}

public static class Builder {
private long id;
private LatLngBounds bounds;
private String imageUrl;
private @DrawableRes int imageId;
private Bitmap bitmap;
private float bearing;
private float zIndex;
private boolean visible = true;

public Builder() {

}

public Builder id(long id) {
this.id = id;
return this;
}

public Builder positionFromBounds(LatLngBounds bounds) {
this.bounds = bounds;
return this;
}

// TODO: imageUrl will not work for native maps
public Builder imageUrl(String imageUrl) {
this.imageUrl = imageUrl;
return this;
}

public Builder imageId(@DrawableRes int imageId) {
this.imageId = imageId;
return this;
}

public Builder bitmap(Bitmap bitmap) {
this.bitmap = bitmap;
return this;
}

public Builder bearing(float bearing) {
this.bearing = bearing;
return this;
}

public Builder zIndex(float zIndex) {
this.zIndex = zIndex;
return this;
}

public Builder visible(boolean visible) {
this.visible = visible;
return this;
}

public AirMapGroundOverlay build() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should do some validation here, for example throw an exception if we are missing both imageId and imageUrl

if (imageId == 0 && imageUrl == null) {
throw new IllegalStateException("You should provide an imageId or an imageUrl to your ground overlay.");
}
return new AirMapGroundOverlay(id, bounds, imageUrl, imageId, bitmap, bearing, zIndex, visible);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,18 @@ public interface AirMapInterface {

/** Get a Bitmap snapshot of the current */
void getSnapshot(OnSnapshotReadyListener listener);

/**
* Add the given ground overlay to the map
*
* @param overlay {@link AirMapGroundOverlay} instance to add
*/
void addGroundOverlay(AirMapGroundOverlay overlay);

/**
* Remove the given {@link AirMapGroundOverlay}
*
* @param overlay the {@link AirMapGroundOverlay} to remove
*/
void removeGroundOverlay(AirMapGroundOverlay overlay);
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,22 @@ public void clearGeoJsonLayer() {
mapInterface.clearGeoJsonLayer();
}

public boolean addGroundOverlay(AirMapGroundOverlay overlay) {
if (isInitialized()) {
mapInterface.addGroundOverlay(overlay);
return true;
}
return false;
}

public boolean removeGroundOverlay(AirMapGroundOverlay overlay) {
if (isInitialized()) {
mapInterface.removeGroundOverlay(overlay);
return true;
}
return false;
}

public boolean isInitialized() {
return mapInterface != null && mapInterface.isInitialized();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.CircleOptions;
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 com.google.android.gms.maps.model.Marker;
Expand Down Expand Up @@ -312,6 +314,16 @@ public void onMapClick(LatLng latLng) {
}
}

@Override public void addGroundOverlay(AirMapGroundOverlay overlay) {
GroundOverlayOptions overlayOptions = overlay.getOverlayOptions();
GroundOverlay groundOverlay = googleMap.addGroundOverlay(overlayOptions);
overlay.setGoogleOverlay(groundOverlay);
}

@Override public void removeGroundOverlay(AirMapGroundOverlay overlay) {
overlay.getGoogleOverlay().remove();
}

@Override public void setMapType(MapType type) {
int nativeType;
if (type == MapType.MAP_TYPE_NORMAL) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
Expand Down Expand Up @@ -37,6 +38,7 @@
import org.json.JSONObject;

import java.util.HashMap;
import java.io.ByteArrayOutputStream;
import java.util.Locale;
import java.util.Map;

Expand Down Expand Up @@ -293,6 +295,27 @@ public void setOnMarkerClickListener(OnMapMarkerClickListener listener) {
webView.loadUrl(String.format(Locale.US, "javascript:removePolygon(%1$d);", polygon.getId()));
}

private String bitmapToBase64String(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String imgageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
return "data:image/png;base64," + imgageBase64;
}

@Override public void addGroundOverlay(AirMapGroundOverlay overlay) {
webView.loadUrl(String.format(Locale.US,
"javascript:addGroundOverlay(%1$d, %2$f, %3$f, %4$f, %5$f, '%6$s');", overlay.getId(),
overlay.getBounds().northeast.latitude, overlay.getBounds().southwest.latitude,
overlay.getBounds().northeast.longitude, overlay.getBounds().southwest.longitude,
overlay.getImageUrl() != null ? overlay.getImageUrl() : bitmapToBase64String(overlay.getBitmap(getResources()))));
}

@Override public void removeGroundOverlay(AirMapGroundOverlay overlay) {
webView.loadUrl(String.format(Locale.US,
"javascript:removeGroundOverlay(%1$d);", overlay.getId()));
}

@Override public void setOnMapClickListener(final OnMapClickListener listener) {
onMapClickListener = listener;
}
Expand Down
2 changes: 1 addition & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:theme="@style/AppTheme.NoActionBar" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
Expand Down
Loading