如何在iphone应用程序中获取当前位置?

时间:2012-10-06 06:05:44

标签: xcode cllocationmanager mkreversegeocoder

我得到了经度和纬度值,并将其保存在标签中,并且很容易在我的应用程序中显示,但我想要当前的位置详细信息,主要是地点的名称。
我用了MKReverseGeocoder。我做了这个代码

- (void)locationUpdate:(CLLocation *)location
{
    CLLocationCoordinate2D coord;
    coord.longitude=[NSString stringWithFormat:@"LATITUDE: %f", location.coordinate.latitude];

    MKReverseGeocoder *geocoder=[[MKReverseGeocoder alloc]initWithCoordinate:coord];
    [geocoder setDelegate:self];
    [geocoder  start];



    latlbl.text = [NSString stringWithFormat:@"LATITUDE: %f", location.coordinate.latitude];
    lonlbl.text = [NSString stringWithFormat:@"LONGITUDE: %f", location.coordinate.longitude];
    //txtlocate.text = [location description];

}
![-(void)reverseGeocoder:(MKReverseGeocoder)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
NSLog(@"%@",\[placemark addressDictionary\]
     }

enter image description here 这就是我得到的错误...... 提前谢谢

1 个答案:

答案 0 :(得分:1)

错误很明显:

  

从不兼容的类型'id'分配'CLLocationCoordinate2D'(又名'double')

coordCLLocationCoordinate2D类型:

typedef struct {
    CLLocationDegrees latitude;
    CLLocationDegrees longitude;
} CLLocationCoordinate2D;

latitudelongitude是CLLocationDegrees:

typedef double CLLocationDegrees;

因此,您无法将NSString(它是id类型)对象分配给coord.longitudecoord.latitude


编辑:

你应该这样做:

coord.longitude = location.coordinate.latitude;