【问题标题】:IOS: Displaying multiple annotations on map by nameIOS:按名称在地图上显示多个注释
【发布时间】:2016-02-17 05:53:41
【问题描述】:

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

数组名称:数组位置

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

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

【问题讨论】:

    标签: ios objective-c mkmapview


    【解决方案1】:

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

    例如

    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 在您的地图上显示注释。

    【讨论】:

    • 好的,如果我有数组中位置的纬度和经度。我必须使用什么代码
    • 那么您只需要创建注释,然后将其显示到地图上。
    • 请检查我更新的问题现在我有纬度和经度形式的输出数据。请建议我显示注释的代码
    • 您能否建议我使用代码将我的数组拆分为两个数组,分别显示纬度和经度值。也请支持我的问题,以便我可以提出更多问题
    • 你不应该问属于单个问题查询的不同问题。如果您还有其他问题,那么您可以随时询问您是否确定“以前没有问过”。要检查它,请在谷歌上搜索您的问题或在询问已提出的建议时查找。
    【解决方案2】:

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

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

    希望对你有帮助:)

    【讨论】:

    • 请检查我的问题我有一个数组,其中有 5 个位置值。这些值将显示在地图上
    • 您可以循环这些位置值以创建多个注释,将它们添加到数组中,然后使用这些注释重新加载您的地图。
    • 你能建议我如何将我的数组分成两个数组,分别显示纬度和经度值
    【解决方案3】:

    我自己解决了这个问题。 这是我的解决方案: 首先我为纬度和经度创建单独的数组然后 在 .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;
    

    并综合属性

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多