Skip to content

Commit e52e7d0

Browse files
committed
Merge branch 'issue_647'
2 parents 33cbfac + f9e1317 commit e52e7d0

13 files changed

Lines changed: 519 additions & 47 deletions

File tree

plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@
174174
<header-file src="src/ios/GoogleMaps/NSData-Base64/NSData+Base64.h" />
175175
<header-file src="src/ios/GoogleMaps/NSData-Base64/NSData+Base64.podspec" />
176176
<source-file src="src/ios/GoogleMaps/NSData-Base64/NSData+Base64.m" />
177+
<header-file src="src/ios/GoogleMaps/MFGoogleMapAdditions/GMSCoordinateBounds+Geometry.h" />
178+
<source-file src="src/ios/GoogleMaps/MFGoogleMapAdditions/GMSCoordinateBounds+Geometry.m" />
177179
<header-file src="src/ios/GoogleMaps/MyPluginLayer.h" />
178180
<source-file src="src/ios/GoogleMaps/MyPluginLayer.m" />
179181
<header-file src="src/ios/GoogleMaps/MyReachability.h" />

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

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

3-
import java.io.FileInputStream;
4-
import java.io.IOException;
5-
import java.io.InputStream;
6-
import java.net.HttpURLConnection;
7-
import java.net.URL;
8-
import java.util.ArrayList;
9-
import java.util.Iterator;
10-
import java.util.Locale;
11-
import java.util.Random;
3+
import android.app.Activity;
4+
import android.app.ProgressDialog;
5+
import android.os.AsyncTask;
6+
import android.os.Bundle;
7+
import android.util.Log;
128

139
import org.apache.cordova.CallbackContext;
14-
import org.apache.cordova.CordovaWebView;
1510
import org.apache.cordova.PluginResult;
1611
import org.json.JSONArray;
1712
import org.json.JSONException;
@@ -20,11 +15,14 @@
2015
import org.xmlpull.v1.XmlPullParserException;
2116
import org.xmlpull.v1.XmlPullParserFactory;
2217

23-
import android.app.Activity;
24-
import android.app.ProgressDialog;
25-
import android.os.AsyncTask;
26-
import android.os.Bundle;
27-
import android.util.Log;
18+
import java.io.FileInputStream;
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.net.HttpURLConnection;
22+
import java.net.URL;
23+
import java.util.ArrayList;
24+
import java.util.Iterator;
25+
import java.util.Locale;
2826

2927
public class AsyncKmlParser extends AsyncTask<String, Void, Bundle> {
3028
private XmlPullParser parser;
@@ -437,19 +435,7 @@ protected void onPostExecute(Bundle parseResult) {
437435
this.mCallback.success(kmlId);
438436
}
439437

440-
441-
private abstract class MyCallbackContext extends CallbackContext {
442438

443-
public MyCallbackContext(String callbackId, CordovaWebView webView) {
444-
super(callbackId, webView);
445-
}
446-
@Override
447-
public void sendPluginResult(PluginResult pluginResult) {
448-
this.onResult(pluginResult);
449-
}
450-
451-
abstract public void onResult(PluginResult pluginResult);
452-
}
453439

454440
private void execOtherClassMethod(final JSONArray params, final CallbackContext callback) {
455441

@@ -472,7 +458,7 @@ private void implementToMap(final String className, final JSONObject optionsJSON
472458
JSONArray params = new JSONArray();
473459
params.put(className + ".create" + className);
474460
params.put(optionsJSON);
475-
AsyncKmlParser.this.execOtherClassMethod(params, new MyCallbackContext(kmlId +"_callback", mMapCtrl.webView) {
461+
AsyncKmlParser.this.execOtherClassMethod(params, new PluginUtil.MyCallbackContext(kmlId +"_callback", mMapCtrl.webView) {
476462

477463
@Override
478464
public void onResult(PluginResult pluginResult) {

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

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import com.google.android.gms.location.LocationSettingsRequest;
5151
import com.google.android.gms.location.LocationSettingsResult;
5252
import com.google.android.gms.location.LocationSettingsStatusCodes;
53+
import com.google.android.gms.maps.CameraUpdate;
54+
import com.google.android.gms.maps.CameraUpdateFactory;
5355
import com.google.android.gms.maps.GoogleMap;
5456
import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter;
5557
import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener;
@@ -129,6 +131,7 @@ private enum TEXT_STYLE_ALIGNMENTS {
129131
private GoogleApiClient googleApiClient = null;
130132
private JSONArray _saveArgs = null;
131133
private CallbackContext _saveCallbackContext = null;
134+
private LatLngBounds initCameraBounds;
132135

133136
@SuppressLint("NewApi") @Override
134137
public void initialize(final CordovaInterface cordova, final CordovaWebView webView) {
@@ -534,9 +537,19 @@ public void onClick(DialogInterface dialog,int id) {
534537
if (camera.has("bearing")) {
535538
builder.bearing((float) camera.getDouble("bearing"));
536539
}
537-
if (camera.has("latLng")) {
538-
JSONObject latLng = camera.getJSONObject("latLng");
539-
builder.target(new LatLng(latLng.getDouble("lat"), latLng.getDouble("lng")));
540+
if (camera.has("target")) {
541+
Object target = camera.get("target");
542+
@SuppressWarnings("rawtypes")
543+
Class targetClass = target.getClass();
544+
if ("org.json.JSONArray".equals(targetClass.getName())) {
545+
JSONArray points = camera.getJSONArray("target");
546+
initCameraBounds = PluginUtil.JSONArray2LatLngBounds(points);
547+
builder.target(initCameraBounds.getCenter());
548+
549+
} else {
550+
JSONObject latLng = camera.getJSONObject("target");
551+
builder.target(new LatLng(latLng.getDouble("lat"), latLng.getDouble("lng")));
552+
}
540553
}
541554
if (camera.has("tilt")) {
542555
builder.tilt((float) camera.getDouble("tilt"));
@@ -555,6 +568,7 @@ public void onClick(DialogInterface dialog,int id) {
555568
public void onMapReady(GoogleMap googleMap) {
556569

557570
map = googleMap;
571+
558572
try {
559573
//controls
560574
if (params.has("controls")) {
@@ -593,6 +607,16 @@ public void onMapReady(GoogleMap googleMap) {
593607
GoogleMaps.this.mapDivLayoutJSON = args.getJSONObject(1);
594608
mPluginLayout.attachMyView(mapView);
595609
GoogleMaps.this.resizeMap(args, callbackContext);
610+
} else {
611+
if (initCameraBounds != null) {
612+
Handler handler = new Handler();
613+
handler.postDelayed(new Runnable() {
614+
@Override
615+
public void run() {
616+
fitBounds(initCameraBounds);
617+
}
618+
}, 300);
619+
}
596620
}
597621
callbackContext.success();
598622
} catch (Exception e) {
@@ -762,6 +786,15 @@ private void showDialog(final JSONArray args, final CallbackContext callbackCont
762786
private void resizeMap(JSONArray args, CallbackContext callbackContext) throws JSONException {
763787
if (mPluginLayout == null) {
764788
callbackContext.success();
789+
if (initCameraBounds != null) {
790+
Handler handler = new Handler();
791+
handler.postDelayed(new Runnable() {
792+
@Override
793+
public void run() {
794+
fitBounds(initCameraBounds);
795+
}
796+
}, 100);
797+
}
765798
return;
766799
}
767800
mapDivLayoutJSON = args.getJSONObject(args.length() - 2);
@@ -771,6 +804,15 @@ private void resizeMap(JSONArray args, CallbackContext callbackContext) throws J
771804
float divW, divH, divLeft, divTop;
772805
if (mPluginLayout == null) {
773806
this.sendNoResult(callbackContext);
807+
if (initCameraBounds != null) {
808+
Handler handler = new Handler();
809+
handler.postDelayed(new Runnable() {
810+
@Override
811+
public void run() {
812+
fitBounds(initCameraBounds);
813+
}
814+
}, 100);
815+
}
774816
return;
775817
}
776818
this.mPluginLayout.clearHTMLElement();
@@ -792,9 +834,29 @@ private void resizeMap(JSONArray args, CallbackContext callbackContext) throws J
792834
}
793835
//mPluginLayout.inValidate();
794836
updateMapViewLayout();
837+
if (initCameraBounds != null) {
838+
Handler handler = new Handler();
839+
handler.postDelayed(new Runnable() {
840+
@Override
841+
public void run() {
842+
fitBounds(initCameraBounds);
843+
}
844+
}, 100);
845+
}
795846
this.sendNoResult(callbackContext);
796847
}
797848

849+
public void fitBounds(final LatLngBounds cameraBounds) {
850+
Builder builder = CameraPosition.builder();
851+
builder.tilt(map.getCameraPosition().tilt);
852+
builder.bearing(map.getCameraPosition().bearing);
853+
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(cameraBounds, (int)GoogleMaps.this.density);
854+
map.moveCamera(cameraUpdate);
855+
builder.zoom(map.getCameraPosition().zoom);
856+
builder.target(map.getCameraPosition().target);
857+
map.moveCamera(CameraUpdateFactory.newCameraPosition(builder.build()));
858+
}
859+
798860

799861
private void updateMapViewLayout() {
800862
if (mPluginLayout == null) {
@@ -1263,7 +1325,6 @@ public void onMarkerDragStart(Marker marker) {
12631325
/**
12641326
* Notify map event to JS
12651327
* @param eventName
1266-
* @param point
12671328
*/
12681329
private void onMapEvent(final String eventName) {
12691330
webView.loadUrl("javascript:plugin.google.maps.Map._onMapEvent('" + eventName + "')");

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

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import java.io.ByteArrayOutputStream;
44

55
import org.apache.cordova.CallbackContext;
6+
import org.apache.cordova.PluginResult;
67
import org.json.JSONArray;
78
import org.json.JSONException;
89
import org.json.JSONObject;
910

1011
import android.content.res.Resources;
1112
import android.graphics.Bitmap;
1213
import android.graphics.Point;
14+
import android.os.Handler;
1315
import android.util.Base64;
16+
import android.util.Log;
1417

1518
import com.google.android.gms.maps.CameraUpdate;
1619
import com.google.android.gms.maps.CameraUpdateFactory;
@@ -97,6 +100,7 @@ private void setOptions(JSONArray args, CallbackContext callbackContext) throws
97100

98101
// move the camera position
99102
if (params.has("camera")) {
103+
LatLngBounds cameraBounds = null;
100104
JSONObject camera = params.getJSONObject("camera");
101105
Builder builder = CameraPosition.builder();
102106
if (camera.has("bearing")) {
@@ -106,14 +110,32 @@ private void setOptions(JSONArray args, CallbackContext callbackContext) throws
106110
JSONObject latLng = camera.getJSONObject("latLng");
107111
builder.target(new LatLng(latLng.getDouble("lat"), latLng.getDouble("lng")));
108112
}
113+
114+
if (camera.has("target")) {
115+
CameraPosition newPosition;
116+
Object target = camera.get("target");
117+
@SuppressWarnings("rawtypes")
118+
Class targetClass = target.getClass();
119+
if ("org.json.JSONArray".equals(targetClass.getName())) {
120+
JSONArray points = camera.getJSONArray("target");
121+
cameraBounds = PluginUtil.JSONArray2LatLngBounds(points);
122+
builder.target(cameraBounds.getCenter());
123+
124+
} else {
125+
JSONObject latLng = camera.getJSONObject("target");
126+
builder.target(new LatLng(latLng.getDouble("lat"), latLng.getDouble("lng")));
127+
}
128+
}
109129
if (camera.has("tilt")) {
110130
builder.tilt((float) camera.getDouble("tilt"));
111131
}
112132
if (camera.has("zoom")) {
113133
builder.zoom((float) camera.getDouble("zoom"));
114134
}
115-
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(builder.build());
116-
map.moveCamera(cameraUpdate);
135+
map.moveCamera(CameraUpdateFactory.newCameraPosition(builder.build()));
136+
if (cameraBounds != null) {
137+
mapCtrl.fitBounds(cameraBounds);
138+
}
117139
}
118140

119141
this.sendNoResult(callbackContext);
@@ -192,7 +214,7 @@ private void updateCameraPosition(final String action, final JSONArray args, fin
192214

193215
int durationMS = 4000;
194216
CameraPosition.Builder builder = CameraPosition.builder();
195-
JSONObject cameraPos = args.getJSONObject(1);
217+
final JSONObject cameraPos = args.getJSONObject(1);
196218
if (cameraPos.has("tilt")) {
197219
builder.tilt((float) cameraPos.getDouble("tilt"));
198220
}
@@ -207,16 +229,16 @@ private void updateCameraPosition(final String action, final JSONArray args, fin
207229
}
208230
CameraPosition newPosition;
209231
CameraUpdate cameraUpdate = null;
232+
LatLngBounds cameraBounds = null;
210233
if (cameraPos.has("target")) {
211234
Object target = cameraPos.get("target");
212235
@SuppressWarnings("rawtypes")
213236
Class targetClass = target.getClass();
214237
JSONObject latLng;
215238
if ("org.json.JSONArray".equals(targetClass.getName())) {
216239
JSONArray points = cameraPos.getJSONArray("target");
217-
LatLngBounds bounds = PluginUtil.JSONArray2LatLngBounds(points);
218-
cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, (int)(20 * this.density));
219-
240+
cameraBounds = PluginUtil.JSONArray2LatLngBounds(points);
241+
cameraUpdate = CameraUpdateFactory.newLatLngBounds(cameraBounds, (int)(20 * this.density));
220242
} else {
221243
latLng = cameraPos.getJSONObject("target");
222244
builder.target(new LatLng(latLng.getDouble("lat"), latLng.getDouble("lng")));
@@ -228,11 +250,41 @@ private void updateCameraPosition(final String action, final JSONArray args, fin
228250
cameraUpdate = CameraUpdateFactory.newCameraPosition(builder.build());
229251
}
230252

231-
253+
final LatLngBounds finalCameraBounds = cameraBounds;
254+
PluginUtil.MyCallbackContext myCallback = new PluginUtil.MyCallbackContext("moveCamera", webView) {
255+
@Override
256+
public void onResult(final PluginResult pluginResult) {
257+
if (finalCameraBounds != null) {
258+
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(finalCameraBounds, (int)density);
259+
map.moveCamera(cameraUpdate);
260+
261+
262+
Builder builder = CameraPosition.builder();
263+
if (cameraPos.has("tilt")) {
264+
try {
265+
builder.tilt((float) cameraPos.getDouble("tilt"));
266+
} catch (JSONException e) {
267+
e.printStackTrace();
268+
}
269+
}
270+
if (cameraPos.has("bearing")) {
271+
try {
272+
builder.bearing((float) cameraPos.getDouble("bearing"));
273+
} catch (JSONException e) {
274+
e.printStackTrace();
275+
}
276+
}
277+
builder.zoom(map.getCameraPosition().zoom);
278+
builder.target(map.getCameraPosition().target);
279+
map.moveCamera(CameraUpdateFactory.newCameraPosition(builder.build()));
280+
}
281+
callbackContext.sendPluginResult(pluginResult);
282+
}
283+
};
232284
if (action.equals("moveCamera")) {
233-
myMoveCamera(cameraUpdate, callbackContext);
285+
myMoveCamera(cameraUpdate, myCallback);
234286
} else {
235-
myAnimateCamera(cameraUpdate, durationMS, callbackContext);
287+
myAnimateCamera(cameraUpdate, durationMS, myCallback);
236288
}
237289
}
238290

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
import java.util.List;
77
import java.util.Set;
88

9+
import org.apache.cordova.CallbackContext;
910
import org.apache.cordova.CordovaResourceApi;
11+
import org.apache.cordova.CordovaWebView;
12+
import org.apache.cordova.PluginResult;
1013
import org.json.JSONArray;
1114
import org.json.JSONException;
1215
import org.json.JSONObject;
@@ -34,7 +37,20 @@
3437
import com.google.android.gms.maps.model.LatLngBounds.Builder;
3538

3639
public class PluginUtil {
37-
40+
41+
public static abstract class MyCallbackContext extends CallbackContext {
42+
43+
public MyCallbackContext(String callbackId, CordovaWebView webView) {
44+
super(callbackId, webView);
45+
}
46+
@Override
47+
public void sendPluginResult(PluginResult pluginResult) {
48+
this.onResult(pluginResult);
49+
}
50+
51+
abstract public void onResult(PluginResult pluginResult);
52+
}
53+
3854
public static String getAbsolutePathFromCDVFilePath(CordovaResourceApi resourceApi, String cdvFilePath) {
3955
if (cdvFilePath.indexOf("cdvfile://") != 0) {
4056
return null;

0 commit comments

Comments
 (0)