Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MapViewController.m
- // AppGyver
- //
- // Created by Rafael on 6/03/14.
- // Copyright (c) 2014 AppGyver Inc. All rights reserved.
- //
- #import "MapViewController.h"
- @implementation MapMarkerOptions
- @synthesize title, subtitle, center;
- +(NSArray *)markerOptionsFromList:(NSArray *)list {
- NSMutableArray *result = [NSMutableArray new];
- for(NSDictionary *dict in list){
- id latitude = [dict valueForKeyPath:@"latitude"] ;
- id longitude = [dict valueForKeyPath:@"longitude"] ;
- if(latitude && longitude){
- //all these values are required to create a region
- MapMarkerOptions *mapMarkerOptions = [MapMarkerOptions new];
- //center
- mapMarkerOptions.center = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
- mapMarkerOptions.title = [dict valueForKeyPath:@"title"];
- mapMarkerOptions.subtitle = [dict valueForKeyPath:@"subtitle"];
- [result addObject:mapMarkerOptions];
- }
- }
- return result;
- }
- @end
- @implementation MapOptions
- @synthesize mapType,
- region,
- regionSet,
- centerSet,
- center,
- showsUserLocation;
- +(MapOptions *)optionsFromDictionary:(NSDictionary *)dict {
- MapOptions *options = [MapOptions new];
- //if there is not map options return an empty mapOptions
- if(!dict){
- return options;
- }
- //MapType
- NSString *mapTypeValue = [[NSString stringWithString:[dict valueForKeyPath:@"mapType"]] lowercaseString];
- if([@"satellite" isEqualToString:mapTypeValue]){
- options.mapType = MKMapTypeSatellite;
- }
- else if([@"hybrid" isEqualToString:mapTypeValue]){
- options.mapType = MKMapTypeHybrid;
- }
- else {
- //default value
- options.mapType = MKMapTypeStandard;
- }
- //parse the region parameter
- //center
- id latitude = [dict valueForKeyPath:@"region.center.latitude"] ;
- id longitude = [dict valueForKeyPath:@"region.center.longitude"] ;
- //span
- id latitudinalMeters = [dict valueForKeyPath:@"region.span.latitudinalMeters"] ;
- id longitudinalMeters = [dict valueForKeyPath:@"region.span.longitudinalMeters"] ;
- if(latitude && longitude){
- //all these values are required to create a region
- //center
- options.center = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
- options.centerSet = YES;
- }
- if(latitudinalMeters && longitudinalMeters){
- //region
- options.region = MKCoordinateRegionMakeWithDistance(options.center, [latitudinalMeters doubleValue], [longitudinalMeters doubleValue]);
- options.regionSet = YES;
- }
- // Set to YES to add the user location annotation to the map and start updating its location
- //showsUserLocation
- id showsUserLocation = [dict valueForKeyPath:@"showsUserLocation"] ;
- if(showsUserLocation){
- options.showsUserLocation = [showsUserLocation boolValue];
- }
- return options;
- }
- -(void)copyOptions:(MapOptions*) options{
- if(options.mapType){
- self.mapType = options.mapType;
- }
- if(options.regionSet){
- self.region = options.region;
- }
- if(options.centerSet){
- self.center = options.center;
- }
- self.showsUserLocation = options.showsUserLocation;
- }
- @end
- @interface MapViewController ()
- //holds the markers that will be displayed on the map
- @property (nonatomic, strong) NSMutableArray *markersToBeDisplayed;
- @end
- @implementation MapViewController
- @synthesize mapView, viewDisplayed, latestOptionsApplied, mapOptions = _mapOptions, markersToBeDisplayed;
- +(MapViewController*)controllerWithURL:(NSURL*)url mapOptions:(MapOptions*)options{
- MapViewController *controller = [[ MapViewController alloc ] initWithNibName:NULL bundle:NULL ];
- controller.startURL = url;
- controller.mapOptions = options;
- return controller;
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- if(!self.mapView){
- // create the map view
- self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
- //configure the transparency of the webview
- //and disable the interaction.. so all events are handled in the mapview
- self.webView.opaque = NO;
- self.webView.backgroundColor = [UIColor clearColor];
- self.webView.userInteractionEnabled = NO;
- //add as a subview
- [self.view insertSubview:self.mapView belowSubview:self.webView];
- [self.view setupAutoRotateSubViewConstraints:self.mapView];
- }
- [self applyMapOptions];
- [self applyMarkers];
- self.viewDisplayed = YES;
- }
- -(void) applyMapOptions {
- if(self.latestOptionsApplied){
- return;
- }
- self.mapView.mapType = self.mapOptions.mapType;
- if(self.mapOptions.regionSet){
- self.mapView.region = self.mapOptions.region;
- }
- if(self.mapOptions.centerSet){
- [self.mapView setCenterCoordinate:self.mapOptions.center];
- }
- self.mapView.showsUserLocation = self.mapOptions.showsUserLocation;
- self.latestOptionsApplied = YES;
- }
- -(void) addMarkers:(NSArray *)markers{
- if(!markersToBeDisplayed){
- markersToBeDisplayed = [NSMutableArray new];
- }
- [markersToBeDisplayed addObjectsFromArray:markers];
- if(viewDisplayed){
- [self applyMarkers];
- }
- }
- -(void) applyMarkers {
- if(!markersToBeDisplayed){
- return;
- }
- for(MapMarkerOptions *markerOptions in markersToBeDisplayed){
- MKPointAnnotation *marker = [MKPointAnnotation new];
- marker.coordinate = markerOptions.center;
- marker.title = markerOptions.title;
- marker.subtitle = markerOptions.subtitle;
- [self.mapView addAnnotation:marker];
- }
- [markersToBeDisplayed removeAllObjects];
- markersToBeDisplayed = nil;
- }
- -(void) setMapOptions:(MapOptions *)options{
- if(_mapOptions){
- [_mapOptions copyOptions:options];
- }
- else{
- _mapOptions = options;
- }
- self.latestOptionsApplied = NO;
- if(viewDisplayed){
- [self applyMapOptions];
- }
- }
- -(void)die {
- //TODO THIS IZ HOERRILBE
- if ([self viewDestroyed])
- return;
- [self setViewDestroyed:YES];
- for (UIView *view in [self.view subviews]) {
- [view removeFromSuperview];
- }
- for (CDVPlugin *plugin in [self.pluginObjects allValues]) {
- [plugin onReset];
- [plugin dispose];
- }
- [self.pluginObjects removeAllObjects];
- ApplicationViewController *root = (ApplicationViewController*)[[ApplicationManager instance] currentRootViewController];
- if (root) {
- [[root allWebViewControllers] removeObjectForKey:[self uniqueIdentifier]];
- }
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement