From 639d74eb0b8e6ed78368019c2ec34fa16c525c59 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Wed, 2 May 2012 12:38:05 -0400 Subject: [PATCH 1/2] first cut using Apple's geocoder --- MapBox Example.xcodeproj/project.pbxproj | 4 + MapBox Example/OnlineLayerViewController.h | 2 +- MapBox Example/OnlineLayerViewController.m | 102 +++++++++++++++++++-- 3 files changed, 100 insertions(+), 8 deletions(-) diff --git a/MapBox Example.xcodeproj/project.pbxproj b/MapBox Example.xcodeproj/project.pbxproj index 00fb163..cf21f16 100644 --- a/MapBox Example.xcodeproj/project.pbxproj +++ b/MapBox Example.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + DD227A011551969500E73845 /* CoreLocation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD227A001551969500E73845 /* CoreLocation.framework */; }; DD76D72E152E47660065DBB2 /* libz.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = DDC4BF10152E455500089409 /* libz.dylib */; }; DDC4BEA8152E300800089409 /* libMapView.a in Frameworks */ = {isa = PBXBuildFile; fileRef = DDC4BEA3152E2FDE00089409 /* libMapView.a */; }; DDC4BEC1152E351F00089409 /* online.png in Resources */ = {isa = PBXBuildFile; fileRef = DDC4BEBF152E351F00089409 /* online.png */; }; @@ -54,6 +55,7 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + DD227A001551969500E73845 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = System/Library/Frameworks/CoreLocation.framework; sourceTree = SDKROOT; }; DD96557015264A080008517A /* MapView.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = MapView.xcodeproj; path = "MapBox-iOS-SDK/MapView/MapView.xcodeproj"; sourceTree = SOURCE_ROOT; }; DDC4BEBF152E351F00089409 /* online.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = online.png; sourceTree = ""; }; DDC4BEC0152E351F00089409 /* online@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "online@2x.png"; sourceTree = ""; }; @@ -89,6 +91,7 @@ buildActionMask = 2147483647; files = ( DDEF3A321522813600DE6DB0 /* CoreGraphics.framework in Frameworks */, + DD227A011551969500E73845 /* CoreLocation.framework in Frameworks */, DDEF3A301522813600DE6DB0 /* Foundation.framework in Frameworks */, DDEF3A6D1522847A00DE6DB0 /* QuartzCore.framework in Frameworks */, DDEF3A2E1522813600DE6DB0 /* UIKit.framework in Frameworks */, @@ -154,6 +157,7 @@ isa = PBXGroup; children = ( DDEF3A311522813600DE6DB0 /* CoreGraphics.framework */, + DD227A001551969500E73845 /* CoreLocation.framework */, DDEF3A2F1522813600DE6DB0 /* Foundation.framework */, DDEF3A6C1522847A00DE6DB0 /* QuartzCore.framework */, DDEF3A2D1522813600DE6DB0 /* UIKit.framework */, diff --git a/MapBox Example/OnlineLayerViewController.h b/MapBox Example/OnlineLayerViewController.h index ca69212..dbcda5b 100644 --- a/MapBox Example/OnlineLayerViewController.h +++ b/MapBox Example/OnlineLayerViewController.h @@ -8,6 +8,6 @@ #import -@interface OnlineLayerViewController : UIViewController +@interface OnlineLayerViewController : UIViewController @end \ No newline at end of file diff --git a/MapBox Example/OnlineLayerViewController.m b/MapBox Example/OnlineLayerViewController.m index 1a47bce..16f7e4a 100644 --- a/MapBox Example/OnlineLayerViewController.m +++ b/MapBox Example/OnlineLayerViewController.m @@ -11,27 +11,115 @@ #import "RMMapView.h" #import "RMMapBoxSource.h" +#import + +@interface OnlineLayerViewController () + +@property (nonatomic, strong) CLGeocoder *geocoder; +@property (nonatomic, strong) UISearchBar *searchBar; +@property (nonatomic, strong) RMMapView *mapView; + +@end + @implementation OnlineLayerViewController +@synthesize geocoder; +@synthesize searchBar; +@synthesize mapView; + - (void)viewDidLoad { [super viewDidLoad]; + // geocoder & search bar + // + self.geocoder = [[CLGeocoder alloc] init]; + + self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; + + self.searchBar.placeholder = @"123 Fifth Avenue Anytown, USA"; + + self.searchBar.delegate = self; + + [self.view addSubview:searchBar]; + + // map view + // RMMapBoxSource *onlineSource = [[RMMapBoxSource alloc] initWithReferenceURL:[NSURL URLWithString:@"http://a.tiles.mapbox.com/v3/mapbox.mapbox-streets.json"]]; - RMMapView *mapView = [[RMMapView alloc] initWithFrame:self.view.bounds andTilesource:onlineSource]; + self.mapView = [[RMMapView alloc] initWithFrame:CGRectMake(0, self.searchBar.bounds.size.height, self.view.bounds.size.width, self.view.bounds.size.height - self.searchBar.bounds.size.height) andTilesource:onlineSource]; + + self.mapView.zoom = 2; + + self.mapView.backgroundColor = [UIColor darkGrayColor]; + + self.mapView.decelerationMode = RMMapDecelerationFast; + + self.mapView.boundingMask = RMMapMinHeightBound; + + self.mapView.adjustTilesForRetinaDisplay = YES; + + [self.view addSubview:self.mapView]; - mapView.zoom = 2; + // tap gesture on map to dismiss search + // + UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)]; - mapView.backgroundColor = [UIColor darkGrayColor]; + tap.delegate = self; - mapView.decelerationMode = RMMapDecelerationFast; + [self.mapView addGestureRecognizer:tap]; +} + +#pragma mark - + +- (void)handleGesture:(UIGestureRecognizer *)gesture +{ + [self.searchBar resignFirstResponder]; +} + +#pragma mark - + +- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar +{ + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; - mapView.boundingMask = RMMapMinHeightBound; + [self.geocoder cancelGeocode]; +} + +- (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar +{ + [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; - mapView.adjustTilesForRetinaDisplay = YES; + [aSearchBar resignFirstResponder]; - [self.view addSubview:mapView]; + [self.geocoder geocodeAddressString:aSearchBar.text completionHandler:^(NSArray *placemarks, NSError *error) + { + [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; + + if ( ! placemarks) + { + [[[UIAlertView alloc] initWithTitle:@"Search Error" + message:@"There was an error while performing the search. Please try again." + delegate:nil + cancelButtonTitle:nil + otherButtonTitles:@"OK", nil] show]; + } + else + { + CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0]; + + self.mapView.zoom = 14; + + [self.mapView setCenterCoordinate:firstPlacemark.location.coordinate animated:NO]; + } + }]; +} + +#pragma mark - + +- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer +{ + return YES; } @end \ No newline at end of file From 604e33093ec7149b44a82b34f5c82d96f1d10a29 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Wed, 2 May 2012 17:27:03 -0400 Subject: [PATCH 2/2] zoom based on geocoder return region for better accuracy --- MapBox Example/OnlineLayerViewController.m | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/MapBox Example/OnlineLayerViewController.m b/MapBox Example/OnlineLayerViewController.m index 16f7e4a..880a2ac 100644 --- a/MapBox Example/OnlineLayerViewController.m +++ b/MapBox Example/OnlineLayerViewController.m @@ -108,9 +108,13 @@ - (void)searchBarSearchButtonClicked:(UISearchBar *)aSearchBar { CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0]; - self.mapView.zoom = 14; - - [self.mapView setCenterCoordinate:firstPlacemark.location.coordinate animated:NO]; + RMProjectedPoint projectedCenter = [self.mapView coordinateToProjectedPoint:firstPlacemark.location.coordinate]; + + [self.mapView setProjectedBounds:RMProjectedRectMake(projectedCenter.x - (firstPlacemark.region.radius / 2), + projectedCenter.y - (firstPlacemark.region.radius / 2), + firstPlacemark.region.radius, + firstPlacemark.region.radius) + animated:NO]; } }]; }