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 ;
16+ import android .util .Log ;
1417
1518import com .google .android .gms .maps .CameraUpdate ;
1619import 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
0 commit comments