2011-08-29 13 views
5

Próbuję dostać użytkownikowi aktualny najnowszy lokalizacja na metodzie didUpdateToLocation:newLocation:fromLocation:oldLocation: i przechowywać go w moim ivar CLLocation *userCurrentLocationGet obsługi bieżącej lokalizacji na MKMapView

otrzymuję ten błąd, gdy próbowałem czytać userCurrentLocation

-[NSCFNumber coordinate]: unrecognized selector sent to instance 0x586b220 



- (void) locationManager:(CLLocationManager *) manager 
    didUpdateToLocation:(CLLocation *) newLocation 
      fromLocation:(CLLocation *) oldLocation { 
      fromLocation:(CLLocation *) oldLocation { 
if (oldLocation == nil) { // first time loading? 
     SVGeocoder *geocodeRequest = [[SVGeocoder alloc] 
             initWithCoordinate:CLLocationCoordinate2DMake(newLocation.coordinate.latitude,  newLocation.coordinate.longitude)]; 
     [geocodeRequest setDelegate:self]; 
     [geocodeRequest startAsynchronous]; // reverse geocoding to get annotation coordinates to be placed. 
     [geocodeRequest release]; 
     isCurrentLocation = YES; //flag that next annotation placed is current location 
    } 
     userCurrentLocation = newLocation; 
NSLog(@"didUpdateToLocation - Lat:%f Long:%f", userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude); // verified that userCurrentLocation latitude is correct at this point. 
} 

przycisk paska narzędzi, aby wyświetlić bieżącą lokalizację użytkownika

-(IBAction) showLocation:(id) sender { 
    NSLog(@"Lat:%f Long:%f", userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude); // crashes upon calling this statement. Why? 
    NSLog(@"Show Location Called"); 
    SVGeocoder *geocodeRequest = [[SVGeocoder alloc] 
            initWithCoordinate:CLLocationCoordinate2DMake(userCurrentLocation.coordinate.latitude, userCurrentLocation.coordinate.longitude)]; 
    [geocodeRequest setDelegate:self]; 
    [geocodeRequest startAsynchronous]; 
    [geocodeRequest release]; 

} 

zrobiłem ustawić właściwość i syntezy userCurrentLocation jako takie:

MapViewController.h

@interface MapViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate> { 
    CLLocation *userCurrentLocation; 
} 
@property (nonatomic, retain) CLLocation *userCurrentLocation; 
@end 

MapViewController.m

@synthesize userCurrentLocation; 

      /* EDITED */ 
- (void)viewDidLoad { 
    locationManager = [[CLLocationManager alloc] init]; 
locationManager.delegate = self; 
locationManager.distanceFilter = kCLDistanceFilterNone; 
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 
[locationManager startUpdatingLocation]; 
    userCurrentLocation = [[CLLocation alloc] init]; 
} 

Odpowiedz

5

Twój userCurrentLocation jest autoreleased po zakończeniu metoda - (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation.

Zamiast tego spróbuj użyć self.userCurrentLocation = newLocation;.

5
- (void)viewDidLoad { 
    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0,46,320,370)]; 
    [mapView setDelegate:self]; 
    [mapView setShowsUserLocation:TRUE];  
    [self.view addSubview:mapView]; 
    [mapView setMapType:MKMapTypeStandard]; 

    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; 
    if(locationManager.locationServicesEnabled) 
    { 
     [locationManager startUpdatingLocation]; 
    } 
} 
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { 
    LatNLon = newLocation.coordinate; 
    NSLog(@"%f %f",LatNLon.latitude,LatNLon.longitude); 
    MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:LatNLon]; 
    [geocoder setDelegate:self]; 
    [geocoder start]; 
    isVisit=0; 
    [locationManager stopUpdatingLocation]; 
} 
+0

już zrobił, co zrobiłeś w viewDidLoad. Jeśli chodzi o odwrotne geokodowanie, używam innej klasy ('SVGeocoder') poza Apple' MKReverseGeoder'. Wszystko działa dobrze, tylko wydaje się, że odczytywanie obiektu 'userCurrentLocation' wewnątrz IBAction powoduje awarię aplikacji i nie mogę się domyślić, dlaczego. –

+0

Myślę, że nie otrzymujesz aktualnej lokalizacji użytkownika. To dlatego aplikacja ulega awarii. – Ron

0

Jest to kod dla UIPageControl w UIScrollView tutaj używamy delegata UIScrollView

- (void)scrollViewDidScroll:(UIScrollView *)scrollView 
{ 
    int pageIndex=scrollView.contentOffset.x/scrollView.frame.size.width; 
    self.pageCntrl.currentPage=pageIndex; 
} 
+0

Polecamy rozszerzoną tę odpowiedź, aby wyjaśnić nieco więcej, dlaczego i w jaki sposób odpowiada na to pytanie. – brandonscript

0
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 
CLLocation *currentUserLocation = [locations lastObject]; 
NSLog(@"lat%f - lon%f", currentUserLocation.coordinate.latitude, currentUserLocation.coordinate.longitude); 

CLLocationCoordinate2D zoomLocation; 
zoomLocation.latitude = currentUserLocation.coordinate.latitude; 
zoomLocation.longitude= currentUserLocation.coordinate.longitude; 
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.2*METERS_PER_MILE, 0.2*METERS_PER_MILE); 
[mapView setRegion:viewRegion animated:YES]; 
[locationManager stopUpdatingLocation]; 

}

Powiązane problemy