【发布时间】:2014-10-07 22:55:19
【问题描述】:
如果用户允许位置共享,我有一个应用程序可以将用户位置填充到文本字段中。一切正常,但我的问题是如何将结果拆分为多个文本字段。
目前“地址”文本字段显示所有内容。我在该字段中想要的只是街道地址,以及城市、州、邮编等的另一个地址。任何建议将不胜感激!我查看了文档,例如我尝试过的邮政编码
self.zip.text=
NSString *postalCode = [NSString stringWithFormat:@"%@",placemark.postalCode];
但这不起作用
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if(error.code == kCLErrorDenied)
{
self.address.text = @"Location information denied";
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
// Make sure this is a recent location event
CLLocation *newLocation = [locations lastObject];
NSTimeInterval eventInterval = [newLocation.timestamp timeIntervalSinceNow];
if(abs(eventInterval) < 30.0)
{
// Make sure the event is valid
if (newLocation.horizontalAccuracy < 0)
return;
// Instantiate _geoCoder if it has not been already
if (_geocoder == nil)
_geocoder = [[CLGeocoder alloc] init];
//Only one geocoding instance per action
//so stop any previous geocoding actions before starting this one
if([_geocoder isGeocoding])
[_geocoder cancelGeocode];
[_geocoder reverseGeocodeLocation: newLocation
completionHandler: ^(NSArray* placemarks, NSError* error)
{
if([placemarks count] > 0)
{
CLPlacemark *foundPlacemark = [placemarks objectAtIndex:0];
self.address.text =
[NSString stringWithFormat:@"%@",
foundPlacemark.description];
}
else if (error.code == kCLErrorGeocodeCanceled)
{
NSLog(@"Geocoding cancelled");
}
else if (error.code == kCLErrorGeocodeFoundNoResult)
{
self.address.text=@"No geocode result found";
}
else if (error.code == kCLErrorGeocodeFoundPartialResult)
{
self.address.text=@"Partial geocode result";
}
else
{
self.address.text =
[NSString stringWithFormat:@"Unknown error: %@",
error.description];
}
}
];
//Stop updating location until they click the button again
[manager stopUpdatingLocation];
}
}
【问题讨论】:
-
所以在您的代码中,foundPlacemark.description 是您要拆分的字符串的位置?
标签: ios objective-c ipad nsstring geocoding