如何获取标注的MKPlacemark地址

时间:2015-03-09 11:11:29

标签: objective-c ios8 mapkit mkmapitem mkplacemark

我正在试图找出如何获取MKPlacemark项目的街道地址。我在控制台中打印了一个项目,我可以看到那里的信息,但我只收到thoroughfare信息而没有街道地址编号。

这是我的代码:

- (void)performSearch {
    MKLocalSearchRequest *request = [[MKLocalSearchRequest alloc]init];
    request.naturalLanguageQuery = _searchText.text;
    request.region = _mapView.region;

    _matchingItems = [[NSMutableArray alloc]init];

    MKLocalSearch *search = [[MKLocalSearch alloc]initWithRequest:request];
    NSLog(@"MKLocalSearch array created");

    [search startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
        if (response.mapItems.count == 0) {
            NSLog(@"No Matches Found");
        } else {
            for (MKMapItem *item in response.mapItems) {
                [_matchingItems addObject:item];
                MKPointAnnotation *annotation = [[MKPointAnnotation alloc]init];
                annotation.coordinate = item.placemark.coordinate;

                // Pull out address info from MKMapItem
                MKPlacemark *placemark = item.placemark;
                NSLog(@"Placemark info: %@", item.placemark);
                // Address details
                NSDictionary *address = placemark.addressDictionary;
                NSString *titleString = @"";
                NSString *subtitleString = @"";
                NSString *name = @"";
                NSString *thoroughfare = @"";
                NSString *state = @"";
                NSString *city = @"";
                NSString *country = @"";

                name = [address objectForKey:@"Name"] ? [address objectForKey:@"Name"] : @"";
                thoroughfare = [address objectForKey:@"Thoroughfare"] ? [address objectForKey:@"Thoroughfare"] : @"";
                state = [address objectForKey:@"State"] ? [address objectForKey:@"State"] : @"";
                city = [address objectForKey:@"City"] ? [address objectForKey:@"City"] : @"";
                country = [address objectForKey:@"Country"] ? [address objectForKey:@"Country"] : @"";

                titleString = [NSString stringWithFormat:@"%@ %@", name, thoroughfare];
                subtitleString = [NSString stringWithFormat:@"%@ %@ %@ %@", thoroughfare, state, city, country];

                // Strings for annotation
                annotation.title = item.name;
                annotation.subtitle = subtitleString;

                [_mapView addAnnotation:annotation];
            }
        }
    }];
}

2 个答案:

答案 0 :(得分:2)

您可以找到这样的地址:

        geocoder.reverseGeocodeLocation(location, completionHandler: {(placemarks, error)->Void in
        var placemark:CLPlacemark!

        if error == nil && placemarks!.count > 0 {
            placemark = placemarks![0] as CLPlacemark


            var addressString : String = ""
            if placemark.ISOcountryCode == "TW" /*Address Format in Chinese*/ {
                if placemark.country != nil {
                    addressString = placemark.country!
                }
                if placemark.subAdministrativeArea != nil {
                    addressString = addressString + placemark.subAdministrativeArea! + ", "
                }
                if placemark.postalCode != nil {
                    addressString = addressString + placemark.postalCode! + " "
                }
                if placemark.locality != nil {
                    addressString = addressString + placemark.locality!
                }
                if placemark.thoroughfare != nil {
                    addressString = addressString + placemark.thoroughfare!
                }
                if placemark.subThoroughfare != nil {
                    addressString = addressString + placemark.subThoroughfare!
                }
            } else {
                if placemark.subThoroughfare != nil {
                    addressString = placemark.subThoroughfare! + " "
                }
                if placemark.thoroughfare != nil {
                    addressString = addressString + placemark.thoroughfare! + ", "
                }
                if placemark.postalCode != nil {
                    addressString = addressString + placemark.postalCode! + " "
                }
                if placemark.locality != nil {
                    addressString = addressString + placemark.locality! + ", "
                }
                if placemark.administrativeArea != nil {
                    addressString = addressString + placemark.administrativeArea! + " "
                }
                if placemark.country != nil {
                    addressString = addressString + placemark.country!
                }

                print (placemark.postalCode)

                let new_placemark: MKPlacemark = MKPlacemark (placemark: placemark)

            print(placemark.description)    

            }


        }
    })

答案 1 :(得分:0)

MKPlacemarkCLPlacemark的子类。

CLPlacemark为每个地址元素定义了方便的属性,因此您不必通过键名直接访问字典。 (如果您必须直接访问字典,请尝试使用预定义的ABPerson Address Property key name constants documented here。)

使用便捷的属性访问者,街道号应该在placemark.subThoroughfare

但请注意,并非所有地址元素都可以保证根据给定坐标的准确性和国家而设置。