2011-07-21 9 views
-1

現在の緯度と経度の座標をユーザが取得し、文字通りそのビューのUILabelにNSSStringとして表示することに興味があります。UILabelに表示する緯度と経度の座標を取得する

UILabelに座標を表示するために、MKMapViewやグラフィックを表示する必要はありません。これは可能ですか?

誰も私のための開始ブロックを提供できますか?

おかげ

答えて

0

この仕事をしていませんCoreLocationフレームワークがあります。このデリゲートを実装することにより、ユーザーの現在地を取得できます。

- (void)locationManager:(CLLocationManager *)manager 
didUpdateToLocation:(CLLocation *)newLocation 
     fromLocation:(CLLocation *)oldLocation 
1

可能です。 #import <CoreLocation/CoreLocation.h>をインポートし、デリゲート<CLLocationManagerDelegate>を宣言してください。次のdelegate mathodで値を取得できます。

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ 

    CLLocationCoordinate2D location=newLocation.coordinate; 
    NSString *s = [NSString stringWithFormat:@"%f,%f",location.latitude,location.longitude]; 
} 
+0

ありがとう、私は今晩それを試して実装します。私はちょうどNSStringをUILabelにlocation.text = sで割り当てることができると仮定しています。 ? –

+0

それは十分です。 NSStringは緯度と経度の値が昏睡(、)で区切られています。 – Sisu

+0

何らかの理由で私はそれをUILabelに表示することができません。それは問題なくコンパイルされますが、UILabelには値が入りません。 –

0

の手順に従ってください。

  • は#importを "CoreLocation/CoreLocation.h" との#importを.Hに追加「MapKit/MapKitプロジェクトに

  • をMapKit.frameworkを追加.h "

  • 代理人を@interface yourInterface:UIViewControllerとして使用する< MKMapViewDelegate、CLLocationManagerDelegate>

    今ここにあなたの.mファイルに

    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    { 
    [self setMapCenter:newLocation.coordinate]; 
    [self._mapView selectAnnotation:[[self._mapView annotations] lastObject] animated:YES]; 
    lblLong.text = [nsstring stringWithFormat:@"%f", newLocation.coordinate.longitude]; 
    lblLat = [nsstring stringWithFormat:@"%f", newLocation.coordinate.latitude]; 
    [self.locationManager stopUpdatingLocation]; 
    } 
    
    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
    { 
    NSLog(@"ERROR"); 
    } 
    

    を次のメソッドを追加します

CLLocationManager *locationManager;に言及。がんばろう。

関連する問題