diff --git a/platforms/android/libs/google-play-services.jar b/platforms/android/libs/google-play-services.jar deleted file mode 100644 index 2b1aa4d49..000000000 Binary files a/platforms/android/libs/google-play-services.jar and /dev/null differ diff --git a/platforms/android/project.properties b/platforms/android/project.properties index ef68eb78a..175a44de7 100644 --- a/platforms/android/project.properties +++ b/platforms/android/project.properties @@ -12,3 +12,4 @@ # Project target. target=android-19 android.library.reference.1=CordovaLib +android.library.reference.2=../../../../../../../android-sdk/extras/google/google_play_services/libproject/google-play-services_lib diff --git a/platforms/android/res/values/version.xml b/platforms/android/res/values/version.xml index 0b7a4ed59..d9cb5eaea 100644 --- a/platforms/android/res/values/version.xml +++ b/platforms/android/res/values/version.xml @@ -1,4 +1,4 @@ - 5089000 + 6587000 \ No newline at end of file diff --git a/src/android/plugin/google/maps/GoogleMaps.java b/src/android/plugin/google/maps/GoogleMaps.java index b6e59a02b..17e6e9515 100644 --- a/src/android/plugin/google/maps/GoogleMaps.java +++ b/src/android/plugin/google/maps/GoogleMaps.java @@ -60,9 +60,13 @@ import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks; import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener; import com.google.android.gms.common.GooglePlayServicesUtil; -import com.google.android.gms.location.LocationClient; +import com.google.android.gms.common.api.GoogleApiClient; +import com.google.android.gms.common.api.PendingResult; +import com.google.android.gms.common.api.ResultCallback; +import com.google.android.gms.common.api.Status; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; +import com.google.android.gms.location.LocationServices; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.InfoWindowAdapter; import com.google.android.gms.maps.GoogleMap.OnCameraChangeListener; @@ -76,6 +80,7 @@ import com.google.android.gms.maps.GoogleMapOptions; import com.google.android.gms.maps.MapView; import com.google.android.gms.maps.MapsInitializer; +import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.Projection; import com.google.android.gms.maps.model.CameraPosition; import com.google.android.gms.maps.model.CameraPosition.Builder; @@ -114,8 +119,8 @@ private enum TEXT_STYLE_ALIGNMENTS { private final int LICENSE_LINK_ID = 0x7f99991; //random private final String PLUGIN_VERSION = "1.2.2"; private MyPluginLayout mPluginLayout = null; - private LocationClient locationClient = null; private boolean isDebug = false; + private GoogleApiClient googleApiClient = null; @SuppressLint("NewApi") @Override public void initialize(final CordovaInterface cordova, final CordovaWebView webView) { @@ -295,7 +300,7 @@ private void setVisible(JSONArray args, CallbackContext callbackContext) throws } @TargetApi(Build.VERSION_CODES.HONEYCOMB) - private void getMap(JSONArray args, final CallbackContext callbackContext) throws JSONException { + private void getMap(final JSONArray args, final CallbackContext callbackContext) throws JSONException { if (map != null) { callbackContext.success(); return; @@ -332,9 +337,6 @@ private void getMap(JSONArray args, final CallbackContext callbackContext) throw String errorMsg = "Google Maps Android API v2 is not available for some reason on this device. Do you install the latest Google Play Services from Google Play Store?"; switch (checkGooglePlayServices) { - case ConnectionResult.DATE_INVALID: - errorMsg = "It seems your device date is set incorrectly. Please update the correct date and time."; - break; case ConnectionResult.DEVELOPER_ERROR: errorMsg = "The application is misconfigured. This error is not recoverable and will be treated as fatal. The developer should look at the logs after this to determine more actionable information."; break; @@ -436,7 +438,7 @@ public void onClick(DialogInterface dialog,int id) { return; } GoogleMapOptions options = new GoogleMapOptions(); - JSONObject params = args.getJSONObject(0); + final JSONObject params = args.getJSONObject(0); //background color if (params.has("backgroundColor")) { JSONArray rgba = params.getJSONArray("backgroundColor"); @@ -523,47 +525,58 @@ public void onClick(DialogInterface dialog,int id) { mapView = new MapView(activity, options); mapView.onCreate(null); mapView.onResume(); - map = mapView.getMap(); - - //controls - if (params.has("controls")) { - JSONObject controls = params.getJSONObject("controls"); + mapView.getMapAsync(new OnMapReadyCallback() { + @Override + public void onMapReady(GoogleMap googleMap) { + + map = googleMap; - if (controls.has("myLocationButton")) { - Boolean isEnabled = controls.getBoolean("myLocationButton"); - map.setMyLocationEnabled(isEnabled); - map.getUiSettings().setMyLocationButtonEnabled(isEnabled); - } - if (controls.has("indoorPicker")) { - Boolean isEnabled = controls.getBoolean("indoorPicker"); - map.setIndoorEnabled(isEnabled); + try { + //controls + if (params.has("controls")) { + JSONObject controls = params.getJSONObject("controls"); + + if (controls.has("myLocationButton")) { + Boolean isEnabled = controls.getBoolean("myLocationButton"); + map.setMyLocationEnabled(isEnabled); + map.getUiSettings().setMyLocationButtonEnabled(isEnabled); + } + if (controls.has("indoorPicker")) { + Boolean isEnabled = controls.getBoolean("indoorPicker"); + map.setIndoorEnabled(isEnabled); + } + } + + // Set event listener + map.setOnCameraChangeListener(GoogleMaps.this); + map.setOnInfoWindowClickListener(GoogleMaps.this); + map.setOnMapClickListener(GoogleMaps.this); + map.setOnMapLoadedCallback(GoogleMaps.this); + map.setOnMapLongClickListener(GoogleMaps.this); + map.setOnMarkerClickListener(GoogleMaps.this); + map.setOnMarkerDragListener(GoogleMaps.this); + map.setOnMyLocationButtonClickListener(GoogleMaps.this); + + // Load PluginMap class + GoogleMaps.this.loadPlugin("Map"); + //Custom info window + map.setInfoWindowAdapter(GoogleMaps.this); + + // ------------------------------ + // Embed the map if a container is specified. + // ------------------------------ + if (args.length() == 3) { + GoogleMaps.this.mapDivLayoutJSON = args.getJSONObject(1); + mPluginLayout.attachMyView(mapView); + GoogleMaps.this.resizeMap(args, callbackContext); + } + callbackContext.success(); + } catch (Exception e) { + callbackContext.error(e.getMessage()); + } } - } + }); - // Set event listener - map.setOnCameraChangeListener(this); - map.setOnInfoWindowClickListener(this); - map.setOnMapClickListener(this); - map.setOnMapLoadedCallback(this); - map.setOnMapLongClickListener(this); - map.setOnMarkerClickListener(this); - map.setOnMarkerDragListener(this); - map.setOnMyLocationButtonClickListener(this); - - // Load PluginMap class - this.loadPlugin("Map"); - //Custom info window - map.setInfoWindowAdapter(this); - - callbackContext.success(); - // ------------------------------ - // Embed the map if a container is specified. - // ------------------------------ - if (args.length() == 3) { - this.mapDivLayoutJSON = args.getJSONObject(1); - mPluginLayout.attachMyView(mapView); - this.resizeMap(args, callbackContext); - } } private float contentToView(long d) { @@ -953,60 +966,113 @@ public void onClick(DialogInterface dialog, int which) { builder.create().show(); return; } - - Location location = locationManager.getLastKnownLocation(provider); - if (location != null) { - JSONObject result = PluginUtil.location2Json(location); - result.put("status", true); - callbackContext.success(result); - return; - } - PluginResult tmpResult = new PluginResult(PluginResult.Status.NO_RESULT); - tmpResult.setKeepCallback(true); - callbackContext.sendPluginResult(tmpResult); + if (googleApiClient == null) { + googleApiClient = new GoogleApiClient.Builder(this.activity) + .addApi(LocationServices.API) + .addConnectionCallbacks(new com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks() { + + @Override + public void onConnected(Bundle connectionHint) { + Log.e("Marker", "===> onConnected"); + PluginResult tmpResult = new PluginResult(PluginResult.Status.NO_RESULT); + tmpResult.setKeepCallback(true); + callbackContext.sendPluginResult(tmpResult); + + _requestLocationUpdate(enableHighAccuracy, callbackContext); + } + + @Override + public void onConnectionSuspended(int cause) { + Log.e("Marker", "===> onConnectionSuspended"); + } + + }) + .addOnConnectionFailedListener(new com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener() { + + @Override + public void onConnectionFailed(ConnectionResult result) { + Log.e("Marker", "===> onConnectionFailed"); + + PluginResult tmpResult = new PluginResult(PluginResult.Status.ERROR, result.toString()); + tmpResult.setKeepCallback(false); + callbackContext.sendPluginResult(tmpResult); + + googleApiClient.disconnect(); + } + + }) + .build(); + googleApiClient.connect(); + } else if (googleApiClient.isConnected()) { + _requestLocationUpdate(enableHighAccuracy, callbackContext); + } - locationClient = new LocationClient(this.activity, new ConnectionCallbacks() { + } + + private void _requestLocationUpdate(boolean enableHighAccuracy, final CallbackContext callbackContext) { - @Override - public void onConnected(Bundle bundle) { - LocationRequest request = new LocationRequest(); - int priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY; - if (enableHighAccuracy) { - priority = LocationRequest.PRIORITY_HIGH_ACCURACY; - } - request.setPriority(priority); - locationClient.requestLocationUpdates(request, new LocationListener() { + int priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY; + if (enableHighAccuracy) { + priority = LocationRequest.PRIORITY_HIGH_ACCURACY; + } + priority = LocationRequest.PRIORITY_HIGH_ACCURACY; + LocationRequest locationRequest = LocationRequest.create() + .setExpirationTime(5000) + .setNumUpdates(1) + .setSmallestDisplacement(0) + .setPriority(priority).setInterval(5000); + + final PendingResult result = LocationServices.FusedLocationApi.requestLocationUpdates( + googleApiClient, locationRequest, new LocationListener() { @Override public void onLocationChanged(Location location) { + Log.e("Marker", "===> onLocationChanged"); + /* + if (callbackContext.isFinished()) { + return; + } + */ JSONObject result; try { result = PluginUtil.location2Json(location); result.put("status", true); callbackContext.success(result); } catch (JSONException e) {} - locationClient.disconnect(); + + googleApiClient.disconnect(); } - + }); - } - - @Override - public void onDisconnected() {} + + result.setResultCallback(new ResultCallback() { - }, new OnConnectionFailedListener() { - - @Override - public void onConnectionFailed(ConnectionResult connectionResult) { - int errorCode = connectionResult.getErrorCode(); - String errorMsg = GooglePlayServicesUtil.getErrorString(errorCode); - PluginResult result = new PluginResult(PluginResult.Status.ERROR, errorMsg); - callbackContext.sendPluginResult(result); + public void onResult(Status status) { + Log.e("Marker", "===> onResult (success = " + status.isSuccess() + ")"); + if (!status.isSuccess()) { + String errorMsg = status.getStatusMessage(); + PluginResult result = new PluginResult(PluginResult.Status.ERROR, errorMsg); + callbackContext.sendPluginResult(result); + } else { + // no update location + Location location = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); + if (location != null) { + try { + JSONObject result = PluginUtil.location2Json(location); + result.put("status", true); + callbackContext.success(result); + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return; + } else { + Log.e("Marker", "====> waiting onLocationChanged"); + } + } } - }); - locationClient.connect(); } private void showLicenseText() { diff --git a/www/index.html b/www/index.html index 37f218da3..83ebb3f5a 100644 --- a/www/index.html +++ b/www/index.html @@ -7,68 +7,35 @@ Google Maps Plugin for Cordova - - -
+
+ +