Created
August 18, 2015 18:03
-
-
Save ziyang0621/f66dd536382b1b16597d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "GmapViewController.h" | |
#import "AFNetworking.h" | |
@import GoogleMaps; | |
@interface GmapViewController () | |
@end | |
@implementation GmapViewController | |
{ | |
GMSMapView *mapView_; | |
NSString *lat; | |
NSString *lng; | |
CLLocationDegrees latitude; | |
CLLocationDegrees longitude; | |
UIActivityIndicatorView *activityView; | |
} | |
-(void)geoCodeAddress | |
{ | |
NSCharacterSet *doNotWant = [NSCharacterSet characterSetWithCharactersInString:@":/,."]; | |
self.address = [[self.address componentsSeparatedByCharactersInSet: doNotWant] componentsJoinedByString: @""]; | |
NSString *urlString=[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/geocode/json?address=%@",self.address]; | |
urlString = [urlString stringByAddingPercentEscapesUsingEncoding: | |
NSUTF8StringEncoding]; | |
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:urlString]]; | |
manager.requestSerializer = [AFJSONRequestSerializer serializer]; | |
AFHTTPRequestOperation *operation = [manager GET:urlString parameters:nil | |
success:^(AFHTTPRequestOperation *operation, id responseObject) | |
{ | |
NSArray * results = [responseObject objectForKey:@"results"]; | |
NSDictionary *records=[results objectAtIndex:0]; | |
NSDictionary *geometry=[records objectForKey:@"geometry"]; | |
NSLog(@"geomatry is %@",geometry); | |
NSDictionary *latLong=[geometry objectForKey:@"location"]; | |
lat=[latLong objectForKey:@"lat"]; | |
lng=[latLong objectForKey:@"lng"]; | |
latitude=[lat floatValue]; | |
longitude=[lng floatValue]; | |
NSLog(@"main lat is %f",latitude); | |
NSLog(@"main lng is %f",longitude); | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[self activityIndicator:@"hide"]; | |
[self updateMap]; | |
}); | |
} | |
failure:^(AFHTTPRequestOperation *operation, NSError *error) | |
{ | |
NSLog(@"failure string is"); | |
[self activityIndicator:@"hide"]; | |
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Unable to display map" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; | |
[alert show]; | |
}]; | |
[operation start]; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
[self activityIndicator:@"show"]; | |
[self Loadgmap]; | |
[self geoCodeAddress]; | |
} | |
-(void)updateMap { | |
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[lat floatValue] | |
longitude:[lng floatValue] | |
zoom:6]; | |
GMSMarker *marker = [[GMSMarker alloc] init]; | |
marker.position = camera.target; | |
marker.snippet = @"Hello World"; | |
marker.appearAnimation = kGMSMarkerAnimationPop; | |
marker.map = (GMSMapView*)self.view; | |
[((GMSMapView*)self.view) animateToCameraPosition:camera]; | |
} | |
-(void)Loadgmap | |
{ | |
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:[lat floatValue] | |
longitude:[lng floatValue] | |
zoom:6]; | |
GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera]; | |
GMSMarker *marker = [[GMSMarker alloc] init]; | |
marker.position = camera.target; | |
marker.snippet = @"Hello World"; | |
marker.appearAnimation = kGMSMarkerAnimationPop; | |
marker.map = mapView; | |
self.view = mapView; | |
} | |
-(void)activityIndicator:(NSString *)show | |
{ | |
if([show isEqual:@"show"]) | |
{ | |
NSLog(@"loading shown"); | |
[[UIApplication sharedApplication] beginIgnoringInteractionEvents]; | |
activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; | |
activityView.layer.backgroundColor = [[UIColor colorWithWhite:0.0f alpha:0.5f] CGColor]; | |
activityView.hidesWhenStopped = YES; | |
activityView.frame = self.view.bounds; | |
[self.view addSubview:activityView]; | |
[activityView startAnimating]; | |
} | |
else | |
{ | |
[[UIApplication sharedApplication] endIgnoringInteractionEvents]; | |
[activityView stopAnimating]; | |
[activityView removeFromSuperview]; | |
} | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
// Dispose of any resources that can be recreated. | |
} | |
/* | |
#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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment