0

IPhone のデバイスの現在の緯度と経度を取得したいアプリを開発しています。GPS を使用せずにこの情報を取得することは可能ですか。

ありがとう

4

5 に答える 5

3

あなたが使用することができます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;
}
于 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

スウィフトの使用

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 に答える
0

デバイスに WiFi またはセルラー データ サービスがある場合は、CLLocationManager を使用して、GPS を使用せずに現在地を取得できます。位置は、GPS から得られるほど正確ではありませんが、GPS よりも速く結果を得ることができます。

desiredAccuracy プロパティを kCLLocationAccuracyKilometer に設定することにより、CLLocationManager から非 GPS 位置を要求できます。

于 2013-01-25T13:30:08.007 に答える
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 に答える