【发布时间】:2023-04-10 16:59:01
【问题描述】:
你能告诉我,我如何在 iPhone 中获取 GPS 位置。
谢谢。
【问题讨论】:
-
在提出问题之前,您甚至应该尝试搜索 Apple 的文档。而且这个问题非常笼统。
标签: iphone objective-c xcode ipad
你能告诉我,我如何在 iPhone 中获取 GPS 位置。
谢谢。
【问题讨论】:
标签: iphone objective-c xcode ipad
实现 CLLocationManagerDelegate 并编写此函数并在您的 viewController 的 viewDidLoad 方法中调用此函数。
-(void)startLocationManager
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D coordinate = [newLocation coordinate];
double dblLatitude = coordinate.latitude;
double dblLongitude = coordinate.longitude;
}
【讨论】:
如果您使用的是 CLLocationmanager,请务必先检查位置服务是否已启用。我会这样做以获得当前位置:
if ([CLLocationManager locationServicesEnabled])
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
CLLocationCoordinate2D *currentLocation = locationManager.location;
[locationManager stopUpdatingLocation];
[locationManager release];
}
如果您使用的是 mapView,您可以使用以下方法获取当前位置:
[mapView setShowsUserLocation:YES];
CLLocationCoordinate2D *currentLocation = mapView.userLocation.coordinate;
【讨论】:
您使用 CoreLocation 框架。 在这里你可以得到示例代码http://mobileorchard.com/hello-there-a-corelocation-tutorial/
【讨论】: