【问题标题】:I can't get the locatedAt geocodeLocation of a coordinate back from the routine我无法从例程中获取坐标的locatedAt geocodeLocation
【发布时间】:2017-05-30 10:41:09
【问题描述】:

代码在这里。 它基本上可以工作,但我迷失了方向,无法弄清楚如何从 [geocoder] 例程中获取“point.title”。 point.title 是 Mapkit 注释的一部分。我可以按照注释掉,在 point.title 中放入一个简单的字符串,但我不能让它返回完整的 'locatedAT' 或任何 placemark.subLocality,例如。项目。

    - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {

MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];

// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
//point.title = @"Where am I?";
//point.subtitle = @"I'm here!!!";
////////////////////////
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:userLocation.coordinate.latitude longitude:userLocation.coordinate.longitude];

[geocoder reverseGeocodeLocation: loc
               completionHandler:^(NSArray* placemarks, NSError* error){
                   CLPlacemark *placemark = [placemarks objectAtIndex:0];
                   if (placemark) {

                       // NSLog(@"placemark %@",placemark);
                       //String to hold address
                       NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                      // NSLog(@"addressDictionary %@", placemark.addressDictionary);

                     //  NSLog(@"placemark %@",placemark.region);
                     //  NSLog(@"placemark %@",placemark.country);  // Give Country Name
                     //  NSLog(@"placemark %@",placemark.locality); // Extract the city name

                     //  NSLog(@"location %@",placemark.name);
                     //  NSLog(@"location %@",placemark.ocean);
                     //  NSLog(@"location %@",placemark.postalCode);
                     //  NSLog(@"location %@",placemark.subLocality);

                       //Print the location to console
                     //  NSLog(@"I am currently at %@",locatedAt);

                      point.title = locatedAt;

                   }
                   else {
                       NSLog(@"Could not locate");
                   }
}
 ];
///////////////////////////

NSLog(@"point.title %@",point.title);

[self.mapView addAnnotation:point];
[self.mapView selectAnnotation:point animated:YES]; }

【问题讨论】:

  • 您能告诉我您遇到了什么错误或问题吗?
  • 你想从反向地理编码中得到什么。
  • 我正在尝试将locatedAt 字符串放入point.title 或point.subtitle 变量中。它是在地理编码器例程中生成的。
  • 谢谢。标记被添加好了,放置图钉,但是 point.title 没有从地理编码器例程中添加。所以我有标记,但没有来自 reversegeocode 例程的标题?
  • Mahesh - 感谢您之前的回答。它运作良好。但是..现在好像已经被删除了?

标签: ios mapkit reverse-geocoding


【解决方案1】:

我通过简单地添加/修改以下内容来使我的代码正常工作。我看不出它为什么起作用,因为 point.title = locatedAt 覆盖了最后的 point.title = @"Here I am";那是在大括号之外。因为如果我取出这两条 point.title 和 point.sustitle 行,它就不再在 pin 处打印实际的地理位置了。

                       //Print the location to console
                      //NSLog(@"I am currently at %@",locatedAt);


                      point.title = locatedAt;

                   }
                   else {
                       NSLog(@"Could not locate");
                   }

    }
 ];
point.title = @"Where am I";
   // NSLog(@"point.title %@",point.title);
point.subtitle = @"I'm here!!!";

[self.mapView addAnnotation:point];
[self.mapView selectAnnotation:point animated:YES]; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-14
    • 2012-10-31
    • 1970-01-01
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-03-25
    相关资源
    最近更新 更多