tôi đang cố gắng để có được vị trí người dùng mới nhất hiện hành về phương pháp didUpdateToLocation:newLocation:fromLocation:oldLocation:
và lưu nó trong Ivar tôi CLLocation *userCurrentLocation
Nhận dùng hiện vị trí trên MKMapView
tôi nhận được lỗi này khi tôi đã cố gắng để đọc 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.
}
Nút Thanh công cụ để hiển thị vị trí hiện tại của người dùng
-(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];
}
Tôi đã đặt thuộc tính và tổng hợp người dùngCurrentLocation như vậy:
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];
}
Tôi đã làm những gì bạn đã làm trong viewDidLoad. Đối với mã hóa địa lý đảo ngược, tôi đang sử dụng một lớp khác ('SVGeocoder') ngoài' MKReverseGeoder' của Apple. Tất cả các công trình ra tốt, chỉ có vẻ như là đọc đối tượng 'userCurrentLocation' bên trong IBAction treo ứng dụng và tôi không thể tìm ra lý do tại sao. –
tôi nghĩ rằng bạn không nhận được vị trí người dùng hiện tại. Đó là lý do tại sao ứng dụng bị lỗi. – Ron