-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFetchLocationController.m
More file actions
159 lines (134 loc) · 5.2 KB
/
FetchLocationController.m
File metadata and controls
159 lines (134 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// FetchLocationController.m
// LocationTracker
//
// Created by Naveen Katari on 17/11/15.
// Copyright (c) 2015 Sourcebits. All rights reserved.
//
#import "FetchLocationController.h"
#define METERS_PER_MILE 1609.344
@interface FetchLocationController ()<UISearchBarDelegate, UISearchDisplayDelegate>
{
NSMutableArray *matchingSearchResults;
CLPlacemark *locationPlacemark;
CLGeocoder *geoCoder;
MKLocalSearch *localSearch;
MKLocalSearchResponse *searchResponse;
NSInteger selectedIndex;
MKMapItem *item;
}
@end
@implementation FetchLocationController
- (void)viewDidLoad
{
[super viewDidLoad];
[_mapResultsTable registerNib:[UINib nibWithNibName:@"LocationDetailsViewCell" bundle:nil] forCellReuseIdentifier:@"LocationDetailsCell"];
self.searchBar.delegate = self;
geoCoder = [[CLGeocoder alloc]init];
if (_locationManager == nil)
{
_locationManager = [[CLLocationManager alloc]init];
[_locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[[self locationManager] setDelegate:self];
}
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
[_locationManager startUpdatingLocation];
[[self mapView] setShowsUserLocation:YES];
CLAuthorizationStatus authorizationStatus = [CLLocationManager authorizationStatus];
if( authorizationStatus == kCLAuthorizationStatusAuthorizedAlways )
{
[_locationManager startUpdatingLocation];
[[self mapView] setShowsUserLocation:YES];
}
_mapResultsTable.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) viewWillAppear:(BOOL)animated
{
}
#pragma mark - CLLocationManager Delegate methods
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
[geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
{
if(error == nil && [placemarks count] > 0)
{
locationPlacemark = [placemarks lastObject];
}
else
{
NSLog(@"%@", error.debugDescription);
}
}];
CLLocationCoordinate2D zoomLocation;
zoomLocation=location.coordinate;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
[self.mapView setRegion:viewRegion animated:YES];
[manager stopUpdatingLocation];
}
#pragma CLLocationManager didFailWithError delegate method
-(void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Unable to find the location");
NSLog(@"error %@",error.description);
}
#pragma Searchbar delegate methods
-(void) searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[localSearch cancel];
MKLocalSearchRequest *searchRequest = [[MKLocalSearchRequest alloc]init];
searchRequest.naturalLanguageQuery = self.searchBar.text;
searchRequest.region = self.mapView.region;
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
localSearch = [[MKLocalSearch alloc] initWithRequest:searchRequest];
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error)
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if (response.mapItems.count == 0 )
{
NSLog(@"No Matches Found");
}
else
for (item in response.mapItems)
{
NSLog(@"name == %@", item.name);
NSLog(@"Phone == %@", item.phoneNumber);
NSLog(@"Address == %@", item.placemark);
MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
annotation.coordinate = item.placemark.coordinate;
annotation.title = item.name;
annotation.subtitle = item.placemark.title;
[self.mapView addAnnotation:annotation];
}
searchResponse = response;
locationPlacemark = item.placemark;
[_mapResultsTable reloadData];
}];
}
#pragma Tableview delegate methods
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [searchResponse.mapItems count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *Identifier = @"LocationDetailsCell";
LocationDetailsViewCell *cell = [self.mapResultsTable dequeueReusableCellWithIdentifier:Identifier];
if (cell == nil) {
cell = [[LocationDetailsViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:Identifier];
}
item = searchResponse.mapItems[indexPath.row];
cell.locationNameLabel.text = item.name;
return cell;
}
-(void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
@end