IoS Google map v2 not working - my location could not be obtained

I am working on an iOS application using the Google Map SDK. I am trying to install on CLLocationManager to get the location of my device in 10 seconds and the bottom right button to instantly get my location. When it comes to implementation, the application does not respond to starting the startUpdatingLocation method to get my location. Could you tell me how to use locationManager to achieve the goal?

The following is my work:

-(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)viewDidLoad { [super viewDidLoad]; // [self getTime]; if (![CLLocationManager locationServicesEnabled]) { NSLog(@"Please enable location services"); return; } if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) { NSLog(@"Please authorize location services"); return; } if([self isNetworkAvailable]){ NSLog(@"connected "); }else { NSLog(@"not connected "); } CarArray = [[NSMutableArray alloc] init]; GMSMarker *marker = [[GMSMarker alloc] init]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; float latitide = [defaults floatForKey:@"lati"]; float longitude = [defaults floatForKey:@"longi"]; NSString *desp = [defaults objectForKey:@"desp"]; 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); } if(desp.length > 0 ){ marker.title = desp; } self.locationManager = [[CLLocationManager alloc]init]; self.locationManager.delegate = self; [self.locationManager requestWhenInUseAuthorization]; [self.locationManager requestAlwaysAuthorization]; self.locationManager.desiredAccuracy = kCLLocationAccuracyBest ; self.locationManager.distanceFilter = 5.0f; mapView_ = [GMSMapView mapWithFrame:CGRectZero camera:camera]; marker.snippet = @"HK"; marker.map = mapView_; mapView_.mapType = kGMSTypeSatellite; mapView_.delegate = self; dispatch_async(dispatch_get_main_queue(), ^{ mapView_.myLocationEnabled = YES; }); mapView_.settings.compassButton = YES; mapView_.settings.myLocationButton = YES; [mapView_ addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:NULL]; self.view = mapView_; [self.locationManager startUpdatingLocation]; NSLog(@"assadsd configured d map"); } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) { NSLog(@"Please authorize location services"); return; } NSLog(@"CLLocationManager error: %@", error.localizedFailureReason); NSLog(@"didFailWithError: %@", error); UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"application_name", nil) message:NSLocalizedString(@"location_error", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) otherButtonTitles:nil]; [errorAlert show]; return; } -(void) handleDoubleTap { NSLog(@"location double tap "); } -(UIStatusBarStyle)preferredStatusBarStyle { return UIStatusBarStyleLightContent; } // CLLocationDelegate - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{ // Optional: check error for desired accuracy CLLocation* location = [locations lastObject]; NSLog(@"location x : %f" , location.coordinate.longitude); NSLog(@"location y : %f" , location.coordinate.latitude); NSDate* eventDate = location.timestamp; NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; [manager allowDeferredLocationUpdatesUntilTraveled:CLLocationDistanceMax timeout:10]; if (markera == nil) { markera = [[GMSMarker alloc] init] ; markera.position = CLLocationCoordinate2DMake( location.coordinate.latitude , location.coordinate.longitude ); markera.groundAnchor = CGPointMake(0.5f, 0.97f); // Taking into account walker shadow markera.map = mapView_; }else { markera.position = location.coordinate; } GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17]; [mapView_ animateWithCameraUpdate:move]; 

}

+5
source share

All Articles