Advertisement
Guest User

Untitled

a guest
Jan 28th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. //
  2. // MapViewController.m
  3. // AppGyver
  4. //
  5. // Created by Rafael on 6/03/14.
  6. // Copyright (c) 2014 AppGyver Inc. All rights reserved.
  7. //
  8.  
  9. #import "MapViewController.h"
  10.  
  11. @implementation MapMarkerOptions
  12.  
  13. @synthesize title, subtitle, center;
  14.  
  15. +(NSArray *)markerOptionsFromList:(NSArray *)list {
  16. NSMutableArray *result = [NSMutableArray new];
  17.  
  18. for(NSDictionary *dict in list){
  19.  
  20. id latitude = [dict valueForKeyPath:@"latitude"] ;
  21. id longitude = [dict valueForKeyPath:@"longitude"] ;
  22.  
  23. if(latitude && longitude){
  24. //all these values are required to create a region
  25.  
  26. MapMarkerOptions *mapMarkerOptions = [MapMarkerOptions new];
  27.  
  28. //center
  29. mapMarkerOptions.center = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
  30.  
  31. mapMarkerOptions.title = [dict valueForKeyPath:@"title"];
  32. mapMarkerOptions.subtitle = [dict valueForKeyPath:@"subtitle"];
  33.  
  34. [result addObject:mapMarkerOptions];
  35. }
  36. }
  37. return result;
  38. }
  39.  
  40. @end
  41.  
  42. @implementation MapOptions
  43.  
  44. @synthesize mapType,
  45. region,
  46. regionSet,
  47. centerSet,
  48. center,
  49. showsUserLocation;
  50.  
  51. +(MapOptions *)optionsFromDictionary:(NSDictionary *)dict {
  52.  
  53. MapOptions *options = [MapOptions new];
  54.  
  55. //if there is not map options return an empty mapOptions
  56. if(!dict){
  57. return options;
  58. }
  59.  
  60. //MapType
  61. NSString *mapTypeValue = [[NSString stringWithString:[dict valueForKeyPath:@"mapType"]] lowercaseString];
  62.  
  63. if([@"satellite" isEqualToString:mapTypeValue]){
  64. options.mapType = MKMapTypeSatellite;
  65. }
  66. else if([@"hybrid" isEqualToString:mapTypeValue]){
  67. options.mapType = MKMapTypeHybrid;
  68. }
  69. else {
  70. //default value
  71. options.mapType = MKMapTypeStandard;
  72. }
  73.  
  74. //parse the region parameter
  75.  
  76. //center
  77. id latitude = [dict valueForKeyPath:@"region.center.latitude"] ;
  78. id longitude = [dict valueForKeyPath:@"region.center.longitude"] ;
  79.  
  80. //span
  81. id latitudinalMeters = [dict valueForKeyPath:@"region.span.latitudinalMeters"] ;
  82. id longitudinalMeters = [dict valueForKeyPath:@"region.span.longitudinalMeters"] ;
  83.  
  84. if(latitude && longitude){
  85. //all these values are required to create a region
  86.  
  87. //center
  88. options.center = CLLocationCoordinate2DMake([latitude doubleValue], [longitude doubleValue]);
  89. options.centerSet = YES;
  90. }
  91. if(latitudinalMeters && longitudinalMeters){
  92. //region
  93. options.region = MKCoordinateRegionMakeWithDistance(options.center, [latitudinalMeters doubleValue], [longitudinalMeters doubleValue]);
  94. options.regionSet = YES;
  95. }
  96.  
  97. // Set to YES to add the user location annotation to the map and start updating its location
  98. //showsUserLocation
  99. id showsUserLocation = [dict valueForKeyPath:@"showsUserLocation"] ;
  100. if(showsUserLocation){
  101. options.showsUserLocation = [showsUserLocation boolValue];
  102. }
  103.  
  104. return options;
  105. }
  106.  
  107. -(void)copyOptions:(MapOptions*) options{
  108. if(options.mapType){
  109. self.mapType = options.mapType;
  110. }
  111. if(options.regionSet){
  112. self.region = options.region;
  113. }
  114. if(options.centerSet){
  115. self.center = options.center;
  116. }
  117. self.showsUserLocation = options.showsUserLocation;
  118. }
  119.  
  120. @end
  121.  
  122. @interface MapViewController ()
  123.  
  124. //holds the markers that will be displayed on the map
  125. @property (nonatomic, strong) NSMutableArray *markersToBeDisplayed;
  126.  
  127. @end
  128.  
  129. @implementation MapViewController
  130.  
  131. @synthesize mapView, viewDisplayed, latestOptionsApplied, mapOptions = _mapOptions, markersToBeDisplayed;
  132.  
  133. +(MapViewController*)controllerWithURL:(NSURL*)url mapOptions:(MapOptions*)options{
  134. MapViewController *controller = [[ MapViewController alloc ] initWithNibName:NULL bundle:NULL ];
  135. controller.startURL = url;
  136.  
  137. controller.mapOptions = options;
  138.  
  139. return controller;
  140. }
  141.  
  142. - (void)viewDidLoad
  143. {
  144. [super viewDidLoad];
  145.  
  146. if(!self.mapView){
  147. // create the map view
  148. self.mapView = [[MKMapView alloc] initWithFrame:self.view.frame];
  149.  
  150. //configure the transparency of the webview
  151. //and disable the interaction.. so all events are handled in the mapview
  152. self.webView.opaque = NO;
  153. self.webView.backgroundColor = [UIColor clearColor];
  154. self.webView.userInteractionEnabled = NO;
  155.  
  156. //add as a subview
  157. [self.view insertSubview:self.mapView belowSubview:self.webView];
  158. [self.view setupAutoRotateSubViewConstraints:self.mapView];
  159. }
  160.  
  161. [self applyMapOptions];
  162.  
  163. [self applyMarkers];
  164.  
  165. self.viewDisplayed = YES;
  166.  
  167. }
  168.  
  169. -(void) applyMapOptions {
  170.  
  171. if(self.latestOptionsApplied){
  172. return;
  173. }
  174.  
  175. self.mapView.mapType = self.mapOptions.mapType;
  176.  
  177. if(self.mapOptions.regionSet){
  178. self.mapView.region = self.mapOptions.region;
  179. }
  180.  
  181. if(self.mapOptions.centerSet){
  182. [self.mapView setCenterCoordinate:self.mapOptions.center];
  183. }
  184.  
  185. self.mapView.showsUserLocation = self.mapOptions.showsUserLocation;
  186.  
  187. self.latestOptionsApplied = YES;
  188. }
  189.  
  190. -(void) addMarkers:(NSArray *)markers{
  191.  
  192. if(!markersToBeDisplayed){
  193. markersToBeDisplayed = [NSMutableArray new];
  194. }
  195.  
  196. [markersToBeDisplayed addObjectsFromArray:markers];
  197.  
  198. if(viewDisplayed){
  199. [self applyMarkers];
  200. }
  201. }
  202.  
  203. -(void) applyMarkers {
  204.  
  205. if(!markersToBeDisplayed){
  206. return;
  207. }
  208.  
  209. for(MapMarkerOptions *markerOptions in markersToBeDisplayed){
  210. MKPointAnnotation *marker = [MKPointAnnotation new];
  211.  
  212. marker.coordinate = markerOptions.center;
  213. marker.title = markerOptions.title;
  214. marker.subtitle = markerOptions.subtitle;
  215.  
  216. [self.mapView addAnnotation:marker];
  217. }
  218.  
  219. [markersToBeDisplayed removeAllObjects];
  220. markersToBeDisplayed = nil;
  221. }
  222.  
  223. -(void) setMapOptions:(MapOptions *)options{
  224.  
  225. if(_mapOptions){
  226. [_mapOptions copyOptions:options];
  227. }
  228. else{
  229. _mapOptions = options;
  230. }
  231.  
  232. self.latestOptionsApplied = NO;
  233.  
  234. if(viewDisplayed){
  235. [self applyMapOptions];
  236. }
  237. }
  238.  
  239. -(void)die {
  240. //TODO THIS IZ HOERRILBE
  241.  
  242. if ([self viewDestroyed])
  243. return;
  244.  
  245. [self setViewDestroyed:YES];
  246.  
  247. for (UIView *view in [self.view subviews]) {
  248. [view removeFromSuperview];
  249. }
  250.  
  251. for (CDVPlugin *plugin in [self.pluginObjects allValues]) {
  252. [plugin onReset];
  253. [plugin dispose];
  254. }
  255. [self.pluginObjects removeAllObjects];
  256.  
  257. ApplicationViewController *root = (ApplicationViewController*)[[ApplicationManager instance] currentRootViewController];
  258. if (root) {
  259. [[root allWebViewControllers] removeObjectForKey:[self uniqueIdentifier]];
  260. }
  261. }
  262.  
  263. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement