【发布时间】:2011-07-21 09:22:54
【问题描述】:
我有兴趣获取我的用户当前的纬度和经度坐标,并在视图上的 UILabel 中将它们按字面意思显示为 NSSString。
我不需要任何 MKMapView 或以图形方式显示任何内容,只是为了在 UILabel 中显示坐标。这可能吗?
谁能给我一个起点?
谢谢
【问题讨论】:
标签: iphone objective-c xcode mkmapview
我有兴趣获取我的用户当前的纬度和经度坐标,并在视图上的 UILabel 中将它们按字面意思显示为 NSSString。
我不需要任何 MKMapView 或以图形方式显示任何内容,只是为了在 UILabel 中显示坐标。这可能吗?
谁能给我一个起点?
谢谢
【问题讨论】:
标签: iphone objective-c xcode mkmapview
是的,有可能。只需导入#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];
}
【讨论】:
NSLog(@"\t %@",s);检查字符串weather是否有值
CoreLocation 框架可以完成这项工作。您可以通过实现此委托来获取用户的当前位置。
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
【讨论】:
按照步骤:
将 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;。祝你好运。
【讨论】: