我正在使用Google Maps SDK for iOS显示谷歌地图。当我第一次启动视图控制器时,它显示地图正常。但是当我第二次转到视图控制器时,它不会显示谷歌地图。它显示空白屏幕。实际上我正在通过谷歌地理编码api中的地址传递lang & lat
然后我显示谷歌地图。
显示谷歌地图的代码
//
// GmapViewController.m
// MyDex
// Created by Admin on 8/18/15.
// Copyright (c) 2015 com.vastedge. All rights reserved.
#import "GmapViewController.h"
#import "AFNetworking.h"
#import "UIKit+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);
[self activityIndicator:@"hide"];
[self Loadgmap];
}
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 geoCodeAddress];
}
-(void)Loadgmap
{
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
longitude:151.2086
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
答案 0 :(得分:1)
使用dispatch_async(dispatch_get_main_queue(), ^{})
是一种更好的做法,但white screen
问题的主要问题是view
ViewController
被分配给新值两次。
当您[self Loadgmap]
中调用viewDidLoad()
时,会调用self.view = mapView;
。完成所需的网络后,系统会再次调用[self Loadgmap]
,然后再次调用self.view = mapView;
,这会使您的视图变为白屏。
您应该只在view
方法中为viewDidLoad()
分配值,而不是在其他方法调用中分配。
要解决您的问题,您可以拨打新的方法-(void)updateMap()
:
-(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];
}
您应该在网络请求成功块中调用它:
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];
});
您的viewDidLoad()
应首先致电[self Loadgmap]
,将Google地图初始化为view
。
- (void)viewDidLoad
{
[super viewDidLoad];
[self activityIndicator:@"show"];
[self Loadgmap];
[self geoCodeAddress];
}
完整代码段:https://gist.github.com/ziyang0621/f66dd536382b1b16597d