在地图视图中显示多个注释 [英] show multiple annotation in map view

查看:84
本文介绍了在地图视图中显示多个注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ios sdk中的地图视图的新手.我想在地图视图中显示多个注释 使用lat和long.基本上所有lat和long都来自服务器端 json格式.我正在解析所有纬度和经度,并将其保存在不同的数组中. 但是如何一次显示所有注释.我只能显示一个注释 一次.下面是我正在使用的单个注释的代码,

i am new to map view in ios sdk. i want to show multiple annotation in map view using lat and long.basically all lat and long are coming from server side in json format. i am parsing all lat and long and saving it in different array. but how to show all annotation at single time. i am able to show only one annotation at a time.Below is code for single annotation i am using,

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];

推荐答案

尝试一下

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是

MyAnnotation.h is

@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是

MyAnnotation.m is

@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

这篇关于在地图视图中显示多个注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆