PageRenderTime 46ms CodeModel.GetById 13ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/samples/SimpleMap/Classes/MapViewViewController.m

http://github.com/route-me/route-me
Objective C | 186 lines | 104 code | 36 blank | 46 comment | 2 complexity | 03ff4de3e3db3c459b6c86c2dc36d8bc MD5 | raw file
  1//
  2//  MapViewViewController.m
  3//
  4// Copyright (c) 2008, Route-Me Contributors
  5// All rights reserved.
  6//
  7// Redistribution and use in source and binary forms, with or without
  8// modification, are permitted provided that the following conditions are met:
  9//
 10// * Redistributions of source code must retain the above copyright notice, this
 11//   list of conditions and the following disclaimer.
 12// * Redistributions in binary form must reproduce the above copyright notice,
 13//   this list of conditions and the following disclaimer in the documentation
 14//   and/or other materials provided with the distribution.
 15//
 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 19// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 20// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 22// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 23// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 24// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 25// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 26// POSSIBILITY OF SUCH DAMAGE.
 27
 28#import "MapViewViewController.h"
 29
 30#import "RMMapContents.h"
 31#import "RMFoundation.h"
 32#import "RMMarker.h"
 33#import "RMMarkerManager.h"
 34
 35@implementation MapViewViewController
 36
 37@synthesize mapView;
 38/*
 39// Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad.
 40- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 41    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 42        // Custom initialization
 43    }
 44    return self;
 45}
 46*/
 47
 48/*
 49// Implement loadView to create a view hierarchy programmatically.
 50- (void)loadView {
 51}
 52*/
 53
 54- (void)testMarkers
 55{
 56	RMMarkerManager *markerManager = [mapView markerManager];
 57	NSArray *markers = [markerManager markers];
 58	
 59	NSLog(@"Nb markers %d", [markers count]);
 60	
 61	NSEnumerator *markerEnumerator = [markers objectEnumerator];
 62	RMMarker *aMarker;
 63	
 64	while (aMarker = (RMMarker *)[markerEnumerator nextObject])
 65		
 66	{
 67		RMProjectedPoint point = [aMarker projectedLocation];
 68		NSLog(@"Marker projected location: east:%lf, north:%lf", point.easting, point.northing);
 69		CGPoint screenPoint = [markerManager screenCoordinatesForMarker: aMarker];
 70		NSLog(@"Marker screen location: X:%lf, Y:%lf", screenPoint.x, screenPoint.y);
 71		CLLocationCoordinate2D coordinates =  [markerManager latitudeLongitudeForMarker: aMarker];
 72		NSLog(@"Marker Lat/Lon location: Lat:%lf, Lon:%lf", coordinates.latitude, coordinates.longitude);
 73		
 74		[markerManager removeMarker:aMarker];
 75	}
 76	
 77	// Put the marker back
 78	RMMarker *marker = [[RMMarker alloc]initWithUIImage:[UIImage imageNamed:@"marker-blue.png"]
 79											anchorPoint:CGPointMake(0.5, 1.0)];
 80	[marker changeLabelUsingText:@"Hello"];
 81	
 82	[markerManager addMarker:marker AtLatLong:[[mapView contents] mapCenter]];
 83	
 84	[marker release];
 85	markers  = [markerManager markersWithinScreenBounds];
 86	
 87	NSLog(@"Nb Markers in Screen: %d", [markers count]);
 88	
 89	//	[mapView getScreenCoordinateBounds];
 90	
 91	[markerManager hideAllMarkers];
 92	[markerManager unhideAllMarkers];
 93	
 94
 95}
 96
 97- (BOOL)mapView:(RMMapView *)map shouldDragMarker:(RMMarker *)marker withEvent:(UIEvent *)event
 98{
 99   //If you do not implement this function, then all drags on markers will be sent to the didDragMarker function.
100   //If you always return YES you will get the same result
101   //If you always return NO you will never get a call to the didDragMarker function
102   return YES;
103}
104
105- (void)mapView:(RMMapView *)map didDragMarker:(RMMarker *)marker withEvent:(UIEvent *)event 
106{
107   CGPoint position = [[[event allTouches] anyObject] locationInView:mapView];
108   
109	RMMarkerManager *markerManager = [mapView markerManager];
110
111	NSLog(@"New location: east:%lf north:%lf", [marker projectedLocation].easting, [marker projectedLocation].northing);
112	CGRect rect = [marker bounds];
113	
114	[markerManager moveMarker:marker AtXY:CGPointMake(position.x,position.y +rect.size.height/3)];
115
116}
117
118- (void) singleTapOnMap: (RMMapView*) map At: (CGPoint) point
119{
120	NSLog(@"Clicked on Map - New location: X:%lf Y:%lf", point.x, point.y);
121}
122
123- (void) tapOnMarker: (RMMarker*) marker onMap: (RMMapView*) map
124{
125	NSLog(@"MARKER TAPPED!");
126	RMMarkerManager *markerManager = [mapView markerManager];
127	if(!tap)
128	{
129		[marker replaceUIImage:[UIImage imageNamed:@"marker-red.png"]];
130		[marker changeLabelUsingText:@"World"];
131		tap=YES;
132		[markerManager moveMarker:marker AtXY:CGPointMake([marker position].x,[marker position].y + 20.0)];
133		[mapView setDeceleration:YES];
134	}else
135	{
136			[marker replaceUIImage:[UIImage imageNamed:@"marker-blue.png"]
137					   anchorPoint:CGPointMake(0.5, 1.0)];
138		[marker changeLabelUsingText:@"Hello"];
139		[markerManager moveMarker:marker AtXY:CGPointMake([marker position].x,[marker position].y - 20.0)];
140		tap=NO;
141		[mapView setDeceleration:NO];
142	}
143
144}
145
146- (void) tapOnLabelForMarker:(RMMarker*) marker onMap:(RMMapView*) map
147{
148	NSLog(@"Label <0x%x, RC:%U> tapped for marker <0x%x, RC:%U>",  marker.label, [marker.label retainCount], marker, [marker retainCount]);
149	[marker changeLabelUsingText:[NSString stringWithFormat:@"Tapped! (%U)", ++tapCount]];
150}
151
152// Implement viewDidLoad to do additional setup after loading the view.
153- (void)viewDidLoad {
154	NSLog(@"%@ viewDidLoad", self);
155    [super viewDidLoad];
156	tap=NO;
157	RMMarkerManager *markerManager = [mapView markerManager];
158	[mapView setDelegate:self];
159	
160	CLLocationCoordinate2D coolPlace;
161	coolPlace.latitude = -33.9464;
162	coolPlace.longitude = 151.2381;
163	
164	RMMarker *marker = [[RMMarker alloc]initWithUIImage:[UIImage imageNamed:@"marker-blue.png"]
165											anchorPoint:CGPointMake(0.5, 1.0)];
166	[marker setTextForegroundColor:[UIColor blueColor]];
167	[marker changeLabelUsingText:@"Hello"];
168	[markerManager addMarker:marker AtLatLong:[[mapView contents] mapCenter]];
169	[marker release];
170	NSLog(@"Center: Lat: %lf Lon: %lf", mapView.contents.mapCenter.latitude, mapView.contents.mapCenter.longitude);
171}
172
173- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
174    return YES;
175}
176
177- (void)didReceiveMemoryWarning {
178    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
179}
180
181- (void)dealloc {
182	[mapView release];
183    [super dealloc];
184}
185
186@end