检索谷歌纵横和经度CoOrdinates,以UILabel显示

时间:2011-07-21 09:22:55

标签: iphone objective-c xcode mkmapview

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

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

有人可以为我提供一个起跑线吗?

由于

3 个答案:

答案 0 :(得分:1)

雅,这可能。只需导入#import <CoreLocation/CoreLocation.h>并声明代理<CLLocationManagerDelegate>即可。然后你可以在下面的委托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];
}

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

按照以下步骤操作:

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

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

  • 使用委托作为@interface yourInterface:UIViewController&lt; MKMapViewDelegate,CLLocationManagerDelegate&gt;

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