-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDDPMap.m
232 lines (199 loc) · 9.76 KB
/
DDPMap.m
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// DDPMap.m
// minijob
//
// Created by Dario De pascalis on 07/06/14.
// Copyright (c) 2014 Dario De Pascalis. All rights reserved.
//
#import "DDPMap.h"
#import "DDPApplicationContext.h"
@implementation DDPMap
- (void)fetchPlaceDetail:(NSString *)textSearch{
//NSString *str = [textSearch stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *str = [textSearch stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSLog(@"fetchPlaceDetail : %@ - %@", textSearch, str);
NSString *url=[self googleURLString:str];
//url = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
self.googleConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.responseData = [[NSMutableData alloc] init];
self.arraySearch = [[NSMutableArray alloc] init];
}
- (NSString *)googleURLString:(NSString *)textSearch {
//https://developers.google.com/places/documentation/autocomplete
NSString *URL_GOOGLE_AUTOCOMPLETE = [self.applicationContext.constantsPlist valueForKey:@"URL_GOOGLE_AUTOCOMPLETE"];
NSString *KEY_GOOGLE_MAP = [self.applicationContext.constantsPlist valueForKey:@"KEY_GOOGLE_MAP"];
//[textSearch stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableString *url = [NSMutableString stringWithFormat:URL_GOOGLE_AUTOCOMPLETE, textSearch, KEY_GOOGLE_MAP, self.latitude, self.longitude, self.radius, self.language];
NSLog(@"googleURLString: %@",url);
return url;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"didReceiveResponse");
if (connection == self.googleConnection) {
[self.responseData setLength:0];
}
}
- (void)connection:(NSURLConnection *)connnection didReceiveData:(NSData *)data {
NSLog(@"didReceiveData");
if (connnection == self.googleConnection) {
[self.responseData appendData:data];
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError %@", error);
if (connection == self.googleConnection) {
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
//NSString *responseString = [[NSString alloc] initWithData:self.responseData encoding:NSISOLatin1StringEncoding];
//NSLog(@"response: %@", responseString);
if (connection == self.googleConnection) {
NSError *error = nil;
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:self.responseData options:kNilOptions error:&error];
if (error) {
//[self failWithError:error];
NSLog(@"error: %@", error);
return;
}
if ([[response objectForKey:@"status"] isEqualToString:@"ZERO_RESULTS"]) {
[self succeedWithPlaces:[NSArray array]];
NSLog(@"ZERO_RESULTS: %@", response);
return;
}
if ([[response objectForKey:@"status"] isEqualToString:@"OK"]) {
NSLog(@"OK:");
[self succeedWithPlaces:[response objectForKey:@"predictions"]];
return;
}
}
}
//--------------------------------------------------//
- (void)succeedWithPlaces:(NSArray *)places {
//NSLog(@"succeedWithPlaces: %@",places);
[self.arraySearch removeAllObjects];
for (NSDictionary *place in places) {
[self.arraySearch addObject:place];
}
NSLog(@"arraySearch: ");
[self.delegate reloadTablePlace:self.arraySearch];
}
- (void)addPlacemarkAnnotationToMap:(NSString *)address mapView:(MKMapView *)mapView {
NSLog(@"addPlacemarkAnnotationToMap %@",address);
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
NSLog(@"%@", error);
} else {
CLPlacemark *placemark = [placemarks lastObject];
float spanX = 0.30725;
float spanY = 0.30725;
MKCoordinateRegion region;
region.center.latitude = placemark.location.coordinate.latitude;
region.center.longitude = placemark.location.coordinate.longitude;
region.span = MKCoordinateSpanMake(spanX, spanY);
[mapView setRegion:region animated:YES];
MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc] init];
myAnnotation.coordinate = CLLocationCoordinate2DMake(placemark.location.coordinate.latitude, placemark.location.coordinate.longitude);
//myAnnotation.title = @"Matthews Pizza";
//myAnnotation.subtitle = @"Best Pizza in Town";
[mapView addAnnotation:myAnnotation];
CLLocation *location = [[CLLocation alloc] initWithLatitude:myAnnotation.coordinate.latitude longitude:myAnnotation.coordinate.longitude];
[self.delegate addPlacemarkToMap:mapView location:location];
//[self.delegate saveCurrentLocation:location];
}
}];
}
- (MKMapView *)addPointAnnotation:(MKMapView *)mapView location:(CLLocation *)location{
float spanX = 0.20725;
float spanY = 0.20725;
MKCoordinateRegion region;
region.center.latitude = location.coordinate.latitude;
region.center.longitude = location.coordinate.longitude;
region.span = MKCoordinateSpanMake(spanX, spanY);
[mapView setRegion:region animated:YES];
MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc] init];
myAnnotation.coordinate = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude);
[mapView addAnnotation:myAnnotation];
return mapView;
}
-(void)getGeoPoint{
NSLog(@"getGeoPoint");
[self setPermissionLocationManager];
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
if (!error) {
CLLocation *newLocation = [[CLLocation alloc] initWithCoordinate:CLLocationCoordinate2DMake(geoPoint.latitude, geoPoint.longitude)
altitude:0
horizontalAccuracy:0
verticalAccuracy:0
timestamp:[NSDate date]];
NSLog(@"newLocation: %@",newLocation);
[self.delegate saveCurrentLocation:newLocation];
}else {
NSLog(@"location error: %@",error);
[self.delegate alertError:@"noSetGeopoint"];
}
}];
}
-(void)reverseGeocodeLocation:(CLLocation *)location{
NSLog(@"reverseGeocodeLocation");
[self setPermissionLocationManager];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if(error){
NSLog(@"%@", [error localizedDescription]);
}
CLPlacemark *placemark = [placemarks lastObject];
NSString *addressTxt = [NSString stringWithFormat:@"%@ (%@)",
placemark.locality,
placemark.subAdministrativeArea];
//placemark.country];
[self.delegate saveCurrentCity:addressTxt];
NSLog(@"addressTxt: %@", addressTxt);
}];
}
-(void)getCoordinateToReference:(NSString *)reference {
NSString *URL_GOOGLE_REFERENCE = [self.applicationContext.constantsPlist valueForKey:@"URL_GOOGLE_REFERENCE"];
NSString *url = [NSMutableString stringWithFormat:URL_GOOGLE_REFERENCE, reference, self.googleMapKey];
NSLog(@"downloading google reference %@", url);
NSURLRequest *request = [NSURLRequest requestWithURL: [NSURL URLWithString:url]];
NSURLConnection *googleConnectionReference = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (googleConnectionReference) {
//self.receivedData = [[NSMutableData alloc] init];
} else {
// Inform the user that the connection failed.
// [self connectionFailed];
}
//self.responseData = [[NSMutableData alloc] init];
//self.arraySearch = [[NSMutableArray alloc] init];
}
-(void)setPermissionLocationManager{
if (!self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
self.locationManager.distanceFilter = 5;
self.locationManager.delegate = self;
//[self.locationManager startUpdatingLocation];
// Do any additional setup after loading the view, typically from a nib.
//self.locationManager = [[CLLocationManager alloc]init];
// Use either one of these authorizations **The top one gets called first and the other gets ignored
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
[self.locationManager startUpdatingLocation];
}
}
//http://www.devfright.com/location-authorization-ios-8/
-(void)requestAlwaysAuth{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status==kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) {
NSString*title;
title=(status == kCLAuthorizationStatusDenied) ? @"Location Services Are Off" : @"Background use is not enabled";
NSString *message = @"Go to settings";
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:title message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Settings", nil];
[alert show];
}
else if (status==kCLAuthorizationStatusNotDetermined){
[self.locationManager requestAlwaysAuthorization];
}
}
@end