将MKMapView更新为从CLGeocoder返回的CLPlacemark [英] Updating MKMapView to CLPlacemark returned from CLGeocoder
问题描述
我希望能够通过允许用户在UIAlertView中输入地址或位置来更新MKMapView上显示的区域。我目前有:
I want to be able to update the region displayed on a MKMapView by allowing the user to type in an address or location in a UIAlertView. I currently have:
if (geocoder.geocoding)
[geocoder cancelGeocode];
[geocoder geocodeAddressString:[[alertView textFieldAtIndex:0] text] completionHandler:^(NSArray *placemarks, NSError *error) {
if (!error) {
NSLog(@"Found a location");
} else {
NSLog(@"Error in geocoding");
}
NSLog(@"Num found: %d", [placemarks count]);
CLPlacemark *placemark = [placemarks objectAtIndex:0];
MKCoordinateRegion region;
region.center.latitude = placemark.region.center.latitude;
region.center.longitude = placemark.region.center.longitude;
MKCoordinateSpan span;
double radius = placemark.region.radius / 1000;
NSLog(@"Radius is %f", radius);
span.latitudeDelta = radius / 112.0;
//span.longitudeDelta = ???
region.span = span;
NSLog(@"Region is %f %f %f", region.center.latitude, region.center.longitude, span.latitudeDelta);
[mapView setRegion:region animated:YES];
}];
我的问题是我不确定如何计算经度增量。
My problem is I am unsure how to calculate the longitude delta.
推荐答案
你可以设置它等于 latitudeDelta
和地图视图将根据需要进行调整。
You could just set it equal to latitudeDelta
and the map view will adjust as needed.
但您首先不需要自己计算跨度。您可以使用:
But you don't need to calculate the span yourself in the first place. You can use:
region = MKCoordinateRegionMakeWithDistance(
placemark.region.center,
placemark.region.radius,
placemark.region.radius);
不确定问题的第二部分。
在iOS 7及更高版本中,
区域
由 CLPlacemark
实际上是 CLCircularRegion
(参见不推荐的CLRegion方法 - 如何获得半径?。
Not sure about the second part of your question.
In iOS 7 and higher, the
region
returned by the CLPlacemark
is actually a CLCircularRegion
(see Deprecated CLRegion methods - how to get radius?).
虽然原始代码仍然存在按原样工作,您可能会收到编译器警告, radius
和 center
已被弃用。
Although the original code will still work as-is, you may get a compiler warning that radius
and center
are deprecated.
为避免出现此警告,请将区域
转换为 CLCircularRegion
:
To avoid the warning, cast the region
as a CLCircularRegion
:
CLCircularRegion *pmCircularRegion = (CLCircularRegion *)placemark.region;
region = MKCoordinateRegionMakeWithDistance(
pmCircularRegion.center,
pmCircularRegion.radius,
pmCircularRegion.radius);
这篇关于将MKMapView更新为从CLGeocoder返回的CLPlacemark的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!