【问题标题】:How can I get GPS location in Iphone如何在 Iphone 中获取 GPS 位置
【发布时间】:2023-04-10 16:59:01
【问题描述】:

你能告诉我,我如何在 iPhone 中获取 GPS 位置。

谢谢。

【问题讨论】:

  • 在提出问题之前,您甚至应该尝试搜索 Apple 的文档。而且这个问题非常笼统。

标签: iphone objective-c xcode ipad


【解决方案1】:

实现 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;     
 }

【讨论】:

  • 在 iOS 6 中 didUpdateToLocation 已弃用,因此请改用 didUpdateLocations
【解决方案2】:

如果您使用的是 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;

【讨论】:

    【解决方案3】:

    您使用 CoreLocation 框架。 在这里你可以得到示例代码http://mobileorchard.com/hello-there-a-corelocation-tutorial/

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 1970-01-01
      • 2014-07-12
      • 1970-01-01
      • 2013-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多