Skip to content

Commit f9e1317

Browse files
committed
issue #647 The target property of map.getMap(), map.setOptions(), moveCamera(), and animateCamera() accept Array<LatLng> with tilt and bearing properties. (Android)
1 parent 73f939c commit f9e1317

4 files changed

Lines changed: 100 additions & 60 deletions

File tree

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: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private enum TEXT_STYLE_ALIGNMENTS {
131131
private GoogleApiClient googleApiClient = null;
132132
private JSONArray _saveArgs = null;
133133
private CallbackContext _saveCallbackContext = null;
134-
private CameraUpdate initCameraUpdate;
134+
private LatLngBounds initCameraBounds;
135135

136136
@SuppressLint("NewApi") @Override
137137
public void initialize(final CordovaInterface cordova, final CordovaWebView webView) {
@@ -538,22 +538,17 @@ public void onClick(DialogInterface dialog,int id) {
538538
builder.bearing((float) camera.getDouble("bearing"));
539539
}
540540
if (camera.has("target")) {
541-
CameraPosition newPosition;
542541
Object target = camera.get("target");
543542
@SuppressWarnings("rawtypes")
544543
Class targetClass = target.getClass();
545544
if ("org.json.JSONArray".equals(targetClass.getName())) {
546545
JSONArray points = camera.getJSONArray("target");
547-
LatLngBounds bounds = PluginUtil.JSONArray2LatLngBounds(points);
548-
initCameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, (int)this.density);
549-
builder.target(bounds.getCenter());
546+
initCameraBounds = PluginUtil.JSONArray2LatLngBounds(points);
547+
builder.target(initCameraBounds.getCenter());
550548

551549
} else {
552550
JSONObject latLng = camera.getJSONObject("target");
553-
Log.d("GoogleMaps", "----> " + latLng.getDouble("lat"));
554551
builder.target(new LatLng(latLng.getDouble("lat"), latLng.getDouble("lng")));
555-
newPosition = builder.build();
556-
initCameraUpdate = null; //CameraUpdateFactory.newCameraPosition(newPosition);
557552
}
558553
}
559554
if (camera.has("tilt")) {
@@ -613,14 +608,14 @@ public void onMapReady(GoogleMap googleMap) {
613608
mPluginLayout.attachMyView(mapView);
614609
GoogleMaps.this.resizeMap(args, callbackContext);
615610
} else {
616-
if (initCameraUpdate != null) {
611+
if (initCameraBounds != null) {
617612
Handler handler = new Handler();
618613
handler.postDelayed(new Runnable() {
619614
@Override
620615
public void run() {
621-
map.moveCamera(initCameraUpdate);
616+
fitBounds(initCameraBounds);
622617
}
623-
}, 500);
618+
}, 300);
624619
}
625620
}
626621
callbackContext.success();
@@ -791,12 +786,12 @@ private void showDialog(final JSONArray args, final CallbackContext callbackCont
791786
private void resizeMap(JSONArray args, CallbackContext callbackContext) throws JSONException {
792787
if (mPluginLayout == null) {
793788
callbackContext.success();
794-
if (initCameraUpdate != null) {
789+
if (initCameraBounds != null) {
795790
Handler handler = new Handler();
796791
handler.postDelayed(new Runnable() {
797792
@Override
798793
public void run() {
799-
map.moveCamera(initCameraUpdate);
794+
fitBounds(initCameraBounds);
800795
}
801796
}, 100);
802797
}
@@ -809,12 +804,12 @@ public void run() {
809804
float divW, divH, divLeft, divTop;
810805
if (mPluginLayout == null) {
811806
this.sendNoResult(callbackContext);
812-
if (initCameraUpdate != null) {
807+
if (initCameraBounds != null) {
813808
Handler handler = new Handler();
814809
handler.postDelayed(new Runnable() {
815810
@Override
816811
public void run() {
817-
map.moveCamera(initCameraUpdate);
812+
fitBounds(initCameraBounds);
818813
}
819814
}, 100);
820815
}
@@ -839,18 +834,29 @@ public void run() {
839834
}
840835
//mPluginLayout.inValidate();
841836
updateMapViewLayout();
842-
if (initCameraUpdate != null) {
837+
if (initCameraBounds != null) {
843838
Handler handler = new Handler();
844839
handler.postDelayed(new Runnable() {
845840
@Override
846841
public void run() {
847-
map.moveCamera(initCameraUpdate);
842+
fitBounds(initCameraBounds);
848843
}
849844
}, 100);
850845
}
851846
this.sendNoResult(callbackContext);
852847
}
853848

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+
854860

855861
private void updateMapViewLayout() {
856862
if (mPluginLayout == null) {
@@ -1319,7 +1325,6 @@ public void onMarkerDragStart(Marker marker) {
13191325
/**
13201326
* Notify map event to JS
13211327
* @param eventName
1322-
* @param point
13231328
*/
13241329
private void onMapEvent(final String eventName) {
13251330
webView.loadUrl("javascript:plugin.google.maps.Map._onMapEvent('" + eventName + "')");

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

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
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;
1416
import android.util.Log;
1517

@@ -98,7 +100,7 @@ private void setOptions(JSONArray args, CallbackContext callbackContext) throws
98100

99101
// move the camera position
100102
if (params.has("camera")) {
101-
CameraUpdate cameraUpdate = null;
103+
LatLngBounds cameraBounds = null;
102104
JSONObject camera = params.getJSONObject("camera");
103105
Builder builder = CameraPosition.builder();
104106
if (camera.has("bearing")) {
@@ -116,9 +118,8 @@ private void setOptions(JSONArray args, CallbackContext callbackContext) throws
116118
Class targetClass = target.getClass();
117119
if ("org.json.JSONArray".equals(targetClass.getName())) {
118120
JSONArray points = camera.getJSONArray("target");
119-
LatLngBounds bounds = PluginUtil.JSONArray2LatLngBounds(points);
120-
cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, (int)this.density);
121-
builder.target(bounds.getCenter());
121+
cameraBounds = PluginUtil.JSONArray2LatLngBounds(points);
122+
builder.target(cameraBounds.getCenter());
122123

123124
} else {
124125
JSONObject latLng = camera.getJSONObject("target");
@@ -131,8 +132,10 @@ private void setOptions(JSONArray args, CallbackContext callbackContext) throws
131132
if (camera.has("zoom")) {
132133
builder.zoom((float) camera.getDouble("zoom"));
133134
}
134-
CameraUpdate cameraUpdate2 = CameraUpdateFactory.newCameraPosition(builder.build());
135-
map.moveCamera(cameraUpdate != null ? cameraUpdate : cameraUpdate2);
135+
map.moveCamera(CameraUpdateFactory.newCameraPosition(builder.build()));
136+
if (cameraBounds != null) {
137+
mapCtrl.fitBounds(cameraBounds);
138+
}
136139
}
137140

138141
this.sendNoResult(callbackContext);
@@ -211,7 +214,7 @@ private void updateCameraPosition(final String action, final JSONArray args, fin
211214

212215
int durationMS = 4000;
213216
CameraPosition.Builder builder = CameraPosition.builder();
214-
JSONObject cameraPos = args.getJSONObject(1);
217+
final JSONObject cameraPos = args.getJSONObject(1);
215218
if (cameraPos.has("tilt")) {
216219
builder.tilt((float) cameraPos.getDouble("tilt"));
217220
}
@@ -226,16 +229,16 @@ private void updateCameraPosition(final String action, final JSONArray args, fin
226229
}
227230
CameraPosition newPosition;
228231
CameraUpdate cameraUpdate = null;
232+
LatLngBounds cameraBounds = null;
229233
if (cameraPos.has("target")) {
230234
Object target = cameraPos.get("target");
231235
@SuppressWarnings("rawtypes")
232236
Class targetClass = target.getClass();
233237
JSONObject latLng;
234238
if ("org.json.JSONArray".equals(targetClass.getName())) {
235239
JSONArray points = cameraPos.getJSONArray("target");
236-
LatLngBounds bounds = PluginUtil.JSONArray2LatLngBounds(points);
237-
cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, (int)(20 * this.density));
238-
240+
cameraBounds = PluginUtil.JSONArray2LatLngBounds(points);
241+
cameraUpdate = CameraUpdateFactory.newLatLngBounds(cameraBounds, (int)(20 * this.density));
239242
} else {
240243
latLng = cameraPos.getJSONObject("target");
241244
builder.target(new LatLng(latLng.getDouble("lat"), latLng.getDouble("lng")));
@@ -247,11 +250,41 @@ private void updateCameraPosition(final String action, final JSONArray args, fin
247250
cameraUpdate = CameraUpdateFactory.newCameraPosition(builder.build());
248251
}
249252

250-
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+
};
251284
if (action.equals("moveCamera")) {
252-
myMoveCamera(cameraUpdate, callbackContext);
285+
myMoveCamera(cameraUpdate, myCallback);
253286
} else {
254-
myAnimateCamera(cameraUpdate, durationMS, callbackContext);
287+
myAnimateCamera(cameraUpdate, durationMS, myCallback);
255288
}
256289
}
257290

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)