2013-10-28 2 views
1

Я новичок в отображении карты в ios sdk. Я хочу показать несколько аннотаций в виде карты с использованием lat и long.basically все lat и long идут со стороны сервера в формате json. Я разбираю все lat и long и сохраняю их в разных массивах. , но как показать все аннотации за один раз. я могу показать только один аннотацию на time.Below является код для одной аннотации я использую,показать несколько аннотаций на карте

zoomLocation.latitude = latmpa.doubleValue; 
zoomLocation.longitude = logmpa.doubleValue; 
annotationPoint = [[MKPointAnnotation alloc] init]; 
annotationPoint.coordinate = zoomLocation; 
annotationPoint.title = @"masjid...."; 
[mapView selectAnnotation:annotationPoint animated:YES]; 


[mapView addAnnotation:annotationPoint]; 

mapView.centerCoordinate = annotationPoint.coordinate; 

MKCoordinateSpan span; 
span.latitudeDelta = 1.5; 
span.longitudeDelta = 1.0; 
MKCoordinateRegion newRegion; 
newRegion.center = zoomLocation; 
newRegion.span = span; 
[mapView setRegion:newRegion animated:YES]; 
+2

пойти на 'для loop' для добавления аннотаций к карте. – user1673099

ответ

4

Try This

for (int i=0; i<[yourLatLongarray count]; i++) 
{ 
CLLocationCoordinate2D coord; 

    coord.latitude=[[NSString stringWithFormat:@"%@",[yourLatitudeArray objectAtIndex:i]] floatValue]; 
    coord.longitude=[[NSString stringWithFormat:@"%@", 
         [yourLongitudeArray objectAtIndex:i]] floatValue]; 
    MKCoordinateRegion region1; 
    region1.center=coord; 
    region1.span.longitudeDelta=20 ; 
    region1.span.latitudeDelta=20; 
    [mapview setRegion:region1 animated:YES]; 

    NSString *titleStr =[namesArr objectAtIndex:i] ; 
    // NSLog(@"title is:%@",titleStr); 

    MyAnnotation* annotObj =[[MyAnnotation alloc]initWithCoordinate:coord title:titleStr]; 
    [mapview addAnnotation:annotObj]; 

} 

MyAnnotation.h является

@interface MyAnnotation : NSObject <MKAnnotation> 
{ 
    CLLocationCoordinate2D coordinate; 
    NSString *title; 
    NSString *subTitle; 
    NSString *time; 
} 

@property (nonatomic)CLLocationCoordinate2D coordinate; 

@property (nonatomic, retain) NSString *title; 

@property (nonatomic, retain) NSString *subTitle; 

@property (nonatomic,retain) NSString *time; 

-(id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *) t subTitle:(NSString *)timed time:(NSString *)tim; 

-(id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)tit; 

@end 

MyAnnotation.m является

@implementation MyAnnotation 

@synthesize coordinate; 

@synthesize title; 

@synthesize time; 

@synthesize subTitle; 

-(id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *) t subTitle:(NSString *)timed time:(NSString *)tim 
{ 
    self.coordinate=c; 
    self.time=tim; 
    self.subTitle=timed; 
    self.title=t; 
    return self; 
} 

-(id)initWithCoordinate:(CLLocationCoordinate2D) c title:(NSString *)tit 
{ 
    self.coordinate=c; 
    self.title=tit; 
    return self; 
} 

@end 
+0

MyAnnotation * annotObj = [[MyAnnotation alloc] initWithCoordinate: coord title: titleStr]; [mapview addAnnotation: annotObj]; что это?? – user2902101

+0

MyAnnotation - это класс ... в этом классе мы должны реализовать заголовок аннотации и подзаголовок ... – guptha

+0

благодаря его работе. – user2902101

0

Получить всю широту и долготу в массиве и пойти на петлю for, как user1673099 сказал,

и добавьте следующий код zoomtofit для изменения масштаба MapView, чтобы охватить все аннотации в MapView путем вызова метода,

[self zoomToFitMapAnnotations:self.mapView]; 

- (void)zoomToFitMapAnnotations:(MKMapView *)mapView { 
if ([mapView.annotations count] == 0) return; 

CLLocationCoordinate2D topLeftCoord; 
topLeftCoord.latitude = -90; 
topLeftCoord.longitude = 180; 
CLLocationCoordinate2D bottomRightCoord; 
bottomRightCoord.latitude = 90; 
bottomRightCoord.longitude = -180; 

for(id<MKAnnotation> annotation in mapView.annotations) { 
    topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude); 
    topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude); 
    bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude); 
    bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude); 
} 

MKCoordinateRegion region; 
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5; 
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5; 
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; 

region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; 

region = [mapView regionThatFits:region]; 
[mapView setRegion:region animated:YES]; 
} 
+0

должен ли я добавить этот код в цикл? – user2902101

+0

Добавить '- (void) zoomToFitMapAnnotations: (MKMapView *) mapView;' в вашем .h и затем в .m файле после цикла for (не внутри) добавить '[self zoomToFitMapAnnotations: self.mapView];' – NAZIK

+0

Я добавил тот же, но не работает ..... – user2902101

Смежные вопросы