Ios 用户在MKMapView中看不到所有注释
我正在使用Ios 用户在MKMapView中看不到所有注释,ios,objective-c,annotations,mkmapview,mapkit,Ios,Objective C,Annotations,Mkmapview,Mapkit,我正在使用MKMapView,并在地图上添加了许多注释插脚。所有注释都属于不同的国家,所以我无法将所有注释设置为用户可见 请尝试使用以下纬度和经度进行注释 第一个注释纬度:23.029690 第一个注释经度:72.527359 Second注解纬度:34.210855 第二个注释经度:-118.622636 将注释添加到地图视图的代码。 CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([[dicObjects valu
MKMapView
,并在地图上添加了许多注释插脚。所有注释都属于不同的国家,所以我无法将所有注释设置为用户可见
请尝试使用以下纬度和经度进行注释
第一个注释纬度:
23.029690
第一个注释经度:
72.527359
Second注解纬度:
34.210855
第二个注释经度:
-118.622636
将注释添加到地图视图的代码。
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([[dicObjects valueForKey:K_Latitude] doubleValue], [[dicObjects valueForKey:K_Longitude] doubleValue]);
MyCustomAnnotation *myCustomAnnotation = [[MyCustomAnnotation alloc]initWithTitle:[NSString stringWithFormat:@"%@ ",[dicObjects valueForKey:K_Title]] Subtitle:[NSString stringWithFormat:@"%@",[dicObjects valueForKey:K_SubTitle]] andDescritpion:[NSString stringWithFormat:@"%@",[dicObjects valueForKey:K_TitelDes]] withLocation:coordinate andImageName:[dicObjects valueForKey:K_Rating] andTagValue:intTempCounter];
[mapViewPledgeList addAnnotation:myCustomAnnotation];
MyCustomAnnotation.h
@interface MyCustomAnnotation : NSObject<MKAnnotation>
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (copy, nonatomic) NSString *title;
@property (copy, nonatomic) NSString *subTitle;
@property (copy, nonatomic) NSString *titleDescritpion;
@property (copy, nonatomic) NSString *strImageName;
@property int intTagValue;
- (id)initWithTitle:(NSString *)newTitle Subtitle:(NSString *)subTitle andDescritpion:(NSString *)titleDescritpion withLocation:(CLLocationCoordinate2D)location andTagValue:(int) intTag;
- (MKAnnotationView *)annotationView;
- (id)initWithTitle:(NSString *)newTitle Subtitle:(NSString *)subTitle andDescritpion:(NSString *)titleDescritpion withLocation:(CLLocationCoordinate2D)location andImageName:(NSString*) strImageName andTagValue:(int) intTag;
@end
我尝试了下面的解决方案,但它不起作用。
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([[dicObjects valueForKey:K_Latitude] doubleValue], [[dicObjects valueForKey:K_Longitude] doubleValue]);
MyCustomAnnotation *myCustomAnnotation = [[MyCustomAnnotation alloc]initWithTitle:[NSString stringWithFormat:@"%@ ",[dicObjects valueForKey:K_Title]] Subtitle:[NSString stringWithFormat:@"%@",[dicObjects valueForKey:K_SubTitle]] andDescritpion:[NSString stringWithFormat:@"%@",[dicObjects valueForKey:K_TitelDes]] withLocation:coordinate andImageName:[dicObjects valueForKey:K_Rating] andTagValue:intTempCounter];
[mapViewPledgeList addAnnotation:myCustomAnnotation];
1) 使用MKMapRectUnion
MKMapRect zoomRect = MKMapRectNull;
for (id <MKAnnotation> annotation in mapViewPledgeList.annotations)
{
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.1, 0.1);
zoomRect = MKMapRectUnion(zoomRect, pointRect);
}
[mapViewPledgeList setVisibleMapRect:zoomRect animated:YES];
问题:-建议任何解决方案,以便所有注释对用户可见。不要忘记向地图添加注释。也许这就是问题所在
[self.mapView addAnnotation:point];
您需要告诉
mapView
类型为MKCoordinateRegion
的区域的正确值。在告诉mapView
显示正确的区域之前,您需要进行一些计算
A.通过删除所有可用的坐标数组,找出以下变量的值
CLLocationDegrees minLatitude = // To be calculated
CLLocationDegrees maxLatitude = // To be calculated
CLLocationDegrees minLongitude = // To be calculated
CLLocationDegrees maxLongitude = // To be calculated
B.计算地图的中心坐标
CLLocationCoordinate2D center =
CLLocationCoordinate2DMake((maxLatitude+minLatitude)/2, (maxLongitude+minLongitude)/2);
C.计算latitudelta
和longitudeDelta
值-
CLLocationDegrees latitudeDelta = ABS(maxLatitude-minLatitude)*2;
CLLocationDegrees longitudeDelta = ABS(maxLongitude-minLongitude)*2;
D.计算span
MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
E.计算区域
,并告诉您的地图视图
显示该区域-
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:region animated:YES];
执行此操作时还有一些其他注意事项,例如希望支持的最大/最小latitudelta
和longitudeDelta
。计算值时,可将其应用于步骤C
希望这有帮助。您需要通知mapview以完善区域,以便所有注释接点都可见
下面的swift代码帮助您
extension MKMapView {
func getAllAnnotationExpectCurrntLocation() -> [MKAnnotation] {
var arrAnnotion: [MKAnnotation] = self.annotations
arrAnnotion = arrAnnotion.filter { $0 !== self.userLocation }
return arrAnnotion
}
func zoomToFitMapAnnotations() {
guard self.annotations.count != 0 else {
return
}
var topLeftCordinate = CLLocationCoordinate2D(latitude: -90, longitude: 180)
var bottomRightCordinate = CLLocationCoordinate2D(latitude: 90, longitude: -180)
for ann in self.getAllAnnotationExpectCurrntLocation() {
topLeftCordinate.longitude = fmin(topLeftCordinate.longitude, ann.coordinate.longitude)
topLeftCordinate.latitude = fmax(topLeftCordinate.latitude, ann.coordinate.latitude)
bottomRightCordinate.longitude = fmax(bottomRightCordinate.longitude, ann.coordinate.longitude)
bottomRightCordinate.latitude = fmin(bottomRightCordinate.latitude, ann.coordinate.latitude)
}
let latitude = topLeftCordinate.latitude - ((topLeftCordinate.latitude - bottomRightCordinate.latitude) * 0.5)
let longitude = topLeftCordinate.longitude + ((bottomRightCordinate.longitude - topLeftCordinate.longitude) * 0.5)
let latitudeDelta = fabs(topLeftCordinate.latitude - bottomRightCordinate.latitude) * 1.1
let longitudeDelta = fabs(bottomRightCordinate.longitude - topLeftCordinate.longitude) * 1.1
let span: MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: latitudeDelta, longitudeDelta: longitudeDelta)
let centerCordinate: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region: MKCoordinateRegion = MKCoordinateRegion(center: centerCordinate, span: span)
let newregion = self.regionThatFits(region)
self.setRegion(newregion, animated: true)
}
}
Q] 所有注释对用户保持可见
- 遗憾的是,无法将最小缩放级别设置为小于MKMapView最小限制
- 同时在苹果官方地图应用程序中,下方的点不可见
第一个注释纬度:
23.029690
第一个注释经度:
72.527359
Second注解纬度:
34.210855
第二个注释经度:
-118.622636
注意:在地图视图达到最小限制后,我们无法放大地图视图。注释已放置在地图中,但所有注释都不同时可见。我误解了问题@Hiteshsuranitell me关于“ZoomTofitPanNotations”的问题,无法修复地图以查看地图上的所有注释。在最小放大级别后,无法放大地图视图
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([[dicObjects valueForKey:K_Latitude] doubleValue], [[dicObjects valueForKey:K_Longitude] doubleValue]);
MyCustomAnnotation *myCustomAnnotation = [[MyCustomAnnotation alloc]initWithTitle:[NSString stringWithFormat:@"%@ ",[dicObjects valueForKey:K_Title]] Subtitle:[NSString stringWithFormat:@"%@",[dicObjects valueForKey:K_SubTitle]] andDescritpion:[NSString stringWithFormat:@"%@",[dicObjects valueForKey:K_TitelDes]] withLocation:coordinate andImageName:[dicObjects valueForKey:K_Rating] andTagValue:intTempCounter];
[mapViewPledgeList addAnnotation:myCustomAnnotation];