名前、緯度、経度など、複数の場所を含む NSMutableArray があります。これらの場所の値は、現在の場所から 5000 メートルの範囲内にあります。これらすべての場所の注釈ピンを 1 つのマップに表示する必要があります。出来ますか?
前もって感謝します..
名前、緯度、経度など、複数の場所を含む NSMutableArray があります。これらの場所の値は、現在の場所から 5000 メートルの範囲内にあります。これらすべての場所の注釈ピンを 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);
}
あなたは無限MKAnnotationPoint
の数を広告することができます