Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use Google Maps API for geocoding.

  • Loading branch information...
commit 2d58dd70df20ef5b1317f0d0a3fca599d6c18014 1 parent f7dd40a
@bcse bcse authored
View
2  ClassicMap.xcodeproj/project.pbxproj
@@ -83,6 +83,7 @@
087F5B371612E6AB004BF7D9 /* AFXMLRequestOperation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = AFXMLRequestOperation.m; path = Vendor/AFNetworking/AFXMLRequestOperation.m; sourceTree = SOURCE_ROOT; };
087F5B381612E6AB004BF7D9 /* UIImageView+AFNetworking.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImageView+AFNetworking.h"; path = "Vendor/AFNetworking/UIImageView+AFNetworking.h"; sourceTree = SOURCE_ROOT; };
087F5B391612E6AB004BF7D9 /* UIImageView+AFNetworking.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImageView+AFNetworking.m"; path = "Vendor/AFNetworking/UIImageView+AFNetworking.m"; sourceTree = SOURCE_ROOT; };
+ 0BAE9433161E117C0010F07F /* WMOverlayConstants.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WMOverlayConstants.h; sourceTree = "<group>"; };
1414DF4D1613D43000161754 /* Icon-72.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon-72.png"; sourceTree = "<group>"; };
1414DF4E1613D43000161754 /* Icon-72@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon-72@2x.png"; sourceTree = "<group>"; };
1414DF4F1613D43000161754 /* Icon-Small-50.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon-Small-50.png"; sourceTree = "<group>"; };
@@ -156,6 +157,7 @@
children = (
14EAE0EB16120FE70026BD7C /* WMOverlay.h */,
14EAE0EC16120FE70026BD7C /* WMOverlay.m */,
+ 0BAE9433161E117C0010F07F /* WMOverlayConstants.h */,
14EAE0F2161210A80026BD7C /* WMOverlayView.h */,
14EAE0F3161210A80026BD7C /* WMOverlayView.m */,
087F5B241612D13D004BF7D9 /* WMImageCache.h */,
View
6 ClassicMap/WMConfigurationViewController.h
@@ -8,11 +8,13 @@
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
-#import "WMMapViewController.h"
+#import "WMOverlayConstants.h"
+
+@protocol WMConfigurationViewControllerDelegate;
@interface WMConfigurationViewController : UIViewController
-@property (weak, nonatomic) id delegate;
+@property (weak, nonatomic) id<WMConfigurationViewControllerDelegate> delegate;
@property (assign, nonatomic) WMMapSource mapSource;
@property (assign, nonatomic) MKMapType mapType;
View
8 ClassicMap/WMDetailViewController.m 100644 → 100755
@@ -31,7 +31,13 @@ - (void)viewDidLoad
_headerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
_titleLabel.text = [NSString stringWithFormat:@"<%g/%g>", _placemark.coordinate.latitude, _placemark.coordinate.longitude];
_addressTitleLabel.text = NSLocalizedString(@"Address", nil);
- _addressLabel.text = ABCreateStringWithAddressDictionary(_placemark.addressDictionary, YES);
+ NSArray *addressLines = [_placemark.addressDictionary objectForKey:@"FormattedAddressLines"];
+ if (addressLines) {
+ _addressLabel.text = [addressLines componentsJoinedByString:@", "];
+ }
+ else {
+ _addressLabel.text = ABCreateStringWithAddressDictionary(_placemark.addressDictionary, YES);
+ }
_removePinLabel.text = NSLocalizedString(@"Remove Pin", nil);
View
10 ClassicMap/WMMapViewController.h
@@ -9,14 +9,10 @@
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
+#import "WMOverlayConstants.h"
+#import "WMConfigurationViewController.h"
-enum {
- WMMapSourceStandard = 0,
- WMMapSourceGoogle
-};
-typedef NSUInteger WMMapSource;
-
-@interface WMMapViewController : UIViewController
+@interface WMMapViewController : UIViewController <MKMapViewDelegate, WMConfigurationViewControllerDelegate>
@property (assign, nonatomic) MKMapType mapType;
@property (assign, nonatomic) WMMapSource mapSource;
View
88 ClassicMap/WMMapViewController.m 100644 → 100755
@@ -6,6 +6,7 @@
// Copyright (c) 2012 kishikawa katsumi. All rights reserved.
//
+#import "AFNetworking.h"
#import "WMMapViewController.h"
#import "WMDetailViewController.h"
#import "WMConfigurationViewController.h"
@@ -142,28 +143,70 @@ - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
[_mapView removeAnnotation:annotation];
}
}
+
+ NSString *searchString = [searchBar.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+ NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", searchString]];
+ NSURLRequest *request = [NSURLRequest requestWithURL:url];
- MKMapRect visibleMapRect = _mapView.visibleMapRect;
- MKMapPoint northeast = MKMapPointMake(MKMapRectGetMaxX(visibleMapRect), MKMapRectGetMinY(visibleMapRect));
- MKMapPoint southwest = MKMapPointMake(MKMapRectGetMinX(visibleMapRect), MKMapRectGetMaxY(visibleMapRect));
- CLLocationCoordinate2D neCoord = MKCoordinateForMapPoint(northeast);
- CLLocationCoordinate2D swCoord = MKCoordinateForMapPoint(southwest);
- CLLocationDistance diameter = [self getDistanceFrom:neCoord to:swCoord];
- CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:_mapView.centerCoordinate radius:(diameter / 2) identifier:@"search"];
+ AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
+ NSString *status = [JSON valueForKeyPath:@"status"];
+ if ([status isEqualToString:@"OK"]) {
+ NSArray *results = [JSON valueForKeyPath:@"results"];
+ NSInteger index = 0;
+ for (id result in results) {
+ NSDictionary *location = [[result valueForKey:@"geometry"] valueForKey:@"location"];
+ NSNumber *lat = [location valueForKey:@"lat"];
+ NSNumber *lng = [location valueForKey:@"lng"];
+ CLLocationCoordinate2D coord = CLLocationCoordinate2DMake([lat doubleValue], [lng doubleValue]);
+
+ NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];
+ addressDictionary[@"FormattedAddressLines"] = [((NSString *)[result valueForKey:@"formatted_address"]) componentsSeparatedByString:@", "];
+ for (id component in [result valueForKey:@"address_components"]) {
+ NSArray *types = [component valueForKey:@"types"];
+ id longName = [component valueForKey:@"long_name"];
+ id shortName = [component valueForKey:@"short_name"];
+ for (NSString *type in types) {
+ if ([type isEqualToString:@"postal_code"]) {
+ addressDictionary[@"ZIP"] = longName;
+ }
+ else if ([type isEqualToString:@"country"]) {
+ addressDictionary[@"Country"] = longName;
+ addressDictionary[@"CountryCode"] = shortName;
+ }
+ else if ([type isEqualToString:@"administrative_area_level_1"]) {
+ addressDictionary[@"State"] = longName;
+ }
+ else if ([type isEqualToString:@"administrative_area_level_2"]) {
+ addressDictionary[@"SubAdministrativeArea"] = longName;
+ }
+ else if ([type isEqualToString:@"locality"]) {
+ addressDictionary[@"City"] = longName;
+ }
+ else if ([type isEqualToString:@"sublocality"]) {
+ addressDictionary[@"SubLocality"] = longName;
+ }
+ else if ([type isEqualToString:@"establishment"]) {
+ addressDictionary[@"Name"] = longName;
+ }
+ else if ([type isEqualToString:@"route"]) {
+ addressDictionary[@"Thoroughfare"] = longName;
+ }
+ else if ([type isEqualToString:@"street_number"]) {
+ addressDictionary[@"SubThoroughfare"] = longName;
+ }
+ }
+ }
+ if (index == 0) {
+ [_mapView setCenterCoordinate:coord animated:NO];
+ }
+ MKPlacemark *placemark = [[MKPlacemark alloc] initWithCoordinate:coord addressDictionary:addressDictionary];
+ [_mapView addAnnotation:placemark];
+ index++;
+ }
+ }
+ } failure:nil];
- [_geocoder geocodeAddressString:searchBar.text inRegion:region completionHandler:^(NSArray *placemarks, NSError *error)
- {
- if (!error) {
- NSInteger index = 0;
- for (CLPlacemark *placemark in placemarks) {
- if (index == 0) {
- [_mapView setCenterCoordinate:placemark.location.coordinate animated:NO];
- }
- [_mapView addAnnotation:[[MKPlacemark alloc] initWithPlacemark:placemark]];
- index++;
- }
- }
- }];
+ [operation start];
[self finishSearch];
}
@@ -190,7 +233,7 @@ - (CLLocationDistance)getDistanceFrom:(CLLocationCoordinate2D)start to:(CLLocati
return [startLoccation distanceFromLocation:endLoccation];
}
-#pragma mark -
+#pragma mark - MKMapViewDelegate
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation; {
if (annotation == mapView.userLocation) {
@@ -237,7 +280,8 @@ - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view cal
[self performSegueWithIdentifier:@"Details" sender:view];
}
-#pragma mark -
+#pragma mark - WMConfigurationViewControllerDelegate
+
- (void)configurationViewController:(WMConfigurationViewController *)controller mapSourceChanged:(WMMapSource)mapSource
{
self.mapSource = mapSource;
View
13 ClassicMap/WMOverlayConstants.h
@@ -0,0 +1,13 @@
+//
+// WMOverlayConstants.h
+// ClassicMap
+//
+// Created by Grey Lee on 2012/10/05.
+// Copyright (c) 2012 kishikawa katsumi. All rights reserved.
+//
+
+enum {
+ WMMapSourceStandard = 0,
+ WMMapSourceGoogle
+};
+typedef NSUInteger WMMapSource;
Please sign in to comment.
Something went wrong with that request. Please try again.