在mapview上显示多个地图注释/坐标

时间:2015-11-08 22:37:27

标签: ios objective-c mkmapview

我使用下面的代码在我的mapview上显示数组中的地址(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];

1 个答案:

答案 0 :(得分:0)

您只访问一个对象?

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的语法错误,请原谅我。

相关问题