diff --git a/sample/js/MapKit.js b/sample/js/MapKit.js index 043e2aa..f6728b6 100644 --- a/sample/js/MapKit.js +++ b/sample/js/MapKit.js @@ -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]); }, @@ -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]); + } } }; diff --git a/src/android/MapKit.java b/src/android/MapKit.java index d6b0823..2382c05 100644 --- a/src/android/MapKit.java +++ b/src/android/MapKit.java @@ -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; @@ -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 { @@ -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) { @@ -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(); + } + } + } }); @@ -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) { @@ -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"); diff --git a/src/ios/MapKit.h b/src/ios/MapKit.h index 291a1cb..23ba2a0 100644 --- a/src/ios/MapKit.h +++ b/src/ios/MapKit.h @@ -34,4 +34,10 @@ - (void)addMapPins:(CDVInvokedUrlCommand *)command; +/* custom addition */ +- (void)setViewWithOptions:(NSDictionary *)options; + +- (void)setMapData:(CDVInvokedUrlCommand *)command; +/* end custom addition */ + @end diff --git a/src/ios/MapKit.m b/src/ios/MapKit.m index bc29117..ffd5e53 100644 --- a/src/ios/MapKit.m +++ b/src/ios/MapKit.m @@ -101,21 +101,21 @@ - (void)clearMapPins:(CDVInvokedUrlCommand *)command - (void)addMapPins:(CDVInvokedUrlCommand *)command { - + NSArray *pins = command.arguments[0]; - - for (int y = 0; y < pins.count; y++) + + for (int y = 0; y < pins.count; y++) { NSDictionary *pinData = [pins objectAtIndex:y]; - CLLocationCoordinate2D pinCoord = { [[pinData objectForKey:@"lat"] floatValue] , [[pinData objectForKey:@"lon"] floatValue] }; - NSString *title=[[pinData valueForKey:@"title"] description]; - NSString *subTitle=[[pinData valueForKey:@"snippet"] description]; - NSInteger index=[[pinData valueForKey:@"index"] integerValue]; - BOOL selected = [[pinData valueForKey:@"selected"] boolValue]; - + CLLocationCoordinate2D pinCoord = { [[pinData objectForKey:@"lat"] floatValue] , [[pinData objectForKey:@"lon"] floatValue] }; + NSString *title=[[pinData valueForKey:@"title"] description]; + NSString *subTitle=[[pinData valueForKey:@"snippet"] description]; + NSInteger index=[[pinData valueForKey:@"index"] integerValue]; + BOOL selected = [[pinData valueForKey:@"selected"] boolValue]; + NSString *pinColor = nil; NSString *imageURL = nil; - + if([[pinData valueForKey:@"icon"] isKindOfClass:[NSNumber class]]) { pinColor = [[pinData valueForKey:@"icon"] description]; @@ -126,17 +126,172 @@ - (void)addMapPins:(CDVInvokedUrlCommand *)command pinColor = [[iconOptions valueForKey:@"pinColor" ] description]; imageURL=[[iconOptions valueForKey:@"resource"] description]; } + + CDVAnnotation *annotation = [[CDVAnnotation alloc] initWithCoordinate:pinCoord index:index title:title subTitle:subTitle imageURL:imageURL]; + annotation.pinColor=pinColor; + annotation.selected = selected; + + [self.mapView addAnnotation:annotation]; + [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId]; + } + +} - CDVAnnotation *annotation = [[CDVAnnotation alloc] initWithCoordinate:pinCoord index:index title:title subTitle:subTitle imageURL:imageURL]; - annotation.pinColor=pinColor; - annotation.selected = selected; +/* custom addition */ +/** + * Set annotations and mapview settings + */ - [self.mapView addAnnotation:annotation]; - [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId]; - } +- (void)setViewWithOptions:(NSDictionary *)options { + + // defaults + CGFloat height = 480.0f; + CGFloat offsetTop = 0.0f; + + if ([options objectForKey:@"height"]) + { + height=[[options objectForKey:@"height"] floatValue]; + } + if ([options objectForKey:@"offsetTop"]) + { + offsetTop=[[options objectForKey:@"offsetTop"] floatValue]; + } + if ([options objectForKey:@"buttonCallback"]) + { + self.buttonCallback=[[options objectForKey:@"buttonCallback"] description]; + } + + CLLocationCoordinate2D centerCoord = { [[options objectForKey:@"lat"] floatValue] , [[options objectForKey:@"lon"] floatValue] }; + CLLocationDistance diameter = [[options objectForKey:@"diameter"] floatValue]; + + CGRect webViewBounds = self.webView.bounds; + + CGRect mapBoundsChildView; + CGRect mapBoundsMapView; + mapBoundsChildView = CGRectMake( + webViewBounds.origin.x, + webViewBounds.origin.y + (offsetTop), + webViewBounds.size.width, + webViewBounds.origin.y + height + ); + mapBoundsMapView = CGRectMake( + webViewBounds.origin.x, + webViewBounds.origin.y, + webViewBounds.size.width, + webViewBounds.origin.y + height + ); + + //[self setFrame:mapBounds]; + [self.childView setFrame:mapBoundsChildView]; + [self.mapView setFrame:mapBoundsMapView]; + + MKCoordinateRegion region=[ self.mapView regionThatFits: MKCoordinateRegionMakeWithDistance(centerCoord, + diameter*(height / webViewBounds.size.width), + diameter*(height / webViewBounds.size.width))]; + [self.mapView setRegion:region animated:YES]; + + CGRect frame = CGRectMake(285.0,12.0, 29.0, 29.0); + + [ self.imageButton setImage:[UIImage imageNamed:@"www/map-close-button.png"] forState:UIControlStateNormal]; + [ self.imageButton setFrame:frame]; + [ self.imageButton addTarget:self action:@selector(closeButton:) forControlEvents:UIControlEventTouchUpInside]; +} + +- (void)setMapData:(CDVInvokedUrlCommand *)command +{ + [self setViewWithOptions:command.arguments[0]]; + [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_OK] callbackId:command.callbackId]; +} + +- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id ) annotation { + + if ([annotation class] != CDVAnnotation.class) { + return nil; + } + + CDVAnnotation *phAnnotation=(CDVAnnotation *) annotation; + NSString *identifier=[NSString stringWithFormat:@"INDEX[%i]", phAnnotation.index]; + + MKPinAnnotationView *annView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:identifier]; + + if (annView!=nil) return annView; + + annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; + + annView.animatesDrop=YES; + annView.canShowCallout = YES; + NSLog(phAnnotation.pinColor); + if ([phAnnotation.pinColor isEqualToString:@"120"]) + annView.pinColor = MKPinAnnotationColorGreen; + else if ([phAnnotation.pinColor isEqualToString:@"270"]) + annView.pinColor = MKPinAnnotationColorPurple; + else + annView.pinColor = MKPinAnnotationColorRed; + + if (phAnnotation.imageURL) + { + AsyncImageView* asyncImage = [[AsyncImageView alloc] initWithFrame:CGRectMake(0,0, 50, 32)]; + asyncImage.tag = 999; + NSURL *url = [[NSURL alloc] initWithString:phAnnotation.imageURL]; + [asyncImage loadImageFromURL:url]; + annView.leftCalloutAccessoryView = asyncImage; + } + + + if (self.buttonCallback && phAnnotation.index!=-1) + { + + UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; + myDetailButton.frame = CGRectMake(0, 0, 23, 23); + myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; + myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; + myDetailButton.tag=phAnnotation.index; + annView.rightCalloutAccessoryView = myDetailButton; + [ myDetailButton addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; + + } + + if(phAnnotation.selected) + { + [self performSelector:@selector(openAnnotation:) withObject:phAnnotation afterDelay:1.0]; + } + + return annView; +} +//when a pin is selected or deselected, do something +- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { + NSString *latitude = [[NSString alloc] initWithFormat:@"%f",view.annotation.coordinate.latitude]; + NSString *longitude = [[NSString alloc] initWithFormat:@"%f",view.annotation.coordinate.longitude]; + + //NSLog(@"Selected: %@%@%@",[view.annotation subtitle], latitude, longitude); + + NSString *annotationTapFunctionString = [NSString stringWithFormat:@"%s%@%s%@%s%@%s", "annotationTap('", [view.annotation subtitle], "','", latitude, "','", longitude, "')"]; + [self.webView stringByEvaluatingJavaScriptFromString:annotationTapFunctionString]; } +- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view { + //NSLog(@"De-Selected: %@",[view.annotation title]); + NSString *annotationDeselectFunctionString = [NSString stringWithFormat:@"%s%@%s", "annotationDeselect('", [view.annotation subtitle], "')"]; + [self.webView stringByEvaluatingJavaScriptFromString:annotationDeselectFunctionString]; +} + +- (void)mapView:(MKMapView *)theMapView regionDidChangeAnimated: (BOOL)animated +{ + NSLog(@"region did change animated"); + float currentLat = theMapView.region.center.latitude; + float currentLon = theMapView.region.center.longitude; + float latitudeDelta = theMapView.region.span.latitudeDelta; + float longitudeDelta = theMapView.region.span.longitudeDelta; + + NSString* jsString = nil; + jsString = [[NSString alloc] initWithFormat:@"geo.onMapMove(\'%f','%f','%f','%f\');", currentLat,currentLon,latitudeDelta,longitudeDelta]; + [self.webView stringByEvaluatingJavaScriptFromString:jsString]; + //[jsString autorelease]; +} +/* end custom addition */ + + -(void)showMap:(CDVInvokedUrlCommand *)command { if (!self.mapView) @@ -213,65 +368,7 @@ - (void)mapView:(MKMapView *)theMapView regionDidChangeAnimated: (BOOL)animated */ -- (MKAnnotationView *) mapView:(MKMapView *)theMapView viewForAnnotation:(id ) annotation { - - if ([annotation class] != CDVAnnotation.class) { - return nil; - } - - CDVAnnotation *phAnnotation=(CDVAnnotation *) annotation; - NSString *identifier=[NSString stringWithFormat:@"INDEX[%i]", phAnnotation.index]; - - MKPinAnnotationView *annView = (MKPinAnnotationView *)[theMapView dequeueReusableAnnotationViewWithIdentifier:identifier]; - - if (annView!=nil) return annView; - annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier]; - - annView.animatesDrop=YES; - annView.canShowCallout = YES; - if ([phAnnotation.pinColor isEqualToString:@"120"]) - annView.pinColor = MKPinAnnotationColorGreen; - else if ([phAnnotation.pinColor isEqualToString:@"270"]) - annView.pinColor = MKPinAnnotationColorPurple; - else - annView.pinColor = MKPinAnnotationColorRed; - - AsyncImageView* asyncImage = [[AsyncImageView alloc] initWithFrame:CGRectMake(0,0, 50, 32)]; - asyncImage.tag = 999; - if (phAnnotation.imageURL) - { - NSURL *url = [[NSURL alloc] initWithString:phAnnotation.imageURL]; - [asyncImage loadImageFromURL:url]; - } - else - { - [asyncImage loadDefaultImage]; - } - - annView.leftCalloutAccessoryView = asyncImage; - - - if (self.buttonCallback && phAnnotation.index!=-1) - { - - UIButton *myDetailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; - myDetailButton.frame = CGRectMake(0, 0, 23, 23); - myDetailButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; - myDetailButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; - myDetailButton.tag=phAnnotation.index; - annView.rightCalloutAccessoryView = myDetailButton; - [ myDetailButton addTarget:self action:@selector(checkButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; - - } - - if(phAnnotation.selected) - { - [self performSelector:@selector(openAnnotation:) withObject:phAnnotation afterDelay:1.0]; - } - - return annView; -} -(void)openAnnotation:(id ) annotation { diff --git a/www/MapKit.js b/www/MapKit.js index a849cd9..f6728b6 100644 --- a/www/MapKit.js +++ b/www/MapKit.js @@ -1,70 +1,78 @@ -var exec = require('cordova/exec'); -var MapKit = function() { - this.mapType = { - MAP_TYPE_NONE: 0, //No base map tiles. - MAP_TYPE_NORMAL: 1, //Basic maps. - MAP_TYPE_SATELLITE: 2, //Satellite maps with no labels. - MAP_TYPE_TERRAIN: 3, //Terrain maps. - MAP_TYPE_HYBRID: 4 //Satellite maps with a transparent layer of major streets. - }; +(function() { - this.iconColors = { - HUE_RED: 0.0, - HUE_ORANGE: 30.0, - HUE_YELLOW: 60.0, - HUE_GREEN: 120.0, - HUE_CYAN: 180.0, - HUE_AZURE: 210.0, - HUE_BLUE: 240.0, - HUE_VIOLET: 270.0, - HUE_MAGENTA: 300.0, - HUE_ROSE: 330.0 - }; -}; + var cordovaRef = window.PhoneGap || window.Cordova || window.cordova; -// Returns default params, overrides if provided with values -function setDefaults(options) { - var defaults = { - height: 460, - diameter: 1000, - atBottom: true, - lat: 49.281468, - lon: -123.104446 - }; + var MapKit = function() { + this.options = { + height: 460, + diameter: 1000, + atBottom: true, + lat: 49.281468, + lon: -123.104446 + }; - if (options) { - for(var i in defaults) - if(typeof options[i] === "undefined") - options[i] = defaults[i]; - } + this.mapType = { + MAP_TYPE_NONE: 0, //No base map tiles. + MAP_TYPE_NORMAL: 1, //Basic maps. + MAP_TYPE_SATELLITE: 2, //Satellite maps with no labels. + MAP_TYPE_TERRAIN: 3, //Terrain maps. + MAP_TYPE_HYBRID: 4 //Satellite maps with a transparent layer of major streets. + }; - return options; -} + this.iconColors = { + HUE_RED: 0.0, + HUE_ORANGE: 30.0, + HUE_YELLOW: 60.0, + HUE_GREEN: 120.0, + HUE_CYAN: 180.0, + HUE_AZURE: 210.0, + HUE_BLUE: 240.0, + HUE_VIOLET: 270.0, + HUE_MAGENTA: 300.0, + HUE_ROSE: 330.0 + }; + }; -MapKit.prototype = { + MapKit.prototype = { - showMap: function(success, error, options) { - options = setDefaults(options); - exec(success, error, 'MapKit', 'showMap', [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) { - exec(success, error, 'MapKit', 'addMapPins', [pins]); - }, + addMapPins: function(success, error, pins) { + cordovaRef.exec(success, error, 'MapKit', 'addMapPins', [pins]); + }, - clearMapPins: function(success, error) { - exec(success, error, 'MapKit', 'clearMapPins', []); - }, + clearMapPins: function(success, error) { + cordovaRef.exec(success, error, 'MapKit', 'clearMapPins', []); + }, - hideMap: function(success, error) { - exec(success, error, 'MapKit', 'hideMap', []); - }, + hideMap: function(success, error) { + cordovaRef.exec(success, error, 'MapKit', 'hideMap', []); + }, - changeMapType: function(mapType, success, error) { - exec(success, error, 'MapKit', 'changeMapType', [mapType ? { "mapType": mapType } :{ "mapType": 0 }]); - } + 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]); + } + } + + }; -}; + cordovaRef.addConstructor(function() { + window.mapKit = new MapKit(); + }); -module.exports = new MapKit(); +})();