RMAttributionViewController.m
3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//
// RMAttributionViewController.m
// MapView
//
// Created by Justin Miller on 6/19/12.
// Copyright (c) 2012 MapBox / Development Seed. All rights reserved.
//
#import "RMAttributionViewController.h"
#import "RMMapView.h"
#import "RMTileSource.h"
@interface RMMapView (RMAttributionViewControllerPrivate)
@property (nonatomic, assign) UIViewController *viewControllerPresentingAttribution;
@end
#pragma mark -
@implementation RMAttributionViewController
{
RMMapView *_mapView;
}
- (id)initWithMapView:(RMMapView *)mapView
{
self = [super initWithNibName:nil bundle:nil];
if (self)
{
_mapView = mapView;
self.view.backgroundColor = [UIColor darkGrayColor];
[self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissModalViewControllerAnimated:)]];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - 70, self.view.bounds.size.width, 60)];
webView.delegate = self;
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
NSMutableString *attribution = [NSMutableString string];
for (id <RMTileSource>tileSource in mapView.tileSources)
{
if ([tileSource respondsToSelector:@selector(shortAttribution)])
{
if ([attribution length])
[attribution appendString:@" "];
if ([tileSource shortAttribution])
[attribution appendString:[tileSource shortAttribution]];
}
}
if ( ! [attribution length])
[attribution setString:@"Map data © OpenStreetMap contributors <a href=\"http://mapbox.com/about/maps/\">(Details)</a>"];
NSMutableString *contentString = [NSMutableString string];
[contentString appendString:@"<style type='text/css'>"];
[contentString appendString:@"a:link { color: white; text-decoration: none; }"];
[contentString appendString:@"body { color: lightgray; font-family: Helvetica, Arial, Verdana, sans-serif; text-align: center; }"];
[contentString appendString:@"</style>"];
[contentString appendString:attribution];
[webView loadHTMLString:contentString baseURL:nil];
[self.view addSubview:webView];
}
return self;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [_mapView.viewControllerPresentingAttribution shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#pragma mark -
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
[[UIApplication sharedApplication] openURL:request.URL];
[self performSelector:@selector(dismissModalViewControllerAnimated:) withObject:[NSNumber numberWithBool:YES] afterDelay:0];
}
return [[request.URL scheme] isEqualToString:@"about"];
}
@end