gpt4 book ai didi

objective-c - MKMapView 居中并将 View 缩放到注释,

转载 作者:搜寻专家 更新时间:2023-10-30 19:52:00 28 4
gpt4 key购买 nike

 MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
MKPointAnnotation *annotation1 = [[MKPointAnnotation alloc] init];
MKPointAnnotation *annotation2 = [[MKPointAnnotation alloc] init];
MKPointAnnotation *annotation3 = [[MKPointAnnotation alloc] init];
MKPointAnnotation *annotation4 = [[MKPointAnnotation alloc] init];
MKPointAnnotation *annotation5 = [[MKPointAnnotation alloc] init];
MKPointAnnotation *annotation6 = [[MKPointAnnotation alloc] init];
annotation.coordinate=CLLocationCoordinate2DMake(40.748736,-73.892523);
annotation.title=@"Head Office";
annotation1.coordinate=CLLocationCoordinate2DMake(40.747972,-73.891858);
annotation1.title=@"Kalpana Chawla";
annotation2.coordinate=CLLocationCoordinate2DMake(40.74768,-73.891818);
annotation2.title=@"New Jackson Heights";
annotation3.coordinate=CLLocationCoordinate2DMake(40.642973,-73.979019);
annotation3.title=@"Brooklyn";
annotation4.coordinate=CLLocationCoordinate2DMake(40.617862,-73.962418);
annotation4.title=@"Coney Islands";
annotation5.coordinate=CLLocationCoordinate2DMake(40.83659,-73.853234);
annotation5.title=@"Bronx";
annotation6.coordinate=CLLocationCoordinate2DMake(40.635336,-73.963204);
annotation6.title=@"Malborough";

[mapView addAnnotation:annotation];
[mapView addAnnotation:annotation1];
[mapView addAnnotation:annotation2];
[mapView addAnnotation:annotation3];
[mapView addAnnotation:annotation4];
[mapView addAnnotation:annotation5];
[mapView addAnnotation:annotation6];
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapView.annotations) {
NSLog(@"%@",annotation);
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
[mapView setVisibleMapRect:zoomRect animated:YES];

我必须在 MK map 上显示这 6 个静态位置。手动声明所有注释后,我使用在 stackoverflow 上找到的 for 循环代码将我的 map 居中并缩放到声明的注释。但是当我运行我的应用程序时, map 为我提供了朝向大西洋的默认缩放级别 View 。请帮忙。我正在模拟器中运行该应用程序

最佳答案

MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapView.annotations) {
NSLog(@"%@",annotation);
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect;
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
}
[mapView setVisibleMapRect:zoomRect animated:YES];

关于objective-c - MKMapView 居中并将 View 缩放到注释,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18652764/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com