Fix: map stops if you execute `map.setOptions({controls: {myLocationButton: false})` very after soon `map.getMap(div, {myLocationButton: false})`
MODIFY
plugin.xml
plugin.xml
<?xml version='1.0' encoding='utf-8'?>
-<plugin id="cordova-plugin-googlemaps" version="2.2.6" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
+<plugin id="cordova-plugin-googlemaps" version="2.2.6-beta-20180308-1614" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<name>cordova-plugin-googlemaps</name>
<js-module name="BaseClass" src="www/BaseClass.js">
<runs />

MODIFY
src/android/plugin/google/maps/PluginMap.java
src/android/plugin/google/maps/PluginMap.java
@@ -102,7 +102,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
//public final ConcurrentHashMap<String, Object> objects = new ConcurrentHashMap<String, Object>();
public final ObjectCache objects = new ObjectCache();
private ImageView dummyMyLocationButton;
-
+ public static final Object semaphore = new Object();
private enum TEXT_STYLE_ALIGNMENTS {
@@ -156,6 +156,33 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
if (controls.has("mapToolbar")) {
options.mapToolbarEnabled(controls.getBoolean("mapToolbar"));
}
+
+
+ if (controls.has("myLocationButton") || controls.has("myLocation")) {
+
+ // Request geolocation permission.
+ boolean locationPermission = PermissionChecker.checkSelfPermission(cordova.getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
+ //Log.d(TAG, "---> (235) hasPermission = " + locationPermission);
+
+ if (!locationPermission) {
+ //_saveArgs = args;
+ //_saveCallbackContext = callbackContext;
+ synchronized (semaphore) {
+ cordova.requestPermissions(PluginMap.this, callbackContext.hashCode(), new String[]{
+ Manifest.permission.ACCESS_FINE_LOCATION
+ });
+ try {
+ semaphore.wait();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+ locationPermission = PermissionChecker.checkSelfPermission(cordova.getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
+
+ //Log.d(TAG, "---> (252)setMyLocationEnabled, hasPermission = " + locationPermission);
+
+ }
+ }
}
//gestures
@@ -279,6 +306,30 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
map.setIndoorEnabled(isEnabled);
}
+ if (controls.has("myLocationButton") || controls.has("myLocation")) {
+ boolean locationPermission = PermissionChecker.checkSelfPermission(cordova.getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
+ //Log.d(TAG, "---> (314) hasPermission = " + locationPermission);
+
+ if (locationPermission) {
+ Boolean isMyLocationEnabled = false;
+ if (controls.has("myLocation")) {
+ isMyLocationEnabled = controls.getBoolean("myLocation");
+ map.setMyLocationEnabled(isMyLocationEnabled);
+ }
+
+ Boolean isMyLocationButtonEnabled = false;
+ if (controls.has("myLocationButton")) {
+ isMyLocationButtonEnabled = controls.getBoolean("myLocationButton");
+ map.getUiSettings().setMyLocationButtonEnabled(isMyLocationButtonEnabled);
+ }
+ //Log.d(TAG, "--->isMyLocationButtonEnabled = " + isMyLocationButtonEnabled + ", isMyLocationEnabled = " + isMyLocationEnabled);
+ if (!isMyLocationEnabled && isMyLocationButtonEnabled) {
+ dummyMyLocationButton.setVisibility(View.VISIBLE);
+ } else {
+ dummyMyLocationButton.setVisibility(View.GONE);
+ }
+ }
+ }
}
//preferences
if (params.has("preferences")) {
@@ -343,26 +394,6 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
mapView.onResume();
- if (params.has("controls")) {
- JSONObject controls = params.getJSONObject("controls");
-
- if (controls.has("myLocationButton") || controls.has("myLocation")) {
- final JSONArray args = new JSONArray();
- args.put(controls);
-
- cordova.getThreadPool().submit(new Runnable() {
- @Override
- public void run() {
- try {
- PluginMap.this.setMyLocationEnabled(args, callbackContext);
- } catch (JSONException e) {
- e.printStackTrace();
- callbackContext.error(e.getMessage() + "");
- }
- }
- });
- }
- }
// ------------------------------
// Embed the map if a container is specified.
@@ -444,7 +475,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
@Override
public void onPause(boolean multitasking) {
super.onPause(multitasking);
- if (mapView != null) {
+ if (mapView != null && mapView.isActivated()) {
mapView.onPause();
}
mapCtrl.mPluginLayout.stopTimer();
@@ -455,7 +486,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
@Override
public void onResume(boolean multitasking) {
super.onResume(multitasking);
- if (mapView != null) {
+ if (mapView != null && mapView.isActivated()) {
mapView.onResume();
}
mapCtrl.mPluginLayout.addPluginMap(PluginMap.this);
@@ -649,7 +680,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
RectF drawRect = mapCtrl.mPluginLayout.HTMLNodeRectFs.get(mapDivId);
- Log.d(TAG, "--->mapDivId = " + mapDivId + ", drawRect = " + drawRect);
+ //Log.d(TAG, "--->mapDivId = " + mapDivId + ", drawRect = " + drawRect);
if (drawRect != null) {
final int scrollY = webView.getView().getScrollY();
@@ -1100,11 +1131,9 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
* @throws JSONException
*/
public void setOptions(final JSONArray args, final CallbackContext callbackContext) throws JSONException {
- final AsyncTask<Void, Void, AsyncSetOptionsResult> task = new AsyncTask<Void, Void, AsyncSetOptionsResult>() {
- private Exception mException = null;
- @Override
- protected AsyncSetOptionsResult doInBackground(Void... Void) {
- AsyncSetOptionsResult results = new AsyncSetOptionsResult();
+
+
+ final AsyncSetOptionsResult results = new AsyncSetOptionsResult();
results.cameraPadding = DEFAULT_CAMERA_PADDING;
try {
@@ -1168,28 +1197,14 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
}
- } catch (Exception e) {
- mException = e;
- this.cancel(true);
- return null;
- }
- return results;
- }
-
- @Override
- public void onCancelled() {
- if (mException != null) {
- mException.printStackTrace();
- callbackContext.error("" + mException.getMessage());
- } else {
- callbackContext.error("");
- }
- }
+ cordova.getActivity().runOnUiThread(new Runnable() {
@Override
- public void onPostExecute(AsyncSetOptionsResult results) {
+ public void run() {
+ try {
+
if (results.cameraPosition != null) {
try {
map.moveCamera(CameraUpdateFactory.newCameraPosition(results.cameraPosition));
@@ -1210,9 +1225,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
map.setMapType(results.MAP_TYPE_ID);
}
-
JSONObject params = null;
- try {
params = args.getJSONObject(0);
UiSettings settings = map.getUiSettings();
@@ -1274,7 +1287,7 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
//controls
if (params.has("controls")) {
- JSONObject controls = params.getJSONObject("controls");
+ final JSONObject controls = params.getJSONObject("controls");
if (controls.has("compass")) {
settings.setCompassEnabled(controls.getBoolean("compass"));
@@ -1289,23 +1302,39 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
settings.setMapToolbarEnabled(controls.getBoolean("mapToolbar"));
}
if (controls.has("myLocation") || controls.has("myLocationButton")) {
+ cordova.getThreadPool().submit(new Runnable() {
+ @Override
+ public void run() {
JSONArray args = new JSONArray();
args.put(controls);
+ try {
PluginMap.this.setMyLocationEnabled(args, callbackContext);
+ } catch (JSONException e) {
+ e.printStackTrace();
+ callbackContext.error("error at map.setOptions()");
+ }
+ }
+ });
} else {
callbackContext.success();
}
} else {
callbackContext.success();
}
-
- } catch (JSONException e) {
+ } catch (Exception e) {
e.printStackTrace();
- callbackContext.error("" + e.getMessage());
+ callbackContext.error("error at map.setOptions()");
+
}
}
- };
- task.execute();
+ });
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ callbackContext.error("error at map.setOptions()");
+ }
+
+
}
@@ -1682,13 +1711,6 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
callbackContext.success();
}
- public void onRequestPermissionResult(int requestCode, String[] permissions,
- int[] grantResults) throws JSONException {
- synchronized (PluginLocationService.semaphore) {
- PluginLocationService.semaphore.notify();
- }
- }
-
/**
* Enable MyLocation feature if set true
@@ -1700,23 +1722,25 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
// Request geolocation permission.
- boolean locationPermission = PermissionChecker.checkSelfPermission(cordova.getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
- Log.d(TAG, "---> setMyLocationEnabled, hasPermission = " + locationPermission);
+ boolean locationPermission = PermissionChecker.checkSelfPermission(cordova.getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
+ //Log.d(TAG, "---> setMyLocationEnabled, hasPermission = " + locationPermission);
if (!locationPermission) {
//_saveArgs = args;
//_saveCallbackContext = callbackContext;
+ synchronized (semaphore) {
cordova.requestPermissions(this, callbackContext.hashCode(), new String[]{
- Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
+ Manifest.permission.ACCESS_FINE_LOCATION
});
- synchronized (PluginLocationService.semaphore) {
try {
- PluginLocationService.semaphore.wait();
+ semaphore.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
- locationPermission = PermissionChecker.checkSelfPermission(cordova.getActivity().getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
+ locationPermission = PermissionChecker.checkSelfPermission(cordova.getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) == PermissionChecker.PERMISSION_GRANTED;
+
+ //Log.d(TAG, "---> (1720)setMyLocationEnabled, hasPermission = " + locationPermission);
if (!locationPermission) {
callbackContext.error(PluginUtil.getPgmStrings(activity,"pgm_location_rejected_by_user"));
@@ -1731,14 +1755,17 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
@Override
public void run() {
try {
+
Boolean isMyLocationEnabled = false;
if (params.has("myLocation")) {
+ //Log.d(TAG, "--->myLocation = " + params.getBoolean("myLocation"));
isMyLocationEnabled = params.getBoolean("myLocation");
map.setMyLocationEnabled(isMyLocationEnabled);
}
Boolean isMyLocationButtonEnabled = false;
if (params.has("myLocationButton")) {
+ //Log.d(TAG, "--->myLocationButton = " + params.getBoolean("myLocationButton"));
isMyLocationButtonEnabled = params.getBoolean("myLocationButton");
map.getUiSettings().setMyLocationButtonEnabled(isMyLocationButtonEnabled);
}
@@ -2876,4 +2903,14 @@ public class PluginMap extends MyPlugin implements OnMarkerClickListener,
}
+ public void onRequestPermissionResult(int requestCode, String[] permissions,
+ int[] grantResults) throws JSONException {
+ //Log.d(TAG, "---> onRequestPermissionResult");
+
+ synchronized (semaphore) {
+ semaphore.notify();
+ }
+ }
+
+
}

MODIFY
www/Map.js
www/Map.js
@@ -95,7 +95,7 @@ Map.prototype.getMap = function(mapId, div, options) {
self.set("clickable", options.clickable === false ? false : true);
self.set("visible", options.visible === false ? false : true);
- if (options.control) {
+ if (options.controls) {
this.set("myLocation", options.controls.myLocation === true);
this.set("myLocationButton", options.controls.myLocationButton === true);
}
@@ -123,10 +123,6 @@ Map.prototype.getMap = function(mapId, div, options) {
this.set('camera_tilt', options.camera.tilt);
}
}
- if (options.controls) {
- this.set('myLocation', options.controls.myLocation === true);
- this.set('myLocationButton', options.controls.myLocationButton === true);
- }
args.push(options);
} else {
@@ -155,10 +151,6 @@ Map.prototype.getMap = function(mapId, div, options) {
this.set('camera_tilt', options.camera.tilt);
}
}
- if (options.controls) {
- this.set('myLocation', options.controls.myLocation === true);
- this.set('myLocationButton', options.controls.myLocationButton === true);
- }
if (utils.isArray(options.styles)) {
options.styles = JSON.stringify(options.styles);
}
@@ -251,9 +243,19 @@ Map.prototype.getMap = function(mapId, div, options) {
Map.prototype.setOptions = function(options) {
options = options || {};
- if (options.control) {
- this.set("myLocation", options.controls.myLocation === true);
- this.set("myLocationButton", options.controls.myLocationButton === true);
+ if (options.controls) {
+ var myLocation = this.get("myLocation");
+ if ("myLocation" in options.controls) {
+ myLocation = options.controls.myLocation === true;
+ }
+ var myLocationButton = this.get("myLocationButton");
+ if ("myLocationButton" in options.controls) {
+ myLocationButton = options.controls.myLocationButton === true;
+ }
+ this.set("myLocation", myLocation);
+ this.set("myLocationButton", myLocationButton);
+ options.controls.myLocation = myLocation;
+ options.controls.myLocationButton = myLocation;
}
if (options.camera) {
if (options.camera.latLng) {
@@ -541,7 +543,7 @@ Map.prototype.setMyLocationButtonEnabled = function(enabled) {
this.set("myLocationButton", enabled);
exec.call(this, null, this.errorHandler, this.id, 'setMyLocationEnabled', [{
myLocationButton: enabled,
- myLocation: self.get("myLocation")
+ myLocation: self.get("myLocation") === true
}], {
sync: true
});
@@ -553,7 +555,7 @@ Map.prototype.setMyLocationEnabled = function(enabled) {
enabled = common.parseBoolean(enabled);
this.set("myLocation", enabled);
exec.call(this, null, this.errorHandler, this.id, 'setMyLocationEnabled', [{
- myLocationButton: self.get("myLocationButton"),
+ myLocationButton: self.get("myLocationButton") === true,
myLocation: enabled
}], {
sync: true

Fix: map stops if you execute `map.setOptions({controls: {myLocationButton: false})` very after soon `map.getMap(div, {myLocationButton: false})` #2120