我正在开发一个应用程序,我想让设备当前的经纬度和IP经理。是否可以在不使用GPS的情况下获取此信息。
谢谢
答案 0 :(得分: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;
}
答案 1 :(得分:1)
您可以使用CLLocationManager
获取当前的lat和long。
创建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];
}
答案 2 :(得分: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);
答案 3 :(得分:0)
如果设备具有WiFi或蜂窝数据服务,您可以使用CLLocationManager获取没有GPS的当前位置。该位置不会像您从GPS获得的那样准确,但是您获得的结果比从GPS获得更快。
您可以通过将desiredAccuracy属性设置为kCLLocationAccuracyKilometer来从CLLocationManager请求非GPS位置。
答案 4 :(得分:0)
使用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
是可选的,因此应该安全地解包。