33import java .io .ByteArrayOutputStream ;
44
55import org .apache .cordova .CallbackContext ;
6+ import org .apache .cordova .PluginResult ;
67import org .json .JSONArray ;
78import org .json .JSONException ;
89import org .json .JSONObject ;
910
1011import android .content .res .Resources ;
1112import android .graphics .Bitmap ;
1213import android .graphics .Point ;
14+ import android .os .Handler ;
1315import android .util .Base64 ;
1416import 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
0 commit comments