【问题标题】:Show multiple map annotations/coordinates on mapview在地图视图上显示多个地图注释/坐标
【发布时间】:2016-02-09 13:14:13
【问题描述】:

我正在使用以下代码将数组 (responseObject) 中的地址显示为我的地图视图上的注释。它可以工作,并且该引脚已从我的位置字符串中成功删除,但是它仅显示添加到数组中的最新地址的引脚。如何更改我的代码,以便它在地图上显示我数组中所有地址的图钉,而不仅仅是最近的地址?抱歉,如果这是一个新手问题。谢谢!

viewcontroller.m

 NSMutableDictionary *viewParams = [NSMutableDictionary new];
    [viewParams setValue:@"u000" forKey:@"view_name"];
    [DIOSView viewGet:viewParams success:^(AFHTTPRequestOperation *operation, id responseObject) {

        self.addressData = [responseObject mutableCopy];

        NSString *location = self.addressData[0][@"address"];

        CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:location
                 completionHandler:^(NSArray* placemarks, NSError* error){
                    if (placemarks && placemarks.count > 0) {
                         CLPlacemark *topResult = [placemarks objectAtIndex:0];
                         MKPlacemark *placemark = [[MKPlacemark alloc] initWithPlacemark:topResult];

                         MKCoordinateRegion region = self.mapView.region;
                    //     region.center = placemark.region.center;
                         region.span.longitudeDelta /= 8.0;
                         region.span.latitudeDelta /= 8.0;

                        [self.mapView setRegion:region animated:YES];
                      [self.mapView addAnnotation:placemark];

                        MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
                        point.coordinate = placemark.coordinate;
                        point.title = self.addressData[0][@"users_name"];
                        point.subtitle = self.addressData[0][@"userbio"];

                        [self.mapView addAnnotation:point];

【问题讨论】:

    标签: ios objective-c mkmapview


    【解决方案1】:

    您只访问一个对象?

    NSString *location = self.addressData[0][@"address"];
    

    已编辑

    1. 我认为您应该处理与视图分开的数据。即在地图视图委托的mapView:viewForAnnotation: 方法中实现geocoder 相关代码。然后你应该能够一一创建注释并为所有注释使用[self.mapView addAnnotations]
    2. 对于您的代码,我相信它的灵感来自 this answer,您应该能够通过类似的方式遍历所有位置地址

      for (NSMutableDictionary *loc in self.addressData) {
          NSString *loc = location[@"address"];
      
          CLGeocoder *geocoder = [[CLGeocoder alloc] init];
          ......
      }
      

      如果 Objective C 的语法错误,请见谅。

    【讨论】:

    • 好吧,那一行,我以为我正在访问一个对象(地址),正确的。但是,responseObject 中存储了多个地址字段。那么,如果我想在我的地图视图上显示所有地址字段,而不是只显示最近添加的地址,我会如何写那行?
    • 您可以简单地遍历self.addressData 中的所有位置,检查编辑后的答案
    • 已修复 - 谢谢!但是,我确实必须更改可变字典的名称,使其与 NSString 不匹配。除此之外,它就像一个魅力。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    相关资源
    最近更新 更多