-
Notifications
You must be signed in to change notification settings - Fork 0
/
SearchPlaceViewController.m
161 lines (125 loc) · 6.04 KB
/
SearchPlaceViewController.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
//
// SearchPlaceViewController.m
// Code_Library
//
// Created by RIGEL on 3/27/17.
// Copyright © 2017 RIGEL. All rights reserved.
//
#import "SearchPlaceViewController.h"
#import <GooglePlaces/GooglePlaces.h>
@interface SearchPlaceViewController () <GMSAutocompleteViewControllerDelegate,UITextFieldDelegate,CLLocationManagerDelegate>
//@property(strong, nonatomic) NSURLSession *markerSession;
{
GMSCameraPosition *camera;
}
@end
@implementation SearchPlaceViewController {
GMSMapView *mapView;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
camera = [GMSCameraPosition cameraWithLatitude:22.3072 longitude:73.1812 zoom:15 bearing:0 viewingAngle:0];
mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
mapView.myLocationEnabled = YES;
mapView.settings.myLocationButton = YES;
_txtSearchPlaces.delegate = self;
[self.view addSubview:mapView];
[self.view sendSubviewToBack:mapView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
GMSAutocompleteViewController *acController = [[GMSAutocompleteViewController alloc] init];
acController.delegate = self;
[self presentViewController:acController animated:YES completion:nil];
return NO;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
// Handle the user's selection.
- (void)viewController:(GMSAutocompleteViewController *)viewController
didAutocompleteWithPlace:(GMSPlace *)place {
[self dismissViewControllerAnimated:YES completion:nil];
// Do something with the selected place.
NSLog(@"Place name %@", place.name);
NSLog(@"Place address %@", place.formattedAddress);
NSLog(@"Place attributions %@", place.attributions.string);
_txtSearchPlaces.text = place.formattedAddress;
}
- (void)viewController:(GMSAutocompleteViewController *)viewController
didFailAutocompleteWithError:(NSError *)error {
[self dismissViewControllerAnimated:YES completion:nil];
// TODO: handle the error.
NSLog(@"Error: %@", [error description]);
}
// User canceled the operation.
- (void)wasCancelled:(GMSAutocompleteViewController *)viewController {
[self dismissViewControllerAnimated:YES completion:nil];
}
// Turn the network activity indicator on and off again.
- (void)didRequestAutocompletePredictions:(GMSAutocompleteViewController *)viewController {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)didUpdateAutocompletePredictions:(GMSAutocompleteViewController *)viewController {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
/** Now, find out the lat long from the searched address.
We use Goecoding google api to find out address's lat long.
**/
- (IBAction)btnShow:(id)sender {
NSString *urlString = [NSString stringWithFormat:@"%s&address=%@&key=%s",baseURLGeoCode,_txtSearchPlaces.text,GoogleAPIKeyServer];
NSLog(@"URL String is %@",urlString);
if ([urlString containsString:@" "]) {
urlString = [urlString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
}
NSURL *geocodeURL = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:geocodeURL];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask *datatask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error == nil) {
if (data != nil) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@",json);
NSArray *resultData = [json valueForKey:@"results"];
NSDictionary *locationDict = [resultData objectAtIndex:0];
NSDictionary *geometyDict = [locationDict valueForKey:@"geometry"];
NSDictionary *latLonDict = [geometyDict valueForKey:@"location"];
NSString *lati = [latLonDict valueForKey:@"lat"];
NSString *longi = [latLonDict valueForKey:@"lng"];
CLLocationDegrees latitudeDegree = [lati doubleValue];
CLLocationDegrees longitudeDegree = [longi doubleValue];
NSLog(@"Latitude is %@", lati);
NSLog(@"Longitude is %@", longi);
dispatch_async(dispatch_get_main_queue(), ^{
// [mapView removeFromSuperview];
// camera = [GMSCameraPosition cameraWithLatitude:latitudeDegree longitude:longitudeDegree zoom:20 bearing:0 viewingAngle:0];
// mapView = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
[mapView animateToLocation:CLLocationCoordinate2DMake(latitudeDegree, longitudeDegree)];
GMSMarker *marker2 = [[GMSMarker alloc]init];
marker2.position = CLLocationCoordinate2DMake(latitudeDegree, longitudeDegree);
marker2.icon = [GMSMarker markerImageWithColor:[UIColor redColor]];
marker2.map = mapView;
[mapView setMapType:kGMSTypeNormal];
[self.view addSubview:mapView];
[self.view bringSubviewToFront:mapView];
});
}
}
}];
[datatask resume];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end