【问题标题】:how to display multiple locations in a single map view in iphone application如何在 iPhone 应用程序的单个地图视图中显示多个位置
【发布时间】:2012-11-25 13:38:15
【问题描述】:

我有 NSMutableArray,其中包含多个地名、纬度和经度。这些位置值在距当前位置 5000 米的范围内。我需要在一张地图中显示所有位置的注释图钉。有可能吗?

提前谢谢..

【问题讨论】:

  • 关注这个答案。正是你在寻找什么stackoverflow.com/questions/7796923/…>

标签: iphone objective-c annotations mkmapview


【解决方案1】:
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject <MKAnnotation>
{

CLLocationCoordinate2D  coordinate;
NSString*               title;
NSString*               subtitle;
}

@property (nonatomic, assign)CLLocationCoordinate2D coordinate;
@property (nonatomic, copy)NSString *title;
@property (nonatomic, copy)NSString *subtitle;

@end


#import "MyAnnotation.h"

@implementation MyAnnotation

@synthesize title;
@synthesize subtitle;
@synthesize coordinate;

- (void)dealloc 
{
    [super dealloc];
    self.title = nil;
    self.subtitle = nil;
}
@end

//in your controller in .h
MyAnnotation* myAnnotation;
NSMutableArray *annotations;

//im .m file
- (void)viewDidLoad 
{
[super viewDidLoad];
mapp.showsUserLocation = YES;

mapp.delegate = self;

annotations = [[NSMutableArray alloc] init];
for (int i=0; i<[arrayLatitude count]; i++)
{
        CLLocationCoordinate2D theCoordinate1;

            theCoordinate1.latitude  = [[arrayLatitude objectAtIndex:i] floatValue];
            theCoordinate1.longitude = [[arrayLongitude objectAtIndex:i] floatValue];

            myAnnotation = [[MyAnnotation alloc] init];
            myAnnotation.coordinate = theCoordinate1;
            myAnnotation.title      = [arrayName objectAtIndex:i];
            [mapp addAnnotation:myAnnotation];
            [annotations addObject:myAnnotation]

}


NSLog(@"%d",[annotations count]);


MKMapRect flyTo = MKMapRectNull;
for (id <MKAnnotation> annotation in annotations)
{
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
    if (MKMapRectIsNull(flyTo)) 
    {
        flyTo = pointRect;
    }
    else 
    {
        flyTo = MKMapRectUnion(flyTo, pointRect);
    }
}
mapp.visibleMapRect = flyTo;

}


- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[[MKPinAnnotationView alloc]
                                 initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier] autorelease];
pinView.animatesDrop   = NO;
pinView.canShowCallout = YES;
pinView.pinColor = MKPinAnnotationColorRed;

UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%i.jpg",rand()%3908+1]]];
pinView.leftCalloutAccessoryView = icon;
[icon release];

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:nil forState:UIControlStateNormal];
[rightButton addTarget:self
                action:@selector(myMethod:)
      forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;

return pinView;
}


- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(@"Title     : %@",view.annotation.title);
NSLog(@"Latitude  : %f", view.annotation.coordinate.latitude);
NSLog(@"Longitude : %f", view.annotation.coordinate.longitude);



}

【讨论】:

    【解决方案2】:

    您可以发布无限数量的MKAnnotationPoint s

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-16
      • 2019-12-21
      • 1970-01-01
      • 2017-09-14
      • 1970-01-01
      相关资源
      最近更新 更多