MKMapView 中的所有 Annotation 对用户不可见
All Annotation is not visible to user in MKMapView
我正在使用 MKMapView
并在地图上添加了一些注释图钉。所有注释都属于不同的国家,所以我无法设置所有注释对用户可见。
请尝试使用以下经纬度进行注释。
First annotation Latitude:
23.029690
First annotation Longitude:
72.527359
Second annotation Latitude:
34.210855
Second annotation Longitude:
-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
MyCustomAnnotation.m
#import "MyCustomAnnotation.h"
@implementation MyCustomAnnotation
#pragma mark - Custom Annotation View Setup
- (MKAnnotationView *)annotationView{
MKAnnotationView *annotationView = [[MKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:K_ID_CUSTOM_ANNOTATION];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = kImage(_strImageName);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = CGRectMake(0, 0, 23, 23);
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setImage:[[UIImage imageNamed:@"NextArrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = button;
return annotationView;
}
- (id)initWithTitle:(NSString *)newTitle Subtitle:(NSString *)subTitle andDescritpion:(NSString *)titleDescritpion withLocation:(CLLocationCoordinate2D)location andImageName:(NSString*) strImageName andTagValue:(int) intTag{
self = [super init];
if(self){
_title = newTitle;
_subTitle = subTitle;
_titleDescritpion = titleDescritpion;
_coordinate = location;
if ([strImageName intValue] == 1) {
_strImageName = @"MapViewPinRed";
}else if([strImageName intValue] == 2){
_strImageName = @"MapViewPinOrange";
}else if ([strImageName intValue] == 3){
_strImageName = @"MapViewPinYellow";
}else if ([strImageName intValue] == 4){
_strImageName = @"MapViewPinLightGreen";
}else{
_strImageName = @"MapViewPinGreen";
}
}
return self;
}
@end
我尝试了以下解决方案,但它不起作用。
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];
2) 使用 showAnnotations
[mapViewPledgeList showAnnotations:mapViewPledgeList.annotations animated:YES];
问题:- 建议任何解决方案,以便所有注释对用户可见。
别忘了给地图添加注释。也许这可能是问题
[self.mapView addAnnotation:point];
您需要告诉 mapView
region
类型 MKCoordinateRegion
的正确值。在告诉 mapView
显示正确的 region
.
之前,您需要做一些计算
一个。通过遍历所有可用坐标数组找出以下变量的值。
CLLocationDegrees minLatitude = // To be calculated
CLLocationDegrees maxLatitude = // To be calculated
CLLocationDegrees minLongitude = // To be calculated
CLLocationDegrees maxLongitude = // To be calculated
乙。计算地图的中心坐标。
CLLocationCoordinate2D center =
CLLocationCoordinate2DMake((maxLatitude+minLatitude)/2, (maxLongitude+minLongitude)/2);
摄氏度。计算 latitudeDelta
和 longitudeDelta
值 -
CLLocationDegrees latitudeDelta = ABS(maxLatitude-minLatitude)*2;
CLLocationDegrees longitudeDelta = ABS(maxLongitude-minLongitude)*2;
D.计算span
MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
E.计算 region
并告诉你的 mapView
显示这个 -
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:region animated:YES];
There are a few more considerations while doing this like how much max/min latitudeDelta
and longitudeDelta
you wish to support. That can be applied to Step C when you are calculating the values.
希望这对您有所帮助。
您需要通知 mapview 完善区域,以便所有注释图钉可见。
下面的 swift 代码可以帮助您。
extension MKMapView {
func getAllAnnotationExpectCurrntLocation() -> [MKAnnotation] {
var arrAnnotion: [MKAnnotation] = self.annotations
arrAnnotion = arrAnnotion.filter { [=10=] !== 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 最小限制。
另外在苹果官方地图应用中下面的点同时不可见
First annotation Latitude:
23.029690
First annotation Longitude:
72.527359
Second annotation Latitude:
34.210855
Second annotation Longitude:
-118.622636
注意:超过最小限制后我们无法放大地图视图。
我正在使用 MKMapView
并在地图上添加了一些注释图钉。所有注释都属于不同的国家,所以我无法设置所有注释对用户可见。
请尝试使用以下经纬度进行注释。
First annotation Latitude:
23.029690
First annotation Longitude:
72.527359
Second annotation Latitude:
34.210855
Second annotation Longitude:
-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
MyCustomAnnotation.m
#import "MyCustomAnnotation.h"
@implementation MyCustomAnnotation
#pragma mark - Custom Annotation View Setup
- (MKAnnotationView *)annotationView{
MKAnnotationView *annotationView = [[MKAnnotationView alloc]initWithAnnotation:self reuseIdentifier:K_ID_CUSTOM_ANNOTATION];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = kImage(_strImageName);
UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
button.frame = CGRectMake(0, 0, 23, 23);
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[button setImage:[[UIImage imageNamed:@"NextArrow"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
annotationView.rightCalloutAccessoryView = button;
return annotationView;
}
- (id)initWithTitle:(NSString *)newTitle Subtitle:(NSString *)subTitle andDescritpion:(NSString *)titleDescritpion withLocation:(CLLocationCoordinate2D)location andImageName:(NSString*) strImageName andTagValue:(int) intTag{
self = [super init];
if(self){
_title = newTitle;
_subTitle = subTitle;
_titleDescritpion = titleDescritpion;
_coordinate = location;
if ([strImageName intValue] == 1) {
_strImageName = @"MapViewPinRed";
}else if([strImageName intValue] == 2){
_strImageName = @"MapViewPinOrange";
}else if ([strImageName intValue] == 3){
_strImageName = @"MapViewPinYellow";
}else if ([strImageName intValue] == 4){
_strImageName = @"MapViewPinLightGreen";
}else{
_strImageName = @"MapViewPinGreen";
}
}
return self;
}
@end
我尝试了以下解决方案,但它不起作用。
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];
2) 使用 showAnnotations
[mapViewPledgeList showAnnotations:mapViewPledgeList.annotations animated:YES];
问题:- 建议任何解决方案,以便所有注释对用户可见。
别忘了给地图添加注释。也许这可能是问题
[self.mapView addAnnotation:point];
您需要告诉 mapView
region
类型 MKCoordinateRegion
的正确值。在告诉 mapView
显示正确的 region
.
一个。通过遍历所有可用坐标数组找出以下变量的值。
CLLocationDegrees minLatitude = // To be calculated
CLLocationDegrees maxLatitude = // To be calculated
CLLocationDegrees minLongitude = // To be calculated
CLLocationDegrees maxLongitude = // To be calculated
乙。计算地图的中心坐标。
CLLocationCoordinate2D center =
CLLocationCoordinate2DMake((maxLatitude+minLatitude)/2, (maxLongitude+minLongitude)/2);
摄氏度。计算 latitudeDelta
和 longitudeDelta
值 -
CLLocationDegrees latitudeDelta = ABS(maxLatitude-minLatitude)*2;
CLLocationDegrees longitudeDelta = ABS(maxLongitude-minLongitude)*2;
D.计算span
MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
E.计算 region
并告诉你的 mapView
显示这个 -
MKCoordinateRegion region = MKCoordinateRegionMake(center, span);
[mapView setRegion:region animated:YES];
There are a few more considerations while doing this like how much max/min
latitudeDelta
andlongitudeDelta
you wish to support. That can be applied to Step C when you are calculating the values.
希望这对您有所帮助。
您需要通知 mapview 完善区域,以便所有注释图钉可见。
下面的 swift 代码可以帮助您。
extension MKMapView {
func getAllAnnotationExpectCurrntLocation() -> [MKAnnotation] {
var arrAnnotion: [MKAnnotation] = self.annotations
arrAnnotion = arrAnnotion.filter { [=10=] !== 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 最小限制。
另外在苹果官方地图应用中下面的点同时不可见
First annotation Latitude:
23.029690First annotation Longitude:
72.527359Second annotation Latitude:
34.210855Second annotation Longitude:
-118.622636
注意:超过最小限制后我们无法放大地图视图。