2013-12-17 82 views
2

目前我可以在地圖上放置引腳。現在我想讓註解標題顯示引腳被丟棄的地址。當引腳在地圖上丟棄時,在註釋中顯示地址

我看了一下這一點,但不能讓我的工作:
Set annotation's title as current address

代碼在我ViewController.m

更新。

- (void)addPinToMap:(UIGestureRecognizer *)gestureRecognizer 
{ 
    if (gestureRecognizer.state != UIGestureRecognizerStateBegan) 
     return; 

    CGPoint touchPoint = [gestureRecognizer locationInView:self.map]; 
    CLLocationCoordinate2D touchMapCoordinate = 
    [self.map convertPoint:touchPoint toCoordinateFromView:self.map]; 

    CLLocation *currentLocation = [[CLLocation alloc] 
            initWithLatitude:touchMapCoordinate.latitude 
            longitude:touchMapCoordinate.longitude]; 

    [self.geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemark, NSError *error) { 

     //initialize the title to "unknown" in case geocode has failed... 
     NSString *annTitle = @"Address unknown"; 

     //set the title if we got any placemarks... 
     if (placemark.count > 0) 
     { 
      CLPlacemark *topResult = [placemark objectAtIndex:0]; 
      annTitle = [NSString stringWithFormat:@"%@ %@ %@ %@", topResult.country, topResult.locality, topResult.subLocality, topResult.thoroughfare]; 
     } 

     //now create the annotation... 
     MapAnnotation *toAdd = [[MapAnnotation alloc]init]; 

     toAdd.coordinate = touchMapCoordinate; 
     toAdd.title = annTitle; 
     //toAdd.title = @"Title"; 
     toAdd.subtitle = @"Subtitle"; 

     [self.map addAnnotation:toAdd]; 
    }]; 
} 
+0

我目前在嘗試做同樣的事情的過程! – logixologist

回答

2

首先,在addPinToMap:方法,addressLocation調用與currentLocationcurrentLocation永遠不會設置。它宣佈了幾行,但沒有設置任何值。

因此改變:

CLLocation *currentLocation; 

到:

CLLocation *currentLocation = [[CLLocation alloc] 
           initWithLatitude:touchMapCoordinate.latitude 
           longitude:touchMapCoordinate.longitude]; 


,甚至與此修復程序,它仍然會無法正常工作。註釋的title不會被設置,因爲reverseGeocodeLocation方法的完成處理程序塊將在註釋添加完成後完成(該塊是異步的 - addPinToMap:中的代碼不會等待它完成)。

當您確實擁有地理編碼器結果(無論成功還是失敗)時,您需要稍微更改代碼並在內添加批註

移動reverseGeocodeLocation調用addPinToMap:方法:

CLLocation *currentLocation = [[CLLocation alloc] 
           initWithLatitude:touchMapCoordinate.latitude 
           longitude:touchMapCoordinate.longitude]; 

[self.geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemark, NSError *error) { 

    //initialize the title to "unknown" in case geocode has failed... 
    NSString *annTitle = @"Address unknown"; 

    //set the title if we got any placemarks... 
    if (placemark.count > 0) 
    { 
     CLPlacemark *topResult = [placemark objectAtIndex:0]; 
     annTitle = [NSString stringWithFormat:@"%@ %@ %@ %@", topResult.country, topResult.locality, topResult.subLocality, topResult.thoroughfare]; 
    } 

    //now create the annotation... 
    MapAnnotation *toAdd = [[MapAnnotation alloc]init]; 

    toAdd.coordinate = touchMapCoordinate; 
    toAdd.title = annTitle; 
    toAdd.subtitle = @"Subtitle"; 

    [self.map addAnnotation:toAdd]; 
}]; 
+0

這太好了,但我仍然無法顯示引腳,如果我阻止'reverseGeocodeLocation'調用引腳與普通標題和字幕註釋一樣丟失,移動代碼無助於我。有什麼建議麼? – Ernie

+0

將convertPoint行後面的代碼顯示在addPinToMap方法的答案中,對吧?請將您的更新代碼添加到問題中。 – Anna

+0

請參閱更新。 – Ernie