2011-07-21 25 views
-1

我有興趣抓住我的用戶當前的緯度和經度座標,並將它們字面顯示爲視圖上UILabel中的NSSString。正在檢索經緯度CoOrdinates,顯示在UILabel中

我不需要任何MKMapView或以圖形方式顯示任何內容,只需在UILabel中顯示座標即可。這可能嗎?

任何人都可以爲我提供一個出發臺嗎?

感謝

回答

0

還有就是CoreLocation框架,做這件工作。您可以通過實施此代理來獲取用戶的當前位置。

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

雅,它的可能。只需導入#import <CoreLocation/CoreLocation.h>並聲明代表<CLLocationManagerDelegate>。那麼你可以在下面的委託方法中獲取值。

- (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

以下步驟:

  • 添加MapKit.framework到項目

  • 加入的.h#進口 「CoreLocation/CoreLocation.h」 和#進口「MapKit/MapKit .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;。祝你好運。