Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions sample/js/MapKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@

MapKit.prototype = {

showMap: function(success, error) {
cordovaRef.exec(success, error, 'MapKit', 'showMap', [this.options]);
showMap: function(success, error, options) {
if (options) {
cordovaRef.exec(success, error, 'MapKit', 'showMap', [options]);
} else {
cordovaRef.exec(success, error, 'MapKit', 'showMap', [this.options]);
}

},

addMapPins: function(pins, success, error) {
addMapPins: function(success, error, pins) {
cordovaRef.exec(success, error, 'MapKit', 'addMapPins', [pins]);
},

Expand All @@ -54,6 +59,14 @@

changeMapType: function(mapType, success, error) {
cordovaRef.exec(success, error, 'MapKit', 'changeMapType', [mapType ? { "mapType": mapType } :{ "mapType": 0 }]);
},

setMapData: function(success, error, options) {
if (options) {
cordovaRef.exec(success, error, 'MapKit', 'setMapData', [options]);
} else {
cordovaRef.exec(success, error, 'MapKit', 'setMapData', [this.options]);
}
}

};
Expand Down
292 changes: 229 additions & 63 deletions src/android/MapKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.json.JSONException;
import org.json.JSONObject;

import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
Expand All @@ -18,13 +20,17 @@
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.BitmapDescriptor;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.VisibleRegion;

public class MapKit extends CordovaPlugin {

Expand All @@ -33,6 +39,14 @@ public class MapKit extends CordovaPlugin {
protected MapView mapView;
private CallbackContext cCtx;
private String TAG = "MapKitPlugin";
private Marker lastClicked;

double latitude = 0, longitude = 0;
int height = 460;
boolean atBottom = false;
int offsetTop = 0;
int zoomLevel = 0;
boolean infoWindowOpen = false;

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
Expand All @@ -45,67 +59,160 @@ public void showMap(final JSONObject options) {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
double latitude = 0, longitude = 0;
int height = 460;
boolean atBottom = false;
try {
height = options.getInt("height");
latitude = options.getDouble("lat");
longitude = options.getDouble("lon");
atBottom = options.getBoolean("atBottom");
} catch (JSONException e) {
LOG.e(TAG, "Error reading options");
}
if (mapView != null) {
mapView.setVisibility(mapView.VISIBLE);
} else {
LOG.e(TAG, "hello world");
LOG.e(TAG, options);
try {
height = options.getInt("height");
latitude = options.getDouble("lat");
longitude = options.getDouble("lon");
offsetTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, options.getInt("offsetTop"), cordova.getActivity().getResources().getDisplayMetrics());
zoomLevel = options.getInt("zoomLevel");
atBottom = options.getBoolean("atBottom");
LOG.e(height);
} catch (JSONException e) {
LOG.e(TAG, "Error reading options");
}

final int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(cordova.getActivity());
if (resultCode == ConnectionResult.SUCCESS) {
mapView = new MapView(cordova.getActivity(),
new GoogleMapOptions());
root = (ViewGroup) webView.getParent();
root.removeView(webView);
main.addView(webView);
final int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(cordova.getActivity());
if (resultCode == ConnectionResult.SUCCESS) {
mapView = new MapView(cordova.getActivity(),
new GoogleMapOptions());
root = (ViewGroup) webView.getParent();
root.removeView(webView);
main.addView(webView);

cordova.getActivity().setContentView(main);
cordova.getActivity().setContentView(main);

MapsInitializer.initialize(cordova.getActivity());
MapsInitializer.initialize(cordova.getActivity());

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, height);
if (atBottom) {
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
} else {
params.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, height);
if (atBottom) {
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
mapView.setPadding(0, offsetTop, 0, 0);
} else {
params.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
mapView.setPadding(0, offsetTop, 0, 0);
}
params.addRule(RelativeLayout.CENTER_HORIZONTAL,
RelativeLayout.TRUE);
}
params.addRule(RelativeLayout.CENTER_HORIZONTAL,
RelativeLayout.TRUE);

mapView.setLayoutParams(params);
mapView.onCreate(null);
mapView.onResume(); // FIXME: I wish there was a better way
// than this...
main.addView(mapView);

// Moving the map to lot, lon
mapView.getMap().moveCamera(
CameraUpdateFactory.newLatLngZoom(new LatLng(
latitude, longitude), 15));
cCtx.success();

} else if (resultCode == ConnectionResult.SERVICE_MISSING ||
resultCode == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ||
resultCode == ConnectionResult.SERVICE_DISABLED) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, cordova.getActivity(), 1,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
cCtx.error("com.google.android.gms.common.ConnectionResult " + resultCode);
mapView.setLayoutParams(params);
mapView.onCreate(null);
mapView.onResume(); // FIXME: I wish there was a better way
// than this...
main.addView(mapView);

mapView.getMap().setMyLocationEnabled(true);
mapView.getMap().getUiSettings().setMyLocationButtonEnabled(false);

// Moving the map to lot, lon
mapView.getMap().moveCamera(
CameraUpdateFactory.newLatLngZoom(new LatLng(
latitude, longitude), 15));
cCtx.success();

mapView.getMap().setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(final Marker marker) {
webView.loadUrl(
"javascript:annotationTap('" +
marker.getSnippet() +
"'.toString(), " +
marker.getPosition().latitude +
", " +
marker.getPosition().longitude +
");");

//set variable so we can close it later
lastClicked = marker;
// Log.d("MYTAG", "on Marker click: " + marker.getSnippet());
// Log.d("MYTAG", "on Marker click: " + marker.getPosition().latitude);
// Log.d("MYTAG", "on Marker click: " + marker.getPosition().longitude);
return false;
}
});

mapView.getMap().setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
VisibleRegion vr = mapView.getMap().getProjection().getVisibleRegion();
double left = vr.latLngBounds.southwest.longitude;
double top = vr.latLngBounds.northeast.latitude;
double right = vr.latLngBounds.northeast.longitude;
double bottom = vr.latLngBounds.southwest.latitude;
double longDelta = left - right;
double latDelta = top - bottom;

webView.loadUrl(
"javascript:geo.onMapMove(" +
position.target.latitude +
"," +
position.target.longitude +
"," +
latDelta +
"," +
longDelta +
");");
// Log.d("MYTAG", "on Marker click: " + marker.getSnippet());
// Log.d("MYTAG", "on Marker click: " + marker.getPosition().latitude);
// Log.d("MYTAG", "on Marker click: " + marker.getPosition().longitude);
return;
}
});

// set variables when infoWindows open, so we can tell when they close
mapView.getMap().setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(final Marker marker) {
// Log.d("MYTAG", "on infowindow: " + marker);
if (infoWindowOpen == false) {
infoWindowOpen = true;
}

return null;
}

@Override
public View getInfoContents(Marker marker) {
return null;
}
});

// when the map is clicked (not a pin or an infowindow),
// find out if we just closed an infowindow and if so, call a javascript function
mapView.getMap().setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(final LatLng latlng) {

if (infoWindowOpen == true) {
//Log.d("MYTAG", "on infowindow close: " + latlng.latitude);
infoWindowOpen = false;
webView.loadUrl("javascript:annotationDeselect();");
}
}
});

} else if (resultCode == ConnectionResult.SERVICE_MISSING ||
resultCode == ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED ||
resultCode == ConnectionResult.SERVICE_DISABLED) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, cordova.getActivity(), 1,
new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
cCtx.error("com.google.android.gms.common.ConnectionResult " + resultCode);
}
}
}
);
dialog.show();
}
);
dialog.show();
}
}


}
});
Expand All @@ -114,21 +221,79 @@ public void onCancel(DialogInterface dialog) {
cCtx.error("MapKitPlugin::showMap(): An exception occured");
}
}

public void setMapData(final JSONObject options) {
//Log.d("MYTAG", "setMapData");
try {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,options.getInt("height"), cordova.getActivity().getResources().getDisplayMetrics());
latitude = options.getDouble("lat");
longitude = options.getDouble("lon");
offsetTop = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,options.getInt("offsetTop"), cordova.getActivity().getResources().getDisplayMetrics());
zoomLevel = options.getInt("zoomLevel");
atBottom = options.getBoolean("atBottom");
} catch (JSONException e) {
LOG.e(TAG, "Error reading options");
}

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, height + offsetTop);
if (atBottom) {
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
mapView.setPadding(0, offsetTop, 0, 0);
} else {
params.addRule(RelativeLayout.ALIGN_PARENT_TOP,
RelativeLayout.TRUE);
mapView.setPadding(0, offsetTop, 0, 0);
}
params.addRule(RelativeLayout.CENTER_HORIZONTAL,
RelativeLayout.TRUE);

mapView.setLayoutParams(params);

mapView.getMap().animateCamera(
CameraUpdateFactory.newLatLngZoom(new LatLng(
latitude, longitude), zoomLevel));
cCtx.success();
}
});
} catch (Exception e) {
e.printStackTrace();
cCtx.error("MapKitPlugin::showMap(): An exception occured");
}
}

private void hideMap() {
try {
cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (mapView != null) {
mapView.onDestroy();
main.removeView(webView);
main.removeView(mapView);
root.addView(webView);
cordova.getActivity().setContentView(root);
mapView = null;
cCtx.success();
}
String hideMethod = "";
// if we're not destroying the map, then just hide it...
if (mapView != null && !hideMethod.equals("destroy")) {
//Log.d("MYTAG", "true");
// AlphaAnimation animation2 = new AlphaAnimation(1.0f, 0.0f);
// animation2.setDuration(1000);
// mapView.startAnimation(animation2);
if (lastClicked != null) {
lastClicked.hideInfoWindow();
}
mapView.setVisibility(mapView.GONE);
cCtx.success();
} else {
//Log.d("MYTAG", "false");
mapView.onDestroy();
main.removeView(webView);
main.removeView(mapView);
root.addView(webView);
cordova.getActivity().setContentView(root);
mapView = null;
cCtx.success();
}
}
});
} catch (Exception e) {
Expand All @@ -146,6 +311,7 @@ public void run() {
try {
for (int i = 0, j = pins.length(); i < j; i++) {
double latitude = 0, longitude = 0;

JSONObject options = pins.getJSONObject(i);
latitude = options.getDouble("lat");
longitude = options.getDouble("lon");
Expand Down
6 changes: 6 additions & 0 deletions src/ios/MapKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@

- (void)addMapPins:(CDVInvokedUrlCommand *)command;

/* custom addition */
- (void)setViewWithOptions:(NSDictionary *)options;

- (void)setMapData:(CDVInvokedUrlCommand *)command;
/* end custom addition */

@end
Loading