0

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

谢谢

4

5 回答 5

3

您可以使用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;
}
于 2013-01-26T15:36:34.127 回答
1

您可以使用CLLocationManager获取当前的纬度和经度。创建一个iVarCLLocationManager使用它。

这是代码:

- (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];
}
于 2013-01-25T07:31:28.697 回答
0
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
CLLocation* currentLocation = [locationManager location];

NSString *loc = [NSString stringWithFormat:@"%f,%f",currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
NSLog(@"location:%@", loc);
于 2013-01-25T07:51:17.270 回答
0

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

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

于 2013-01-25T13:30:08.007 回答
0

使用斯威夫特

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是一个可选的,因此应该安全地展开。

于 2015-10-28T09:23:57.927 回答