Skip to content

Commit 7fd0389

Browse files
author
masashiGMS
committed
issue #647 Add target property, deprecate latLng for mapOptions. (Android)
The `target` property accepts Array<LatLng>
1 parent adb6396 commit 7fd0389

2 files changed

Lines changed: 80 additions & 5 deletions

File tree

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

Lines changed: 59 additions & 3 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 CameraUpdate initCameraUpdate;
132135

133136
@SuppressLint("NewApi") @Override
134137
public void initialize(final CordovaInterface cordova, final CordovaWebView webView) {
@@ -534,9 +537,24 @@ 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+
CameraPosition newPosition;
542+
Object target = camera.get("target");
543+
@SuppressWarnings("rawtypes")
544+
Class targetClass = target.getClass();
545+
if ("org.json.JSONArray".equals(targetClass.getName())) {
546+
JSONArray points = camera.getJSONArray("target");
547+
LatLngBounds bounds = PluginUtil.JSONArray2LatLngBounds(points);
548+
initCameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, (int)this.density);
549+
builder.target(bounds.getCenter());
550+
551+
} else {
552+
JSONObject latLng = camera.getJSONObject("target");
553+
Log.d("GoogleMaps", "----> " + latLng.getDouble("lat"));
554+
builder.target(new LatLng(latLng.getDouble("lat"), latLng.getDouble("lng")));
555+
newPosition = builder.build();
556+
initCameraUpdate = null; //CameraUpdateFactory.newCameraPosition(newPosition);
557+
}
540558
}
541559
if (camera.has("tilt")) {
542560
builder.tilt((float) camera.getDouble("tilt"));
@@ -555,6 +573,7 @@ public void onClick(DialogInterface dialog,int id) {
555573
public void onMapReady(GoogleMap googleMap) {
556574

557575
map = googleMap;
576+
558577
try {
559578
//controls
560579
if (params.has("controls")) {
@@ -593,6 +612,16 @@ public void onMapReady(GoogleMap googleMap) {
593612
GoogleMaps.this.mapDivLayoutJSON = args.getJSONObject(1);
594613
mPluginLayout.attachMyView(mapView);
595614
GoogleMaps.this.resizeMap(args, callbackContext);
615+
} else {
616+
if (initCameraUpdate != null) {
617+
Handler handler = new Handler();
618+
handler.postDelayed(new Runnable() {
619+
@Override
620+
public void run() {
621+
map.moveCamera(initCameraUpdate);
622+
}
623+
}, 500);
624+
}
596625
}
597626
callbackContext.success();
598627
} catch (Exception e) {
@@ -762,6 +791,15 @@ private void showDialog(final JSONArray args, final CallbackContext callbackCont
762791
private void resizeMap(JSONArray args, CallbackContext callbackContext) throws JSONException {
763792
if (mPluginLayout == null) {
764793
callbackContext.success();
794+
if (initCameraUpdate != null) {
795+
Handler handler = new Handler();
796+
handler.postDelayed(new Runnable() {
797+
@Override
798+
public void run() {
799+
map.moveCamera(initCameraUpdate);
800+
}
801+
}, 100);
802+
}
765803
return;
766804
}
767805
mapDivLayoutJSON = args.getJSONObject(args.length() - 2);
@@ -771,6 +809,15 @@ private void resizeMap(JSONArray args, CallbackContext callbackContext) throws J
771809
float divW, divH, divLeft, divTop;
772810
if (mPluginLayout == null) {
773811
this.sendNoResult(callbackContext);
812+
if (initCameraUpdate != null) {
813+
Handler handler = new Handler();
814+
handler.postDelayed(new Runnable() {
815+
@Override
816+
public void run() {
817+
map.moveCamera(initCameraUpdate);
818+
}
819+
}, 100);
820+
}
774821
return;
775822
}
776823
this.mPluginLayout.clearHTMLElement();
@@ -792,6 +839,15 @@ private void resizeMap(JSONArray args, CallbackContext callbackContext) throws J
792839
}
793840
//mPluginLayout.inValidate();
794841
updateMapViewLayout();
842+
if (initCameraUpdate != null) {
843+
Handler handler = new Handler();
844+
handler.postDelayed(new Runnable() {
845+
@Override
846+
public void run() {
847+
map.moveCamera(initCameraUpdate);
848+
}
849+
}, 100);
850+
}
795851
this.sendNoResult(callbackContext);
796852
}
797853

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import android.graphics.Bitmap;
1212
import android.graphics.Point;
1313
import android.util.Base64;
14+
import android.util.Log;
1415

1516
import com.google.android.gms.maps.CameraUpdate;
1617
import com.google.android.gms.maps.CameraUpdateFactory;
@@ -97,6 +98,7 @@ private void setOptions(JSONArray args, CallbackContext callbackContext) throws
9798

9899
// move the camera position
99100
if (params.has("camera")) {
101+
CameraUpdate cameraUpdate = null;
100102
JSONObject camera = params.getJSONObject("camera");
101103
Builder builder = CameraPosition.builder();
102104
if (camera.has("bearing")) {
@@ -106,14 +108,31 @@ private void setOptions(JSONArray args, CallbackContext callbackContext) throws
106108
JSONObject latLng = camera.getJSONObject("latLng");
107109
builder.target(new LatLng(latLng.getDouble("lat"), latLng.getDouble("lng")));
108110
}
111+
112+
if (camera.has("target")) {
113+
CameraPosition newPosition;
114+
Object target = camera.get("target");
115+
@SuppressWarnings("rawtypes")
116+
Class targetClass = target.getClass();
117+
if ("org.json.JSONArray".equals(targetClass.getName())) {
118+
JSONArray points = camera.getJSONArray("target");
119+
LatLngBounds bounds = PluginUtil.JSONArray2LatLngBounds(points);
120+
cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds, (int)this.density);
121+
builder.target(bounds.getCenter());
122+
123+
} else {
124+
JSONObject latLng = camera.getJSONObject("target");
125+
builder.target(new LatLng(latLng.getDouble("lat"), latLng.getDouble("lng")));
126+
}
127+
}
109128
if (camera.has("tilt")) {
110129
builder.tilt((float) camera.getDouble("tilt"));
111130
}
112131
if (camera.has("zoom")) {
113132
builder.zoom((float) camera.getDouble("zoom"));
114133
}
115-
CameraUpdate cameraUpdate = CameraUpdateFactory.newCameraPosition(builder.build());
116-
map.moveCamera(cameraUpdate);
134+
CameraUpdate cameraUpdate2 = CameraUpdateFactory.newCameraPosition(builder.build());
135+
map.moveCamera(cameraUpdate != null ? cameraUpdate : cameraUpdate2);
117136
}
118137

119138
this.sendNoResult(callbackContext);

0 commit comments

Comments
 (0)