你能告诉我,我怎样才能在iPhone上获得GPS定位。
感谢。
答案 0 :(得分:13)
实现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;
}
答案 1 :(得分:4)
如果您使用的是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;
答案 2 :(得分:2)
您使用CoreLocation框架。 在这里,您可以获得示例代码http://mobileorchard.com/hello-there-a-corelocation-tutorial/