如何在iphone上通过从字典中获取纬度和经度将地图视图缩放到注释点
我有一个在地图上放置多个注释的应用程序。但现在我想在删除这些注释时跨越我的地图。这是我在地图上添加注释的代码如何在iphone上通过从字典中获取纬度和经度将地图视图缩放到注释点,iphone,objective-c,Iphone,Objective C,我有一个在地图上放置多个注释的应用程序。但现在我想在删除这些注释时跨越我的地图。这是我在地图上添加注释的代码 - (void)viewDidLoad { [super viewDidLoad]; mapView.mapType = MKMapTypeStandard; mapView.showsUserLocation = YES; NSMutableArray *annotations = [[NSMutableArray alloc]init]; [
- (void)viewDidLoad {
[super viewDidLoad];
mapView.mapType = MKMapTypeStandard;
mapView.showsUserLocation = YES;
NSMutableArray *annotations = [[NSMutableArray alloc]init];
[annotations addObjectsFromArray:appDelegate.maparray];
NSLog(@"%@",annotations);
if ([annotations count])
{
for (int i =0; i < [annotations count]; i++)
{
dict = [annotations objectAtIndex:i];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [[dict objectForKey:@"Latitude"] floatValue];
region.center.longitude = [[dict objectForKey:@"Longitude"] floatValue];
region.span.longitudeDelta = 0.6f;
region.span.latitudeDelta = 0.6f;
Title = [dict objectForKey:@"Title"];
description = [dict objectForKey:@"DescriptionMobile"];
NSLog(@"%@",description);
NSURL *url = [NSURL URLWithString:[dict objectForKey:@"Image"]];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
image = [[UIImage alloc] initWithData:data];
MyAnnotation *ann = [[MyAnnotation alloc] init];
ann.title = Title;
ann.subtitle = description;
[mapView addAnnotation:ann];
}
}
}
-(void)viewDidLoad{
[超级视图下载];
mapView.mapType=MKMapTypeStandard;
mapView.showsUserLocation=是;
NSMutableArray*注释=[[NSMutableArray alloc]init];
[注释addObjectsFromArray:appDelegate.maparray];
NSLog(@“%@”,注释);
如果([注释计数])
{
对于(int i=0;i<[注释计数];i++)
{
dict=[notations objectAtIndex:i];
MKCoordinateRegion={0.0,0.0},{0.0,0.0};
region.center.latitude=[[dict objectForKey:@“latitude”]floatValue];
region.center.longitude=[[dict objectForKey:@“longitude”]floatValue];
region.span.longitudeDelta=0.6f;
region.span.latitudeDelta=0.6f;
Title=[dict objectForKey:@“Title”];
description=[dict objectForKey:@“DescriptionMobile”];
NSLog(@“%@”,说明);
NSURL*url=[NSURL URLWithString:[dict objectForKey:@“Image”];
NSData*data=[[NSData alloc]initWithContentsOfURL:url];
image=[[UIImage alloc]initWithData:data];
MyAnnotation*ann=[[MyAnnotation alloc]init];
ann.title=标题;
ann.subtitle=描述;
[地图视图添加注释:ann];
}
}
}
如何跨越我的地图到我的注释点,以便我的所有注释都可见,并且我的地图也可以缩放。谢谢你试试这段代码,它对我很有用
- (void)zoomToFitMapAnnotations:(MKMapView *)mapView {
if ([mapView.annotations count] == 0) return;
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(Annotation *annotation in mapView.annotations) {
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1;
// Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1;
// Add a little extra space on the sides
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
我想你错过了这句话:
ann.coordinate = region.center;
这在iOS 4和5中工作得非常完美,但在iOS 6中会出现崩溃。只要检查一下我的线程就可以了。很好,如果你能帮忙的话。