Iphone mapview 中未显示多个注释

Posted

技术标签:

【中文标题】Iphone mapview 中未显示多个注释【英文标题】:Multiple annotations not showing in Iphone mapview 【发布时间】:2012-12-06 01:46:23 【问题描述】:

我查看了 *** 中与我的问题密切相关的其他问题,但是我的问题有点不同。在这里,代码在 iPad 上运行良好,但在我的 iPhone 上运行良好。我的 iPad 将正确加载所有 300 多个注释,但我的 iPhone 只能加载其中的 80 个。我不确定这是否是由于 iPhone 上的内存量造成的,因为当我在模拟器上测试时它会做同样的事情。您将在下面找到我的示例代码。

@interface MOVmapViewController ()

@end

#define VA_LATITUDE 37.413754;
#define VA_LONGITUDE -79.142246;

//Span
#define THE_SPAN 5.50f;



@implementation MOVmapViewController
@synthesize myMapView;

-(IBAction)findmylocation:(id)sender 

    myMapView.showsUserLocation = YES;
    myMapView.delegate = MKMapTypeStandard;
    [myMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];



-(IBAction)setmaptype:(id)sender 

    switch (((UISegmentedControl *)sender).selectedSegmentIndex) 
        case 0:
            myMapView.mapType = MKMapTypeStandard;
            break;
        case 1:
            myMapView.mapType = MKMapTypeSatellite;
            break;
        case 2:
            myMapView.mapType = MKMapTypeHybrid;
            break;

        default:
            myMapView.mapType = MKMapTypeStandard;
            break;

    




- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view, typiclly from a nib.

    //Create the region
    MKCoordinateRegion myRegion;

    //Center
    CLLocationCoordinate2D center;
    center.latitude = VA_LATITUDE;
    center.longitude = VA_LONGITUDE;

    //Span
    MKCoordinateSpan span;
    span.latitudeDelta = THE_SPAN;
    span.longitudeDelta = THE_SPAN;

    myRegion.center = center;
    myRegion.span = span;


    //Set our mapView
    [myMapView setRegion:myRegion animated:YES];

    //Annotations
    //Abingdon Lodge No. 48
    NSString *abingdon = @"325 W Main Street Abingdon, Virginia 24210";
    CLGeocoder *abingdongeo = [[CLGeocoder alloc] init];
    [abingdongeo geocodeAddressString:abingdon completionHandler:^(NSArray* placemarks, NSError* error)
        if (placemarks && placemarks.count > 0) CLPlacemark *topResult = [placemarks objectAtIndex:0];
            MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
            MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
            point.coordinate = placemark.coordinate;
            point.title = @"Abingdon Lodge No. 48";
            point.subtitle = @"Abingdon, VA";
            [self.myMapView addAnnotation:point]; ];


    //York Lodge No. 12
    NSString *york = @"14411 Black Hollow Road Abingdon, Virginia 24210";
    CLGeocoder *yorkgeo = [[CLGeocoder alloc] init];
    [yorkgeo geocodeAddressString:york completionHandler:^(NSArray* placemarks, NSError* error)
        if (placemarks && placemarks.count > 0) CLPlacemark *topResult = [placemarks objectAtIndex:0];
            MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
            MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
            point.coordinate = placemark.coordinate;
            point.title = @"York Lodge No. 12";
            point.subtitle = @"Abingdon, VA";
            [self.myMapView addAnnotation:point]; ];

    //Alberene Lodge No. 277
    NSString *alberene = @"2722 Plank Road Alberene, Virginia 22959";
    CLGeocoder *alberenegeo = [[CLGeocoder alloc] init];
    [alberenegeo geocodeAddressString:alberene completionHandler:^(NSArray* placemarks, NSError* error)
        if (placemarks && placemarks.count > 0) CLPlacemark *topResult = [placemarks objectAtIndex:0];
            MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
            MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
            point.coordinate = placemark.coordinate;
            point.title = @"Alberene Lodge No. 277";
            point.subtitle = @"Alberene, VA";
            [self.myMapView addAnnotation:point]; ];


    //A. Douglas Smith, Jr. Lodge of Research No. 1949
    NSString *douglas = @"101 Callahan Drive Alexandria, Virginia 22301";
    CLGeocoder *douglasgeo = [[CLGeocoder alloc] init];
    [douglasgeo geocodeAddressString:douglas completionHandler:^(NSArray* placemarks, NSError* error)
        if (placemarks && placemarks.count > 0) CLPlacemark *topResult = [placemarks objectAtIndex:0];
            MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
            MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
            point.coordinate = placemark.coordinate;
            point.title = @"A. Douglas Smith, Jr. Lodge of Research No. 1949";
            point.subtitle = @"Alexandria, VA";
            [self.myMapView addAnnotation:point]; ];

    //Alexandria-Washington Lodge No. 22 same location as above use lat/long
    CLLocationCoordinate2D washington;
    washington.latitude = 38.806758;
    washington.longitude = -77.065251;
    Annotation *alexandriawashingtonlodge = [Annotation alloc];
    alexandriawashingtonlodge.coordinate = washington;
    alexandriawashingtonlodge.title = @"Alexandria-Washington Lodge No. 22";
    alexandriawashingtonlodge.subtitle = @"Alexandria, VA";
    [self.myMapView addAnnotation:alexandriawashingtonlodge];

此列表继续包含大约 310 个旅馆位置。

【问题讨论】:

您是否有意创建一个只有-alloc 而不是-init 的Annotation 对象? 嗨 Rickay,首先感谢您抽出宝贵时间。我对编码很陌生。我通过 google / youtube 观看了一些教程,并且我一直在根据我收集的几本书中的信息构建它。我真的不确定有什么区别,但我会用谷歌搜索。但总之回答你的问题。不,我没有故意只使用 -alloc 创建注释对象 此时,您永远不想只使用-alloc 创建对象。请改用-alloc -init。这既为对象分配内存并对其进行初始化,而不是仅仅为它分配内存。 瑞凯,看看我的代码。我想我这样做了...每个旅馆的代码中都有 -alloc 和 -init... 您看到哪些小屋出现而哪些没有出现的任何模式? 【参考方案1】:

这样使用,

NSString *abingdon = @"325 W Main Street Abingdon, Virginia 24210";
CLGeocoder *abingdongeo = [[CLGeocoder alloc] init];
[abingdongeo geocodeAddressString:abingdon completionHandler:^(NSArray* placemarks, NSError* error)
if (placemarks && placemarks.count > 0) 
CLPlacemark *topResult = [placemarks objectAtIndex:0];
MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = placemark.coordinate;
point.title = @"Abingdon Lodge No. 48";
point.subtitle = @"Abingdon, VA";

// Set your region using placemark (not point)          
MKCoordinateRegion region = self.mapView.region;
region.center = placemark.region.center;
region.span.longitudeDelta /= 8.0;
region.span.latitudeDelta /= 8.0;

// Add point (not placemark) to the mapView                                              
[self.mapView setRegion:region animated:YES];
[self.mapView addAnnotation:point];

// Select the PointAnnotation programatically
[self.mapView selectAnnotation:point animated:NO]; ];

【讨论】:

【参考方案2】:

我的一些注释没有显示的原因是因为我多次调用 CLGeocoder。这是我解决问题的方法。

//Columbia Lodge No. 285 CLLocationCoordinate2D columbia; columbia.latitude = 38.895746; columbia.longitude = -77.106511; Annotation *columbialodge = [[Annotation alloc] init]; columbialodge.coordinate = columbia; columbialodge.title = @"Columbia Lodge No. 285"; columbialodge.subtitle = @"Arlington, VA"; [self.myMapView addAnnotation:columbialodge];

我已将我所有的地址转换为经纬度。这也减少了内存使用量。

尽管我很喜欢只是输入地址,但看起来 CLGeocoder 只是占用了很多资源。

【讨论】:

以上是关于Iphone mapview 中未显示多个注释的主要内容,如果未能解决你的问题,请参考以下文章

无法在 iPhone 的地图上显示自定义注释

mapView 中的用户位置未显示在多个模拟器设备(MapKit)上

iPhone - MKMapView 实例的问题

在 Swift 中的同一个 mapView 中分配多个注释 pinColors?

没有在适当的时候更新 iphone 中 mkMapView 中的注释标题和副标题

MapView不显示注释