Skip to content

Commit 5ee3f26

Browse files
committed
Merge branch 'masashi_dev2' into test
2 parents 242c353 + c6338b5 commit 5ee3f26

7 files changed

Lines changed: 187 additions & 26 deletions

File tree

plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
<source-file src="src/android/plugin/google/maps/PluginPolygon.java" target-dir="src/plugin/google/maps" />
6767
<source-file src="src/android/plugin/google/maps/PluginPolyline.java" target-dir="src/plugin/google/maps" />
6868
<source-file src="src/android/plugin/google/maps/PluginTileOverlay.java" target-dir="src/plugin/google/maps" />
69+
<source-file src="src/android/plugin/google/maps/PluginTileProvider.java" target-dir="src/plugin/google/maps" />
6970
<source-file src="src/android/plugin/google/maps/PluginUtil.java" target-dir="src/plugin/google/maps" />
7071
<source-file src="src/android/plugin/google/maps/PluginAsyncInterface.java" target-dir="src/plugin/google/maps" />
7172

src/android/plugin/google/maps/PluginTileOverlay.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
package plugin.google.maps;
22

3-
import java.net.MalformedURLException;
4-
import java.net.URL;
5-
63
import org.apache.cordova.CallbackContext;
74
import org.json.JSONArray;
85
import org.json.JSONException;
96
import org.json.JSONObject;
107

118
import com.google.android.gms.maps.model.TileOverlay;
129
import com.google.android.gms.maps.model.TileOverlayOptions;
13-
import com.google.android.gms.maps.model.UrlTileProvider;
1410

1511
public class PluginTileOverlay extends MyPlugin implements MyPluginInterface {
1612

@@ -26,25 +22,14 @@ private void createTileOverlay(final JSONArray args,
2622
final CallbackContext callbackContext) throws JSONException {
2723

2824
JSONObject opts = args.getJSONObject(1);
29-
int tileWidth = opts.getInt("width");
30-
int tileHeight = opts.getInt("height");
25+
int tileSize = opts.getInt("tileSize");
3126
final String tileUrlFormat = opts.getString("tileUrlFormat");
32-
UrlTileProvider tileProvider = new UrlTileProvider(tileWidth, tileHeight) {
33-
34-
@Override
35-
public URL getTileUrl(int x, int y, int zoom) {
36-
String urlStr = tileUrlFormat.replaceAll("<x>", x + "")
37-
.replaceAll("<y>", y + "")
38-
.replaceAll("<zoom>", zoom + "");
39-
URL url = null;
40-
try {
41-
url = new URL(urlStr);
42-
} catch (MalformedURLException e) {
43-
e.printStackTrace();
44-
}
45-
return url;
46-
}
47-
};
27+
28+
double opacity = 1.0;
29+
if (opts.has("opacity")) {
30+
opacity = opts.getDouble("opacity");
31+
}
32+
PluginTileProvider tileProvider = new PluginTileProvider(tileUrlFormat, opacity, tileSize);
4833

4934
TileOverlayOptions options = new TileOverlayOptions();
5035
options.tileProvider(tileProvider);
@@ -56,7 +41,8 @@ public URL getTileUrl(int x, int y, int zoom) {
5641
}
5742
TileOverlay tileOverlay = this.map.addTileOverlay(options);
5843
String id = "tile_" + tileOverlay.getId();
59-
this.objects.put(id, tileOverlay);
44+
45+
this.objects.put("tileProvider_" + id, tileProvider);
6046

6147

6248
JSONObject result = new JSONObject();
@@ -104,6 +90,10 @@ protected void remove(JSONArray args, CallbackContext callbackContext) throws JS
10490
}
10591
tileOverlay.remove();
10692
tileOverlay.clearTileCache();
93+
94+
id = id.replace("tile_", "tileProvider_");
95+
this.objects.put(id, null);
96+
this.objects.remove(id);
10797
this.sendNoResult(callbackContext);
10898
}
10999
/**
@@ -130,4 +120,18 @@ protected void setFadeIn(JSONArray args, CallbackContext callbackContext) throws
130120
String id = args.getString(1);
131121
this.setBoolean("setFadeIn", id, visible, callbackContext);
132122
}
123+
/**
124+
* Set opacity for the tile layer
125+
* @param args
126+
* @param callbackContext
127+
* @throws JSONException
128+
*/
129+
protected void setOpacity(JSONArray args, CallbackContext callbackContext) throws JSONException {
130+
double opacity = args.getDouble(2);
131+
String id = args.getString(1);
132+
id = id.replace("tile_", "tileProvider_");
133+
134+
PluginTileProvider tileProvider = (PluginTileProvider)this.objects.get(id);
135+
tileProvider.setOpacity(opacity);
136+
}
133137
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package plugin.google.maps;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.InputStream;
5+
import java.net.HttpURLConnection;
6+
import java.net.URL;
7+
8+
import android.graphics.Bitmap;
9+
import android.graphics.Bitmap.Config;
10+
import android.graphics.BitmapFactory;
11+
import android.graphics.Canvas;
12+
import android.graphics.Matrix;
13+
import android.graphics.Paint;
14+
15+
import com.google.android.gms.maps.model.Tile;
16+
import com.google.android.gms.maps.model.TileProvider;
17+
18+
public class PluginTileProvider implements TileProvider {
19+
private String tileUrlFormat = null;
20+
private int tileSize = 256;
21+
private Paint tilePaint = new Paint(Paint.FILTER_BITMAP_FLAG);
22+
23+
public PluginTileProvider(String tileUrlFormat, double opacity, int tileSize) {
24+
this.tileUrlFormat = tileUrlFormat;
25+
this.tileSize = tileSize;
26+
this.tilePaint.setAlpha((int) (opacity * 255));
27+
}
28+
29+
@Override
30+
public Tile getTile(int x, int y, int zoom) {
31+
32+
String urlStr = tileUrlFormat.replaceAll("<x>", x + "")
33+
.replaceAll("<y>", y + "")
34+
.replaceAll("<zoom>", zoom + "");
35+
36+
try {
37+
InputStream inputStream = null;
38+
if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
39+
URL url = new URL(urlStr);
40+
HttpURLConnection http = (HttpURLConnection)url.openConnection();
41+
http.setRequestMethod("GET");
42+
http.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
43+
http.addRequestProperty("User-Agent", "Mozilla");
44+
http.setInstanceFollowRedirects(true);
45+
HttpURLConnection.setFollowRedirects(true);
46+
47+
boolean redirect = false;
48+
// normally, 3xx is redirect
49+
int status = http.getResponseCode();
50+
if (status != HttpURLConnection.HTTP_OK) {
51+
if (status == HttpURLConnection.HTTP_MOVED_TEMP
52+
|| status == HttpURLConnection.HTTP_MOVED_PERM
53+
|| status == HttpURLConnection.HTTP_SEE_OTHER)
54+
redirect = true;
55+
}
56+
if (redirect) {
57+
58+
// get redirect url from "location" header field
59+
String newUrl = http.getHeaderField("Location");
60+
61+
// get the cookie if need, for login
62+
String cookies = http.getHeaderField("Set-Cookie");
63+
64+
// open the new connection again
65+
http = (HttpURLConnection) new URL(newUrl).openConnection();
66+
http.setRequestProperty("Cookie", cookies);
67+
http.addRequestProperty("Accept-Language", "en-US,en;q=0.8");
68+
http.addRequestProperty("User-Agent", "Mozilla");
69+
}
70+
71+
inputStream = http.getInputStream();
72+
73+
Bitmap image = BitmapFactory.decodeStream(inputStream);
74+
Bitmap tileImage = this.resizeForTile(image);
75+
76+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
77+
tileImage.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
78+
byte[] byteArray = outputStream.toByteArray();
79+
Tile tile = new Tile(tileSize, tileSize, byteArray);
80+
81+
outputStream.close();
82+
//tileImage.recycle();
83+
image.recycle();
84+
inputStream.close();
85+
return tile;
86+
}
87+
88+
} catch (Exception e) {
89+
e.printStackTrace();
90+
}
91+
return null;
92+
}
93+
94+
public void setOpacity(double opacity) {
95+
this.tilePaint.setAlpha((int) (opacity * 255));
96+
}
97+
98+
private Bitmap resizeForTile(Bitmap bitmap) {
99+
100+
if (bitmap == null) {
101+
return null;
102+
}
103+
/**
104+
* http://stackoverflow.com/questions/4821488/bad-image-quality-after-resizing-scaling-bitmap#7468636
105+
*/
106+
Bitmap scaledBitmap = Bitmap.createBitmap(tileSize, tileSize, Config.ARGB_8888);
107+
108+
float ratioX = tileSize / (float) bitmap.getWidth();
109+
float ratioY = tileSize / (float) bitmap.getHeight();
110+
float middleX = tileSize / 2.0f;
111+
float middleY = tileSize / 2.0f;
112+
113+
Matrix scaleMatrix = new Matrix();
114+
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
115+
116+
Canvas canvas = new Canvas(scaledBitmap);
117+
canvas.setMatrix(scaleMatrix);
118+
canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, tilePaint);
119+
bitmap.recycle();
120+
121+
return scaledBitmap;
122+
}
123+
124+
}

src/android/plugin/google/maps/PluginUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public static Bundle Json2Bundle(JSONObject json) {
152152
}
153153
return mBundle;
154154
}
155-
155+
156156
public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
157157
if (bitmap == null) {
158158
return null;

src/ios/GoogleMaps/TileOverlay.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@
1818
-(void)clearTileCache:(CDVInvokedUrlCommand *)command;
1919
-(void)setZIndex:(CDVInvokedUrlCommand *)command;
2020
-(void)setFadeIn:(CDVInvokedUrlCommand *)command;
21+
-(void)setOpacity:(CDVInvokedUrlCommand *)command;
2122

2223
@end

src/ios/GoogleMaps/TileOverlay.m

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ -(void)createTileOverlay:(CDVInvokedUrlCommand *)command
3939
if ([json valueForKey:@"zIndex"]) {
4040
layer.zIndex = [[json valueForKey:@"zIndex"] floatValue];
4141
}
42+
if ([json valueForKey:@"tileSize"]) {
43+
layer.tileSize = [[json valueForKey:@"tileSize"] integerValue];
44+
}
45+
if ([json valueForKey:@"opacity"]) {
46+
layer.opacity = [[json valueForKey:@"opacity"] floatValue];
47+
}
4248

4349
NSString *id = [NSString stringWithFormat:@"tileOverlay_%lu", (unsigned long)layer.hash];
4450
[self.mapCtrl.overlayManager setObject:layer forKey: id];
@@ -134,4 +140,19 @@ -(void)setFadeIn:(CDVInvokedUrlCommand *)command
134140
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
135141
}
136142

143+
144+
/**
145+
* Set opacity
146+
* @params key
147+
*/
148+
-(void)setOpacity:(CDVInvokedUrlCommand *)command
149+
{
150+
NSString *tileLayerKey = [command.arguments objectAtIndex:1];
151+
GMSTileLayer *layer = [self.mapCtrl getTileLayerByKey:tileLayerKey];
152+
double opacity = [[command.arguments objectAtIndex:2] doubleValue];
153+
[layer setOpacity:opacity];
154+
155+
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
156+
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
157+
}
137158
@end

www/googlemaps-cdv-plugin.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,8 +1004,8 @@ App.prototype.addTileOverlay = function(tilelayerOptions, callback) {
10041004
}
10051005
tilelayerOptions.visible = tilelayerOptions.visible === undefined ? true : tilelayerOptions.visible;
10061006
tilelayerOptions.zIndex = tilelayerOptions.zIndex || 0;
1007-
tilelayerOptions.width = tilelayerOptions.width || 256;
1008-
tilelayerOptions.height = tilelayerOptions.height || 256;
1007+
tilelayerOptions.tileSize = tilelayerOptions.tileSize || 256;
1008+
tilelayerOptions.opacity = tilelayerOptions.opacity || 1;
10091009

10101010
cordova.exec(function(result) {
10111011
var tileOverlay = new TileOverlay(self, result.id, tilelayerOptions);
@@ -1650,6 +1650,9 @@ TileOverlay.prototype.clearTileCache = function() {
16501650
TileOverlay.prototype.getId = function() {
16511651
return this.id;
16521652
};
1653+
TileOverlay.prototype.getTileSize = function() {
1654+
return this.get("tileSize");
1655+
};
16531656
TileOverlay.prototype.getZIndex = function() {
16541657
return this.get("zIndex");
16551658
};
@@ -1670,6 +1673,13 @@ TileOverlay.prototype.setVisible = function(visible) {
16701673
this.set('visible', visible);
16711674
cordova.exec(null, this.errorHandler, PLUGIN_NAME, 'exec', ['TileOverlay.setVisible', this.getId(), visible]);
16721675
};
1676+
TileOverlay.prototype.getOpacity = function() {
1677+
return this.get('opacity');
1678+
};
1679+
TileOverlay.prototype.setOpacity = function(opacity) {
1680+
this.set('opacity', opacity);
1681+
cordova.exec(null, this.errorHandler, PLUGIN_NAME, 'exec', ['TileOverlay.setOpacity', this.getId(), opacity]);
1682+
};
16731683
TileOverlay.prototype.getVisible = function() {
16741684
return this.get('visible');
16751685
};

0 commit comments

Comments
 (0)