ios定期获取我的位置,并设置button来获取我的位置 – 谷歌地图不工作

我正在使用Google Map Module,使用1.9.0 Bundle来定期查找我的位置,比如说10秒钟,并创build一个button来获取我的位置button作为一个地图视图设置

当执行和位置服务打开时,单击右下angularbutton后没有响应,位置pipe理器不工作。 我可以显示使用位置服务的任何迹象。

我不知道我在设置这个地图上缺less什么,因为我严格遵循SDKDemos – iOS的谷歌演示项目来设置我的模块。 你能告诉我确切的方法来设置它吗?

以下是我的代码:

@implementation MapViewController -(bool)isNetworkAvailable { SCNetworkReachabilityFlags flags; SCNetworkReachabilityRef address; address = SCNetworkReachabilityCreateWithName(NULL, "www.apple.com" ); Boolean success = SCNetworkReachabilityGetFlags(address, &flags); CFRelease(address); bool canReach = success && !(flags & kSCNetworkReachabilityFlagsConnectionRequired) && (flags & kSCNetworkReachabilityFlagsReachable); return canReach; } - (void)requestAlwaysAuthorization { CLAuthorizationStatus status = [CLLocationManager authorizationStatus]; // If the status is denied or only granted for when in use, display an alert if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusDenied) { NSString *title; title = (status == kCLAuthorizationStatusDenied) ? @"Location services are off" : @"Background location is not enabled"; NSString *message = @"To use background location you must turn on 'Always' in the Location Services Settings"; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Settings", nil]; [alertView show]; } // The user has not enabled any location services. Request background authorization. else if (status == kCLAuthorizationStatusNotDetermined) { [locationManager requestAlwaysAuthorization]; } } - (void)viewDidLoad { [super viewDidLoad]; if([self isNetworkAvailable]){ NSLog(@"connected "); }else { NSLog(@"not connected "); } CarArray = [[NSMutableArray alloc] init]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; float latitide = [defaults floatForKey:@"lati"]; float longitude = [defaults floatForKey:@"longi"]; NSString *desp = [defaults objectForKey:@"desp"]; GMSMarker *marker = [[GMSMarker alloc] init]; NSLog(@"assadsd arrived map"); if(latitide!=0.00&&longitude!=0.00) { CLLocationCoordinate2D position = CLLocationCoordinate2DMake(latitide, longitude); marker.position = CLLocationCoordinate2DMake(position.latitude, position.longitude); camera = [GMSCameraPosition cameraWithLatitude:latitide longitude:longitude zoom:12]; }else{ camera = [GMSCameraPosition cameraWithLatitude:22.2855200 longitude:114.1576900 zoom:12]; marker.position = CLLocationCoordinate2DMake(22.2855200, 114.1576900); } mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; marker.snippet = @"HK"; mapView_.mapType = kGMSTypeSatellite; mapView_.delegate = self; mapView_.settings.myLocationButton = YES; mapView_.settings.compassButton = YES; dispatch_async(dispatch_get_main_queue(), ^{ mapView_.myLocationEnabled = YES; }); if(desp.length > 0 ){ marker.title = desp; } // Setup location services if (![CLLocationManager locationServicesEnabled]) { NSLog(@"Please enable location services"); return; } if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) { NSLog(@"Please authorize location services"); return; } locationManager = [[CLLocationManager alloc]init]; locationManager.delegate = self; [locationManager requestWhenInUseAuthorization]; [locationManager requestAlwaysAuthorization]; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyBest; locationManager.distanceFilter = 5.0f; [mapView_ addObserver:self forKeyPath:@"myLocation" options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld) context:NULL]; marker.map = mapView_; self.view = mapView_; [locationManager startUpdatingLocation]; GMSCircle *geoFenceCircle = [GMSCircle circleWithPosition: CLLocationCoordinate2DMake(22.2855200, 114.1576900) radius:1400]; geoFenceCircle.tappable = true; [geoFenceCircle setFillColor:[UIColor colorWithRed:1 green:0 blue:0 alpha:.5]]; geoFenceCircle.strokeWidth = 13; geoFenceCircle.strokeColor = [UIColor orangeColor]; geoFenceCircle.map = mapView_; // Add it to the map. NSLog(@"assadsd configured d map"); } - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (!firstLocationUpdate_) { // If the first location update has not yet been recieved, then jump to that // location. firstLocationUpdate_ = YES; CLLocation *location = [change objectForKey:NSKeyValueChangeNewKey]; mapView_.camera = [GMSCameraPosition cameraWithTarget:location.coordinate zoom:14]; } } - (void)dealloc { [mapView_ removeObserver:self forKeyPath:@"myLocation" context:NULL]; } -(void)mapView:(GMSMapView *)mapView didDragMarker:(GMSMarker *)marker { [mapView clear]; // Re-draw marker marker.map = mapView; // Create your circle with the new marker GMSCircle *circ = [GMSCircle circleWithPosition:marker.position radius:1000]; circ.fillColor = [UIColor grayColor]; circ.strokeColor = [UIColor redColor]; circ.strokeWidth = 5; circ.map = mapView; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSLog(@"buttonIndex:%ld",(long)buttonIndex); if (alertView.tag == 121 && buttonIndex == 1) { //code for opening settings app in iOS 8 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]; } } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"%@",error.userInfo); if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) { NSLog(@"Please authorize location services"); return; } NSLog(@"CLLocationManager error: %@", error.localizedFailureReason); return; } -(void) handleDoubleTap { NSLog(@"location double tap "); } -(UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } // CLLocationDelegate - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ CLLocation *location = [locations lastObject]; if (markera == nil) { markera = [[GMSMarker alloc] init] ; markera.position = CLLocationCoordinate2DMake(22.86, 111.20); markera.groundAnchor = CGPointMake(0.5f, 0.97f); // Taking into account walker's shadow markera.map = mapView_; }else { [CATransaction begin]; [CATransaction setAnimationDuration:2.0]; markera.position = location.coordinate; [CATransaction commit]; } GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17]; [mapView_ animateWithCameraUpdate:move]; } 

我已经在我的项目中设置了pinfo.list的以下参数

NSLocationAlwaysUsageDescription – >位置是需要找出你在哪里NSLocationWhenInUseUsageDescription – >位置是需要找出你在哪里

我的testing设备是8.3,iphone 6

你的- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ get called?

如果没有,你需要确保你的MapViewController实现了CLLocationDelegate 。 (例如: MapViewController.h文件中的@interface ViewController : UIViewController<CLLocationManagerDelegate>

你可以试试GitHub的这个工作例子 ,或者从这个Gist 的代码片断 。

另外,你应该在你真实的设备上testing它,否则你必须在你的XCode模拟器中模拟一个位置(见下图,但它可能不总是工作)。

在这里输入图像说明

您必须将NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription添加到您的info.plist文件(注意:不要重命名或更改info.plist文件的扩展名,扩展名必须为.plist ):

在这里输入图像说明

在这里输入图像说明