Skip to content

Commit

Permalink
(Dev): temporally save for issue #345
Browse files Browse the repository at this point in the history
  • Loading branch information
wf9a5m75 committed Jan 13, 2015
1 parent 27a6e06 commit 7ae9181
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 138 deletions.
Binary file removed platforms/android/libs/google-play-services.jar
Binary file not shown.
1 change: 1 addition & 0 deletions platforms/android/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion platforms/android/res/values/version.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer name="google_play_services_version">5089000</integer>
<integer name="google_play_services_version">6587000</integer>
</resources>
230 changes: 148 additions & 82 deletions src/android/plugin/google/maps/GoogleMaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Status> 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<Status>() {

}, 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() {
Expand Down
Loading

0 comments on commit 7ae9181

Please sign in to comment.