IPhone のデバイスの現在の緯度と経度を取得したいアプリを開発しています。GPS を使用せずにこの情報を取得することは可能ですか。
ありがとう
IPhone のデバイスの現在の緯度と経度を取得したいアプリを開発しています。GPS を使用せずにこの情報を取得することは可能ですか。
ありがとう
あなたが使用することができますCoreLocation framework
Core Location フレームワークには、重要なクラスが 1 つあり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;
}
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];
}
スウィフトの使用
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
これはオプションであるため、安全にアンラップする必要があることに注意してください。
デバイスに WiFi またはセルラー データ サービスがある場合は、CLLocationManager を使用して、GPS を使用せずに現在地を取得できます。位置は、GPS から得られるほど正確ではありませんが、GPS よりも速く結果を得ることができます。
desiredAccuracy プロパティを kCLLocationAccuracyKilometer に設定することにより、CLLocationManager から非 GPS 位置を要求できます。
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
CLLocation* currentLocation = [locationManager location];
NSString *loc = [NSString stringWithFormat:@"%f,%f",currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
NSLog(@"location:%@", loc);