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

/samples/SimpleSampleMap/Classes/MapViewViewController.m

http://github.com/route-me/route-me
Objective C | 264 lines | 173 code | 52 blank | 39 comment | 11 complexity | 07d3e11c89b3a1d4eae8cdcb7e0a4bc5 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@synthesize locationManager;
 39@synthesize currentLocation;
 40
 41// Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad.
 42- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 43{
 44    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) 
 45	{
 46        // Custom initialization
 47    }
 48	
 49    return self;
 50}
 51
 52- (void)testMarkers
 53{
 54	RMMarkerManager *markerManager	= [mapView markerManager];
 55	NSArray			*markers		= [markerManager markers];
 56	
 57	NSLog(@"Nb markers %d", [markers count]);
 58	
 59	NSEnumerator *markerEnumerator = [markers objectEnumerator];
 60	RMMarker *aMarker;
 61	
 62	while (aMarker = (RMMarker *)[markerEnumerator nextObject])
 63	{
 64//		RMXYPoint point						= [aMarker location];
 65//		CGPoint screenPoint					= [markerManager getMarkerScreenCoordinate: aMarker];
 66//		CLLocationCoordinate2D coordinates	=  [markerManager getMarkerCoordinate2D: aMarker];
 67		
 68		[markerManager removeMarker:aMarker];
 69	}
 70	
 71	// Put the marker back
 72	RMMarker *marker = 
 73	[[RMMarker alloc] initWithUIImage: [UIImage imageNamed:@"marker-blue.png"]];
 74	[marker changeLabelUsingText:@"Hello"];
 75	
 76	[markerManager addMarker:marker 
 77				   AtLatLong:[[mapView contents] mapCenter]];
 78	
 79	[marker release];
 80	markers  = [markerManager markersWithinScreenBounds];
 81	
 82	NSLog(@"Nb Markers in Screen: %d", [markers count]);
 83	
 84	//	[mapView getScreenCoordinateBounds];
 85	
 86	[markerManager hideAllMarkers];
 87	[markerManager unhideAllMarkers];
 88	
 89
 90}
 91
 92- (BOOL)mapView:(RMMapView *)map 
 93shouldDragMarker:(RMMarker *)marker
 94	  withEvent:(UIEvent *)event
 95{
 96   //If you do not implement this function, then all drags on markers will be sent to the didDragMarker function.
 97   //If you always return YES you will get the same result
 98   //If you always return NO you will never get a call to the didDragMarker function
 99   return YES;
100}
101
102- (void)mapView:(RMMapView *)map
103  didDragMarker:(RMMarker *)marker 
104	  withEvent:(UIEvent *)event 
105{
106   CGPoint position = [[[event allTouches] anyObject] locationInView:mapView];
107   
108	RMMarkerManager *markerManager = [mapView markerManager];
109
110	NSLog(@"New location: east:%lf north:%lf", [marker projectedLocation].easting, [marker projectedLocation].northing);
111	CGRect rect = [marker bounds];
112	
113	[markerManager moveMarker:marker 
114						 AtXY:CGPointMake(position.x,position.y +rect.size.height/3)];
115
116}
117
118- (void) singleTapOnMap: (RMMapView*) map 
119					 At: (CGPoint) point
120{
121	NSLog(@"Clicked on Map - New location: X:%lf Y:%lf", point.x, point.y);
122}
123
124- (void) tapOnMarker: (RMMarker*) marker 
125			   onMap: (RMMapView*) map
126{
127	NSLog(@"MARKER TAPPED!");
128	RMMarkerManager *markerManager = [mapView markerManager];
129	if (!tap)
130	{
131		[marker replaceUIImage:[UIImage imageNamed:@"marker-red.png"]
132				 anchorPoint:CGPointMake(0.5, 1.0)];
133		[marker changeLabelUsingText:@"World"];
134		tap=YES;
135		[markerManager moveMarker:marker 
136							 AtXY:CGPointMake([marker position].x,[marker position].y + 20.0)];
137		[mapView setDeceleration:YES];
138	}
139	else
140	{
141		[marker replaceUIImage:[UIImage imageNamed:@"marker-blue.png"]
142				   anchorPoint:CGPointMake(0.5, 1.0)];
143		[marker changeLabelUsingText:@"Hello"];
144		[markerManager moveMarker:marker 
145							 AtXY:CGPointMake([marker position].x, [marker position].y - 20.0)];
146		tap = NO;
147		[mapView setDeceleration:NO];
148	}
149
150}
151
152- (void) tapOnLabelForMarker:(RMMarker*) marker 
153					   onMap:(RMMapView*) map
154{
155	NSLog(@"Label <0x%x, RC:%U> tapped for marker <0x%x, RC:%U>",  marker.label, [marker.label retainCount], marker, [marker retainCount]);
156	[marker changeLabelUsingText:[NSString stringWithFormat:@"Tapped! (%U)", ++tapCount]];
157}
158
159// Implement viewDidLoad to do additional setup after loading the view.
160- (void)viewDidLoad 
161{
162    [super viewDidLoad];
163
164	locationManager	= [[CLLocationManager alloc] init];
165	locationManager.delegate		= self;
166	locationManager.desiredAccuracy = kCLLocationAccuracyBest;
167	if (locationManager.locationServicesEnabled == NO)
168	{
169		NSLog(@"Services not enabled");
170		return;
171	}
172	
173	currentLocation. latitude = 33.413313;
174	currentLocation.longitude = -111.907326;
175	
176	[locationManager startUpdatingLocation];
177
178	tap = NO;
179	RMMarkerManager *markerManager = [mapView markerManager];
180
181	[mapView setDelegate:self];
182	[mapView setBackgroundColor:[UIColor grayColor]];  //or clear etc 
183
184	if (locationManager.location != nil)
185	{
186		currentLocation = locationManager.location.coordinate;
187		
188		NSLog(@"Location: Lat: %lf Lon: %lf", currentLocation.latitude, currentLocation.longitude);
189	}
190	
191	[mapView moveToLatLong:currentLocation]; 
192	[self.view addSubview:mapView]; 
193
194	RMMarker *marker = [[RMMarker alloc] initWithUIImage: [UIImage imageNamed:@"marker-blue.png"]];
195	[marker setTextForegroundColor:[UIColor blueColor]];
196	[marker changeLabelUsingText:@"Hello"];
197	[markerManager addMarker:marker 
198				   AtLatLong:currentLocation];
199	[marker release];
200}
201
202- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
203{
204    return YES;
205}
206
207
208- (void)didReceiveMemoryWarning 
209{
210	// due to a bug, RMMapView should never be released, as it causes the application to crash
211    //[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
212
213	[mapView.contents didReceiveMemoryWarning];
214}
215
216
217- (void)dealloc 
218{
219	[mapView release];
220	[locationManager stopUpdatingLocation];
221	[locationManager release];
222	
223    [super dealloc];
224}
225
226#pragma mark --
227#pragma mark locationManagerDelegate Methods
228
229- (void)locationManager: (CLLocationManager *)manager 
230	didUpdateToLocation: (CLLocation *)newLocation
231		   fromLocation: (CLLocation *)oldLocation
232{
233	NSLog(@"Moving from lat: %lf lon: %lf to lat: %lf lon: %lf", 
234		  oldLocation.coordinate.latitude, oldLocation.coordinate.longitude,
235		  newLocation.coordinate.latitude, newLocation.coordinate.longitude);
236	
237	currentLocation = newLocation.coordinate;
238	RMMarkerManager *markerManager = [mapView markerManager];
239	NSArray *markers = [markerManager markers];
240	for (NSInteger i = 0; i < [markers count]; ++i)
241	{
242		RMMarker *marker = [markers objectAtIndex: i];
243		CLLocationCoordinate2D location = [markerManager latitudeLongitudeForMarker:marker];
244		if (location.latitude == oldLocation.coordinate.latitude &&
245			location.longitude == oldLocation.coordinate.longitude)
246		{
247			[markerManager moveMarker: marker
248							AtLatLon: newLocation.coordinate];
249			break; // We're done. 
250		}
251	}
252	
253	[mapView moveToLatLong:currentLocation]; 
254}
255
256- (void)locationManager: (CLLocationManager *)manager
257	   didFailWithError: (NSError *)error
258{
259	NSLog(@"Location Manager error: %@", [error localizedDescription]);
260}
261
262@synthesize tap;
263@synthesize tapCount;
264@end