MainViewController.m
13.6 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
//
// MainViewController.m
// SampleMap : Diagnostic map
//
#import "MainViewController.h"
#import "MarkerMurderAppDelegate.h"
#import "MainView.h"
#import "RMOpenStreetMapSource.h"
#import "RMOpenSeaMapLayer.h"
#import "RMMapView.h"
#import "RMMarker.h"
#import "RMCircle.h"
#import "RMProjection.h"
#import "RMAnnotation.h"
#import "RMQuadTree.h"
#import "RMCoordinateGridSource.h"
#import "RMOpenCycleMapSource.h"
@implementation MainViewController
{
CLLocationCoordinate2D center;
BOOL tapped;
NSUInteger tapCount;
}
@synthesize mapView;
@synthesize infoTextView;
@synthesize mppLabel, mppImage;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if (!(self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
return nil;
//Notifications for tile requests. This code allows for a class to know when a tile is requested and retrieved
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tileRequested:) name:@"RMTileRequested" object:nil ];
// [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tileRetrieved:) name:@"RMTileRetrieved" object:nil ];
tapped = NO;
tapCount = 0;
return self;
}
- (void)tileRequested:(NSNotification *)notification
{
NSLog(@"Tile request started.");
}
- (void)tileRetrieved:(NSNotification *)notification
{
NSLog(@"Tile request ended.");
}
#define kNumberRows 2
#define kNumberColumns 9
#define kSpacing 0.1
#define kCircleAnnotationType @"circleAnnotation"
#define kDraggableAnnotationType @"draggableAnnotation"
- (void)addMarkers
{
CLLocationCoordinate2D markerPosition;
UIImage *redMarkerImage = [UIImage imageNamed:@"marker-red.png"];
UIImage *blueMarkerImage = [UIImage imageNamed:@"marker-blue.png"];
markerPosition.latitude = center.latitude - ((kNumberRows - 1)/2.0 * kSpacing);
for (int i = 0; i < kNumberRows; i++)
{
markerPosition.longitude = center.longitude - ((kNumberColumns - 1)/2.0 * kSpacing);
for (int j = 0; j < kNumberColumns; j++)
{
markerPosition.longitude += kSpacing;
NSLog(@"Add marker @ {%f,%f} = {%f,%f}", markerPosition.longitude, markerPosition.latitude, [mapView coordinateToProjectedPoint:markerPosition].x, [mapView coordinateToProjectedPoint:markerPosition].y);
RMAnnotation *annotation = [RMAnnotation annotationWithMapView:mapView coordinate:markerPosition andTitle:[NSString stringWithFormat:@"%4.1f", markerPosition.longitude]];
if ((markerPosition.longitude < -180) || (markerPosition.longitude > 0))
{
annotation.annotationIcon = redMarkerImage;
annotation.anchorPoint = CGPointMake(0.5, 1.0);
}
else
{
annotation.annotationIcon = blueMarkerImage;
annotation.anchorPoint = CGPointMake(0.5, 1.0);
}
[mapView addAnnotation:annotation];
}
markerPosition.latitude += kSpacing;
}
RMAnnotation *circleAnnotation = [RMAnnotation annotationWithMapView:mapView coordinate:CLLocationCoordinate2DMake(47.4, 10.0) andTitle:@"A Circle"];
circleAnnotation.annotationType = kCircleAnnotationType;
[mapView addAnnotation:circleAnnotation];
RMAnnotation *draggableAnnotation = [RMAnnotation annotationWithMapView:mapView coordinate:CLLocationCoordinate2DMake(47.72, 10.2) andTitle:@"Drag me! Tap me!"];
draggableAnnotation.annotationType = kDraggableAnnotationType;
draggableAnnotation.annotationIcon = [UIImage imageNamed:@"marker-blue.png"];
draggableAnnotation.anchorPoint = CGPointMake(0.5, 1.0);
draggableAnnotation.clusteringEnabled = NO;
draggableAnnotation.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor blueColor],@"foregroundColor",
nil];
[mapView addAnnotation:draggableAnnotation];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
mapView.delegate = self;
mapView.enableClustering = YES;
mapView.positionClusterMarkersAtTheGravityCenter = YES;
// mapView.adjustTilesForRetinaDisplay = YES;
// mapView.decelerationMode = RMMapDecelerationOff;
// mapView.enableBouncing = NO;
// mapView.enableDragging = YES;
// mapView.debugTiles = YES;
// [mapView setConstraintsSouthWest:CLLocationCoordinate2DMake(47.0, 10.0) northEast:CLLocationCoordinate2DMake(48.0, 11.0)];
// [mapView addTileSource:[[[RMCoordinateGridSource alloc] init] autorelease]];
UIImage *clusterMarkerImage = [UIImage imageNamed:@"marker-blue.png"];
mapView.clusterMarkerSize = clusterMarkerImage.size;
mapView.clusterAreaSize = CGSizeMake(clusterMarkerImage.size.width * 1.25, clusterMarkerImage.size.height * 1.25);
center.latitude = 47.5635;
center.longitude = 10.20981;
// int zoneNumber;
// BOOL isNorthernHemisphere;
// NSString *utmZone;
// double easting, northing;
//
// [RMProjection convertCoordinate:center
// toUTMZoneNumber:&zoneNumber
// utmZoneLetter:&utmZone
// isNorthernHemisphere:&isNorthernHemisphere
// easting:&easting
// northing:&northing];
//
// NSLog(@"{%f,%f} -> %d%@ %.0f %.0f (north: %@)", center.latitude, center.longitude, zoneNumber, utmZone, easting, northing, isNorthernHemisphere ? @"YES" : @"NO");
//
// CLLocationCoordinate2D coordinate;
// [RMProjection convertUTMZoneNumber:zoneNumber
// utmZoneLetter:utmZone
// isNorthernHemisphere:isNorthernHemisphere
// easting:easting
// northing:northing
// toCoordinate:&coordinate];
//
// NSLog(@"-> {%f,%f}", coordinate.latitude, coordinate.longitude);
// [mapView zoomWithLatitudeLongitudeBoundsSouthWest:CLLocationCoordinate2DMake(47.5, 10.15) northEast:CLLocationCoordinate2DMake(47.6, 10.25) animated:NO];
[mapView setZoom:10.0];
[mapView setCenterCoordinate:center animated:NO];
[self updateInfo];
[self performSelector:@selector(addMarkers) withObject:nil afterDelay:0.5];
// // Tile bounding box
// RMSphericalTrapezium tileBoundingBox = [mapView latitudeLongitudeBoundingBoxForTile:RMTileMake(541, 357, 10)];
//
// RMAnnotation *annotation = [RMAnnotation annotationWithMapView:mapView coordinate:tileBoundingBox.southWest andTitle:@"bbox SouthWest"];
// annotation.annotationIcon = [UIImage imageNamed:@"marker-red.png"];
// annotation.anchorPoint = CGPointMake(0.5, 1.0);
// annotation.clusteringEnabled = NO;
// [mapView addAnnotation:annotation];
//
// annotation = [RMAnnotation annotationWithMapView:mapView coordinate:tileBoundingBox.northEast andTitle:@"bbox NorthEast"];
// annotation.annotationIcon = [UIImage imageNamed:@"marker-red.png"];
// annotation.anchorPoint = CGPointMake(0.5, 1.0);
// annotation.clusteringEnabled = NO;
// [mapView addAnnotation:annotation];
// Tile sources
// [mapView setTileSources:@[
// [[[RMOpenStreetMapSource alloc] init] autorelease],
// [[[RMOpenSeaMapLayer alloc] init] autorelease]
// ]];
// double delayInSeconds = 5.0;
// dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
// dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
// [mapView addTileSource:[[[RMCoordinateGridSource alloc] init] autorelease]];
//
// dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
// dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
// [mapView setHidden:YES forTileSourceAtIndex:1];
//
// dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
// dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
// [mapView setHidden:NO forTileSourceAtIndex:1];
//
// dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
// dispatch_after(popTime, dispatch_get_main_queue(), ^(void) {
// [mapView removeTileSourceAtIndex:1];
// });
// });
// });
// });
}
- (void)didReceiveMemoryWarning
{
RMLog(@"didReceiveMemoryWarning %@", self);
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)viewDidAppear:(BOOL)animated
{
[self updateInfo];
}
- (void)viewDidUnload
{
[self setMppLabel:nil];
[super viewDidUnload];
}
- (void)dealloc
{
self.infoTextView = nil;
self.mapView = nil;
self.mppLabel = nil;
[super dealloc];
}
- (void)updateInfo
{
CLLocationCoordinate2D mapCenter = [mapView centerCoordinate];
[infoTextView setText:[NSString stringWithFormat:@"Longitude : %f\nLatitude : %f\nZoom level : %.2f\nScale : 1:%.0f\n%@",
mapCenter.longitude,
mapCenter.latitude,
mapView.zoom,
mapView.scaleDenominator,
[[mapView tileSource] shortAttribution]
]];
[mppLabel setText:[NSString stringWithFormat:@"%.0f m", mapView.metersPerPixel * mppImage.bounds.size.width]];
}
#pragma mark -
#pragma mark Delegate methods
- (void)afterMapMove:(RMMapView *)map
{
[self updateInfo];
}
- (void)afterMapZoom:(RMMapView *)map
{
[self updateInfo];
}
- (BOOL)mapView:(RMMapView *)map shouldDragAnnotation:(RMAnnotation *)annotation
{
if ([annotation.annotationType isEqualToString:kDraggableAnnotationType])
{
NSLog(@"Start dragging marker");
return YES;
}
return NO;
}
- (void)mapView:(RMMapView *)map didDragAnnotation:(RMAnnotation *)annotation withDelta:(CGPoint)delta
{
CGPoint screenPosition = CGPointMake(annotation.position.x - delta.x, annotation.position.y - delta.y);
annotation.coordinate = [mapView pixelToCoordinate:screenPosition];
annotation.position = screenPosition;
}
- (void)mapView:(RMMapView *)map didEndDragAnnotation:(RMAnnotation *)annotation
{
RMProjectedPoint projectedPoint = annotation.projectedLocation;
CGPoint screenPoint = annotation.position;
NSLog(@"Did end dragging marker, screen: {%.0f,%.0f}, projected: {%f,%f}, coordinate: {%f,%f}", screenPoint.x, screenPoint.y, projectedPoint.x, projectedPoint.y, annotation.coordinate.latitude, annotation.coordinate.longitude);
}
- (void)tapOnLabelForAnnotation:(RMAnnotation *)annotation onMap:(RMMapView *)map
{
if ([annotation.annotationType isEqualToString:kDraggableAnnotationType])
{
NSLog(@"Label <%@> tapped for marker <%@>", ((RMMarker *)annotation.layer).label, (RMMarker *)annotation.layer);
[(RMMarker *)annotation.layer changeLabelUsingText:[NSString stringWithFormat:@"Drag me! Tap me! (%d)", ++tapCount]];
}
}
- (void)singleTapOnMap:(RMMapView *)map at:(CGPoint)point
{
RMProjectedPoint projectedPoint = [map pixelToProjectedPoint:point];
CLLocationCoordinate2D coordinates = [map pixelToCoordinate:point];
NSLog(@"Clicked on Map - Location: x:%lf y:%lf, Projected east:%f north:%f, Coordinate lat:%f lon:%f", point.x, point.y, projectedPoint.x, projectedPoint.y, coordinates.latitude, coordinates.longitude);
}
- (void)tapOnAnnotation:(RMAnnotation *)annotation onMap:(RMMapView *)map
{
if ([annotation.annotationType isEqualToString:kRMClusterAnnotationTypeName])
{
[map zoomInToNextNativeZoomAt:[map coordinateToPixel:annotation.coordinate] animated:YES];
}
else if ([annotation.annotationType isEqualToString:kDraggableAnnotationType])
{
NSLog(@"MARKER TAPPED!");
if (!tapped)
{
annotation.annotationIcon = [UIImage imageNamed:@"marker-red.png"];
[(RMMarker *)annotation.layer replaceUIImage:annotation.annotationIcon anchorPoint:annotation.anchorPoint];
[(RMMarker *)annotation.layer changeLabelUsingText:@"Hello"];
tapped = YES;
}
else
{
annotation.annotationIcon = [UIImage imageNamed:@"marker-blue.png"];
[(RMMarker *)annotation.layer replaceUIImage:annotation.annotationIcon anchorPoint:annotation.anchorPoint];
[(RMMarker *)annotation.layer changeLabelUsingText:@"World"];
tapped = NO;
}
}
}
- (RMMapLayer *)mapView:(RMMapView *)aMapView layerForAnnotation:(RMAnnotation *)annotation
{
RMMapLayer *marker = nil;
if ([annotation.annotationType isEqualToString:kRMClusterAnnotationTypeName])
{
marker = [[[RMMarker alloc] initWithUIImage:[UIImage imageNamed:@"marker-blue.png"] anchorPoint:CGPointMake(0.5, 1.0)] autorelease];
if (annotation.title)
[(RMMarker *)marker changeLabelUsingText:annotation.title];
}
else if ([annotation.annotationType isEqualToString:kCircleAnnotationType])
{
marker = [[[RMCircle alloc] initWithView:aMapView radiusInMeters:10000.0] autorelease];
[(RMCircle *)marker setLineWidthInPixels:5.0];
}
else
{
marker = [[[RMMarker alloc] initWithUIImage:annotation.annotationIcon anchorPoint:annotation.anchorPoint] autorelease];
if (annotation.title)
[(RMMarker *)marker changeLabelUsingText:annotation.title];
if ([annotation.userInfo objectForKey:@"foregroundColor"])
[(RMMarker *)marker setTextForegroundColor:[annotation.userInfo objectForKey:@"foregroundColor"]];
if ([annotation.annotationType isEqualToString:kDraggableAnnotationType])
marker.enableDragging = YES;
}
return marker;
}
@end