Skip to content

Commit

Permalink
Adding search bar in map view to change easily location #133
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri Dessus authored and Dimitri Dessus committed Aug 23, 2016
1 parent 8819118 commit 17c7f97
Show file tree
Hide file tree
Showing 9 changed files with 251 additions and 80 deletions.
Binary file not shown.
23 changes: 23 additions & 0 deletions iPokeGo/Assets.xcassets/search.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "search_filled-2.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "search_filled-1.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "search_filled.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
186 changes: 107 additions & 79 deletions iPokeGo/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion iPokeGo/MapViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
#import "SVPulsingAnnotationView.h"
#import "global.h"

@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate, UITextFieldDelegate>
@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate, UITextFieldDelegate, UISearchBarDelegate, UITableViewDelegate>

@property(weak, nonatomic) IBOutlet UIButton *locationButton;
@property(weak, nonatomic) IBOutlet UIButton *radarButton;
@property(weak, nonatomic) IBOutlet MKMapView *mapview;
@property(weak, nonatomic) IBOutlet UISearchBar *searchBar;

-(IBAction)locationAction:(id)sender;
-(IBAction)radarAction:(id)sender;
-(IBAction)maptypeAction:(id)sender;
-(IBAction)searchButtonAction:(id)sender;

@end
116 changes: 116 additions & 0 deletions iPokeGo/MapViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ @interface MapViewController() <NSFetchedResultsControllerDelegate, UIGestureRec
@property NSMutableArray *annotationsSpawnpointsToDelete;
@property NSMutableArray *annotationsLocationsToDelete;

@property NSDictionary *locationsSearched;

@property CLLocationManager *locationManager;
@property NSDictionary *localization;
@property CLLocationDegrees oldLatitudeDelta;
Expand Down Expand Up @@ -915,6 +917,73 @@ -(void)maptypeAction:(id)sender
[self presentViewController:alert animated:YES completion:nil];
}

-(IBAction)searchButtonAction:(id)sender
{
BOOL hide;
NSUInteger animation;
if(self.searchBar.hidden) {
hide = NO;
animation = UIViewAnimationOptionTransitionFlipFromTop;

} else {
hide = YES;
animation = UIViewAnimationOptionTransitionFlipFromBottom;
}

[UIView transitionWithView:self.searchBar duration:0.5f options:animation animations:^(void){

[self.searchBar setHidden:hide];

} completion:nil];

[self.searchBar resignFirstResponder];
self.locationsSearched = nil;
[self.searchDisplayController.searchResultsTableView reloadData];
}

#pragma mark - Search bar delegate

-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
[self searchLocationsForAddress:searchBar.text];

return YES;
}

#pragma mark - Search display controller

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.locationsSearched[@"results"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"LocationCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = self.locationsSearched[@"results"][indexPath.row][@"formatted_address"];

return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
[self.searchDisplayController setActive:NO animated:YES];

double latitude = [self.locationsSearched[@"results"][indexPath.row][@"geometry"][@"location"][@"lat"] doubleValue];
double longitude = [self.locationsSearched[@"results"][indexPath.row][@"geometry"][@"location"][@"lng"] doubleValue];

CLLocationCoordinate2D coordinateLocation = CLLocationCoordinate2DMake(latitude, longitude);

[self.mapview setCenterCoordinate:coordinateLocation animated:NO];
}

#pragma mark - Misc

- (CLLocation *)currentLocation
Expand All @@ -931,6 +1000,53 @@ -(void)showAnnotationLocalNotif:(NSNotification *)notification
[self.mapview setRegion:MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(MAP_SCALE_ANNOT, MAP_SCALE_ANNOT)) animated:YES];
}

-(void)searchLocationsForAddress:(NSString *)addr
{
/*
We can use CLGeocoder from Apple but Google seems to be better for little town search ;)
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addr completionHandler:^(NSArray *placemarks, NSError *error) {
//Error checking
NSLog(@"%@", placemarks);
}];
*/

// Build the string to Query Google Maps.
NSString *apiMapsURL = GOOGLE_MAP_SEARCH_API;
apiMapsURL = [apiMapsURL stringByReplacingOccurrencesOfString:@"%%addr%%" withString:addr];
apiMapsURL = [apiMapsURL stringByReplacingOccurrencesOfString:@" " withString:@"%20"];

// Create url from the query
NSURL *url = [NSURL URLWithString:apiMapsURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];

// Launch connexion in background
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{

if ([data length] > 0 && error == nil)
{

// Parse JSON locations in NSDictionnary
self.locationsSearched = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
[self.searchDisplayController.searchResultsTableView reloadData];
});
}

}];
}

#pragma mark - Textfield delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Expand Down
2 changes: 2 additions & 0 deletions iPokeGo/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#define NOTIF_FOLLOW_GREEN_COLOR [UIColor colorWithRed:0.10 green:0.74 blue:0.61 alpha:1.0]
#define NOTIF_FOLLOW_RED_COLOR [UIColor colorWithRed:0.91 green:0.30 blue:0.24 alpha:1.0]

#define GOOGLE_MAP_SEARCH_API @"http://maps.googleapis.com/maps/api/geocode/json?address=%%addr%%"

#define SERVER_API_DATA_POKEMONGOMAP @"%%server_addr%%/raw_data?pokemon=%%pokemon_display%%&pokestops=%%pokestops_display%%&gyms=%%gyms_display%%&spawnpoints=%%spawnpoints_display%%&ids=%%idlist%%"
#define SERVER_API_DATA_SCAN_LOCATION @"%%server_addr%%/loc"
#define SERVER_API_DATA_POGOM @"%%server_addr%%/map-data?pokemon=%%pokemon_display%%&gyms=%%gyms_display%%"
Expand Down

1 comment on commit 17c7f97

@Bizadon
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@istornz can you explain how does it work? Does it must find an address?

Please sign in to comment.