【问题标题】:Retrieving Latitude & Longitude CoOrdinates, Displaying in UILabel检索经纬度坐标,在 UILabel 中显示
【发布时间】:2011-07-21 09:22:54
【问题描述】:

我有兴趣获取我的用户当前的纬度和经度坐标,并在视图上的 UILabel 中将它们按字面意思显示为 NSSString。

我不需要任何 MKMapView 或以图形方式显示任何内容,只是为了在 UILabel 中显示坐标。这可能吗?

谁能给我一个起点?

谢谢

【问题讨论】:

    标签: iphone objective-c xcode mkmapview


    【解决方案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];
    }
    

    【讨论】:

    • 谢谢,今晚我会努力实现的。我假设我可以然后将 NSString 分配给带有 location.text = s 的 UILabel; ?
    • 够了。 NSString 的纬度和经度值由 coma(,) 分隔。
    • 由于某种原因,我无法让它显示在 UILabel 中。它编译没有问题,但它只是不填充 UILabel
    • 使用NSLog(@"\t %@",s);检查字符串weather是否有值
    【解决方案2】:

    CoreLocation 框架可以完成这项工作。您可以通过实现此委托来获取用户的当前位置。

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

    【讨论】:

      【解决方案3】:

      按照步骤:

      • 将 MapKit.framework 添加到您的项目中

      • 添加到 .h #import "CoreLocation/CoreLocation.h" 和 #import "MapKit/MapKit.h"

      • 使用代理,@interface yourInterface : UIViewController

      现在将以下方法添加到您的 .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;。祝你好运。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-31
        • 2010-09-07
        • 2013-02-14
        • 1970-01-01
        • 2019-05-31
        相关资源
        最近更新 更多