【问题标题】:Setting street address as the subtitle in an annotation将街道地址设置为注释中的副标题
【发布时间】:2011-01-02 09:24:15
【问题描述】:

嘿!基本上我想为我的注释反向地理编码我的坐标并将地址显示为我存储的注释(StorePosition)的副标题。我只是不知道该怎么做......我做错了什么?

编辑; .m

    -(NSString *)subtitle{          
 return subtitle;

    - (void)setCoordinate:(CLLocationCoordinate2D)c {
        MKReverseGeocoder *rvg = [[MKReverseGeocoder alloc] initWithCoordinate:c];
        rvg.delegate = self;
        [rvg start];
        coordinate = c;
    }

    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
        self.subtitle = [placemark.addressDictionary valueForKey:@"Street"]; 
    } 

【问题讨论】:

    标签: iphone addressbook cllocationmanager mkannotationview mkreversegeocoder


    【解决方案1】:

    //EDIT2:也许这可以解决您的问题,实现您自己的坐标设置器并在那里请求您的反向地理编码器:

    -(NSString *)subtitle{
        if (!subtitle) {
            return @"No Street";
        } else {
            return subtitle;
        }
    }
    
    -(NSString *)title{
        return @"Saved position";
    }
    
    - (void)setCoordinate:(CLLocationCoordinate2D)c {
        MKReverseGeocoder *rvg = [[MKReverseGeocoder alloc] initWithCoordinate:c];
        rvg.delegate = self;
        [rvg start];
        coordinate = c;
    }
    
    
    -(id)initWithCoordinate:(CLLocationCoordinate2D) coor{
        self.coordinate=coor;
        NSLog(@"%f,%f",coor.latitude,coor.longitude);
        return self;
    }
    
    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error {
        NSLog(@"fail %@", error);
    }
    
    - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark {
        self.subtitle = [placemark.addressDictionary valueForKey:@"Street"];
    }
    

    // 原文:
    首先让我们谈谈你的错误:D

    return [NSString stringWithFormat:@"%f", streetAddress];
    

    %f 用于浮点值,因此不会显示字符串。
    第二件事,您的变量streetAddress 此时不存在,您应该通过将其写入头文件来使其可访问。

    然后在initWithCoordinate: 你调用return self; 这条线之后的所有东西都不会被调用。所以你的ReverseGeocoder 永远不会开始。

    NSString *streetAddress = …
    

    在这里你想保存你的地址,但你只把它写到一个只存在于这个方法中的变量中。方法完成后,此变量将被释放。如果你在头文件中定义了它,你可以把它保存到你的类范围变量中。

    现在让我们谈谈如何做到这一点。我要做的是将反向地理编码器从您的注释中取出并将其放入您的 mapView 控制器中。然后,如果您获得了新位置,则可以更改注释的副标题。

    [[mapView.annotations objectAtIndex:0] setSubtitle:@"Address Information"];
    

    //编辑:您可以登录您的didFailWith,您会看到他无法申请该职位。即使你把你的 reverseGeocoder-init 放在 return 上面,它仍然没有足够的时间来请求位置。

    【讨论】:

    • 请看一下编辑! :D 我不明白我应该如何为 StorePosition 做这件事!
    • self.coordinate=coor;如果它是只读的,则将不起作用,因此要么删除 self(但不会对 init 位置进行地理编码),要么在 @implementation 上方的 @interface 中使用读写覆盖它(在您的 .m 文件中)
    • 我得到两个构建错误:“'subtitle' undeclared (first use in this function)”和“object cannot be set - readonly property or no setter found”...我是一个真正的新手。 .请帮助z
    • 在你的头文件中:@property (nonatomic, copy) NSString *subtitle; @property (nonatomic, readwrite) CLLocationCoordinate2D 坐标;
    • 潜在泄漏:" MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:koor]; geocoder.delegate = self; coordinate = koor;"
    猜你喜欢
    • 2014-06-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多