【问题标题】:How to get device current latitude and longitude for IPhone device如何获取 iPhone 设备的当前经纬度
【发布时间】:2013-01-25 07:26:58
【问题描述】:

我正在开发一个应用程序,我想为 iPhone 获取设备当前的纬度和经度。不使用 GPS 是否可以获取这些信息。

谢谢

【问题讨论】:

    标签: iphone ios5 core-location


    【解决方案1】:

    你可以使用CoreLocation framework

    Core Location 框架有一个重要的类CLLocationManager。

    CLLocationManager 类在位置更改时处理向委托发送更新。它使用委托CLLocationManagerDelegate 协议。

    你可以像这样使用它

    #import <CoreLocation/CoreLocation.h>
    
    @interface ViewController : UIViewController 
      <CLLocationManagerDelegate>
    {
      CLLocationManager *locationManager;
    
    }
    

    在 viewDidLoad 中这样设置 locationManager

    - (void)viewDidLoad {
      [super viewDidLoad];
      locationManager = [[CLLocationManager alloc] init];
      locationManager.delegate = self;
      locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
      locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
      [locationManager startUpdatingLocation];
    }
    

    实现以下委托方法以获取更改位置

     - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
               fromLocation:(CLLocation *)oldLocation
    {
      NSInteger degrees = newLocation.coordinate.latitude;
      double decimal = fabs(newLocation.coordinate.latitude - degrees);
      int minutes = decimal * 60;
      double seconds = decimal * 3600 - minutes * 60;
      NSString *lattitudeStr = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                       degrees, minutes, seconds];
      lattitudeLabel.text = lattitudeStr;
      degrees = newLocation.coordinate.longitude;
      decimal = fabs(newLocation.coordinate.longitude - degrees);
      minutes = decimal * 60;
      seconds = decimal * 3600 - minutes * 60;
      NSString *longitudeStr = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
                         degrees, minutes, seconds];
      longitudeLabel.text = longitudeStr;
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用CLLocationManager 获取当前的经纬度。 创建 iVar 的 CLLocationManager 并使用它。

      代码如下:

      - (void)viewDidLoad
      {
      [super viewDidLoad];
      locationManager = [[CLLocationManager alloc] init];
      locationManager.delegate = self;
      locationManager.distanceFilter = kCLDistanceFilterNone;
      locationManager.desiredAccuracy = kCLLocationAccuracyBest;
      [locationManager startUpdatingLocation];
      }
      
      
      - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
      NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
      NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
      
      currentLat = [NSString stringWithFormat:@"%f",newLocation.coordinate.latitude];
      currentLong = [NSString stringWithFormat:@"%f",newLocation.coordinate.longitude];
      }
      

      【讨论】:

        【解决方案3】:
        CLLocationManager *locationManager = [[CLLocationManager alloc] init];
        CLLocation* currentLocation = [locationManager location];
        
        NSString *loc = [NSString stringWithFormat:@"%f,%f",currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
        NSLog(@"location:%@", loc);
        

        【讨论】:

          【解决方案4】:

          如果设备有 WiFi 或蜂窝数据服务,您可以在没有 GPS 的情况下使用 CLLocationManager 获取当前位置。该位置不会像您从 GPS 获得的那样准确,但您会比从 GPS 获得更快的结果。

          您可以通过将 desiredAccuracy 属性设置为 kCLLocationAccuracyKilometer 从 CLLocationManager 请求非 GPS 位置。

          【讨论】:

            【解决方案5】:

            使用 Swift

            import CoreLocation
            

            获取当前位置

            let locationManager = CLLocationManager()
            locationManager.distanceFilter = kCLDistanceFilterNone
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            
            //get the most recently retrieved user location
            if let currentLocation = locationManager.location {
                let longitude = currentLocation.coordinate.longitude
                let latitude = currentLocation.coordinate.latitude
                //work with the received data
            }
            

            请注意,locationManager.location 是可选的,因此应该安全地展开。

            【讨论】:

              猜你喜欢
              • 2012-02-04
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-09-07
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多