IOS:按名称在地图上显示多个注释

时间:2016-02-17 05:53:42

标签: ios objective-c mkmapview

在我的项目中,我必须在MapView上显示多个注释。我从服务器收到的响应是显示纬度和经度。 有人请建议我代码如何在地图上显示多个注释,因为我将所有地址值放在NSArray中。我的数组在控制台中的输出如下:

数组名称:arrayforlocation

(
"[39.4835647,-119.7310213]",
"[42.5629668,-114.4608711]",
"[45.5064511,-122.7756216]",
"[40.2338438,-111.6585337]",
"[45.7832856,-108.5006904]"
)

还请建议一些代码将此数组拆分为两个数组,以获得不同的纬度和经度值。

3 个答案:

答案 0 :(得分:0)

我建议,您要求您的网络服务开发者发送位置(以纬度和经度的形式),如果他们不能,那么您可以通过CLGeocoder请求(或者可以使用其他一些准确的位置)服务提供商,例如Googlelibrary),以获取您拥有的特定地址的纬度和经度信息。只有在那之后,您才能在地图上显示特定地址的图钉。

E.g。

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Your Address String" 
    completionHandler:^(NSArray* placemarks, NSError* error){
         for (CLPlacemark* aPlacemark in placemarks)
         {
             // Process the placemark.
             NSString *latitude = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.latitude];
             NSString *longitude = [NSString stringWithFormat:@"%.4f",aPlacemark.location.coordinate.longitude];
         }
}];

然后,您可以按照this在地图上显示注释。

答案 1 :(得分:0)

以下是在地图视图上添加注释的代码:

CLLocationCoordinate2D location;
MKCoordinateSpan span;
location.latitude = (double) 44.4758;
location.longitude = (double) -73.2125;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"BCA" andCoordinate:location];
[self.mapView addAnnotation:newAnnotation];



themapView = [[MapView alloc] initWithNibName:@"MapView" bundle:nil];
[self.view addSubview:themapView.view];

希望这会对你有所帮助:)。

答案 2 :(得分:0)

我自己解决这个问题。 这是我的解决方案: 首先,我为纬度和经度创建单独的数组 在.m文件中我输入以下代码

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

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


-(void)showaddressonmap
{
    for ( int i=0; i<[latArray count]; i++)
    {
        CLLocationCoordinate2D coord;

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

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

        NearestResultDiagnose *  annotObj =[[NearestResultDiagnose alloc]initWithCoordinate:coord titleee:titleStr];
        [mapvw addAnnotation:annotObj];
    }
}

在我的.h文件中,我输入以下代码

{
    CLLocationCoordinate2D coordinate;
    NSString *titleee;
    NSString *subTitle;
    NSString *time;


}
@property (nonatomic)CLLocationCoordinate2D coordinate;

@property (nonatomic, retain) NSString *titleee;

@property (nonatomic, retain) NSString *subTitle;

@property (nonatomic,retain) NSString *time;

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

-(id)initWithCoordinate:(CLLocationCoordinate2D) c titleee:(NSString *)t;

并合成属性