gpt4 book ai didi

iphone - 在 MKMapView 上获取用户当前位置

转载 作者:可可西里 更新时间:2023-11-01 04:23:03 24 4
gpt4 key购买 nike

我正在尝试通过 didUpdateToLocation:newLocation:fromLocation:oldLocation: 方法获取用户当前的最新位置,并将其存储在我的 ivar CLLocation *userCurrentLocation

当我尝试读取 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.
}

显示用户当前位置的工具栏按钮

-(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];

}

我确实设置了属性并合成了 userCurrentLocation:

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];
}

最佳答案

您的 userCurrentLocation- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation 方法完成后自动释放.

请尝试使用 self.userCurrentLocation = newLocation;

关于iphone - 在 MKMapView 上获取用户当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7227459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com