2012-01-25 19 views
5

私は現在位置を取得するには、次のコードを使用しています、私はcorelocationフレームワークにObjective Cの中で、緯度と経度で現在位置を取得する方法を

- (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 
{ 
    int degrees = newLocation.coordinate.latitude; 
    double decimal = fabs(newLocation.coordinate.latitude - degrees); 
    int minutes = decimal * 60; 
    double seconds = decimal * 3600 - minutes * 60; 
    NSString *lat = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
        degrees, minutes, seconds]; 
    NSLog(@" Current Latitude : %@",lat); 
    //latLabel.text = lat; 
    degrees = newLocation.coordinate.longitude; 
    decimal = fabs(newLocation.coordinate.longitude - degrees); 
    minutes = decimal * 60; 
    seconds = decimal * 3600 - minutes * 60; 
    NSString *longt = [NSString stringWithFormat:@"%d° %d' %1.4f\"", 
         degrees, minutes, seconds]; 
    NSLog(@" Current Longitude : %@",longt); 
    //longLabel.text = longt; 
} 

を追加している。しかし、私はつまり、デフォルト値を取得していますLatitude:42°17'36.8898 "、経度:-71°27'43.1003" 現在のLatitude:N 12°56 '11.5624 "と経度:E 77°32' 41.0834"です。正確な緯度と経度を取得する方法。 ここで私は間違いをしているか、他の方法や関数、フレームワークを追加する必要があります。

ありがとうございました。

+1

実際のデバイスまたはシミュレータでテストしましたか? –

+0

端末で位置情報サービスを有効にしましたか? –

+0

@XSlash私はシミュレータでテストしました。 – shasha

答えて

2

実際のデバイスでアプリケーションをテストします。シミュレータは常に緯度と経度のデフォルト値を表示します。また、お使いの端末でロケーションサービスが有効になっていることを確認してください。

+0

シミュレータでさえ、それは問題ではありません。デバッグ領域の内側には、デバッグボタンの隣に表示されている、さまざまな領域を選択できます。いくつかのデフォルトのポジションがあります。またはあなたの上にあなた自身を作成し​​てください! –

1

現在の位置の緯度と経度を取得するには、デバイス上でテストする必要があります。私はあなたがクパチーノの緯度と経度をデフォルト値にしていると推測しています。

2

シミュレータ上の位置シミュレーションを有効にすることはできますが、シミュレータではなくデバイス内で位置サービスをテストする必要があります。画像添付ファイルの丸いアイコンを参照してください。しかし、それはあなたの正確な場所ではありません!

-(CLLocationCoordinate2D) getLocation{ 
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease]; 
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation]; 
    CLLocation *location = [locationManager location]; 
    CLLocationCoordinate2D coordinate = [location coordinate]; 

    return coordinate; 
} 

と使用、それを呼び出すために:しかし、少なくとも、あなたはあなたのコードが Image

+0

xcode 4を使用しています。その上に円のアイコンが表示されています – shasha

+0

あなたのアプリを実行すると、 –

+0

@Xスラッシュ、私のアプリを実行した後にチェックしましたが、 。 – shasha

11

を働いているあなたは、現在の緯度と経度を取得する必要があり、このメソッドを追加できることを確認することができます

CLLocationCoordinate2D coordinate = [self getLocation]; 
NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude]; 
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude]; 

NSLog(@"*dLatitude : %@", latitude); 
NSLog(@"*dLongitude : %@",longitude); 
+1

私はそれらに直接アクセスしようとすると 'coordinate'sは(null)であるという奇妙な振る舞いをしています。印象は、コードに時間がある場合は、座標が渡されます。タイミングの問題のようです。 – jerik

+0

これは、LatitudeとLongitudeで0.000000を返します。私はXcode 6.1.1とiPhone 5sシミュレータを使用しています。なぜそうなのか? –

+0

この回答を投稿していただきありがとうございます。本当に本当に初心者のためのヘルプ。 –

0

まず、Simulatorでロケーションサービスを有効にする必要があります。デバッグ - >場所。ほとんどの場合、あなたはクパチーノの緯度と経度を取得します。また、このオプションのカスタムロケーションを設定するには、2番目のオプション(カスタムロケーション)を選択します。これらのオプションは、デバッグ領域で実行時に場所を選択すると自動的にトリガされます。

+0

ありがとう....私はそれを見つけるために多くの苦労:) – vijeesh

関連する問題