ioS谷歌地图v2无法正常工作 - 我的位置无法获取

时间:2015-06-12 10:12:03

标签: ios google-maps

我正在使用Google Map SDK处理iOS应用。我尝试设置CLLocationManager以获取设备的位置10秒,然后右下角按钮立即获取我的位置。当涉及到实现时,应用程序显示没有响应来启动方法startUpdatingLocation来获取我的位置。您能否告诉我使用locationManager完成目标的方法?

以下是我的工作:

-(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's shadow
            markera.map = mapView_;
        }else {
            markera.position = location.coordinate;

        }


    GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
    [mapView_ animateWithCameraUpdate:move];

}

0 个答案:

没有答案