SDKDemoPlacePickerViewController.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #if !defined(__has_feature) || !__has_feature(objc_arc)
  2. #error "This file requires ARC support."
  3. #endif
  4. #import "SDKDemos/PlacesSamples/SDKDemoPlacePickerViewController.h"
  5. #import "SDKDemos/SDKDemoAPIKey.h"
  6. @implementation SDKDemoPlacePickerViewController {
  7. GMSPlacePicker *_placePicker;
  8. }
  9. - (instancetype)init {
  10. if ((self = [super init])) {
  11. CLLocationCoordinate2D southWestSydney = CLLocationCoordinate2DMake(-33.8659, 151.1953);
  12. CLLocationCoordinate2D northEastSydney = CLLocationCoordinate2DMake(-33.8645, 151.1969);
  13. GMSCoordinateBounds *sydneyBounds =
  14. [[GMSCoordinateBounds alloc] initWithCoordinate:southWestSydney coordinate:northEastSydney];
  15. GMSPlacePickerConfig *config =
  16. [[GMSPlacePickerConfig alloc] initWithViewport:sydneyBounds];
  17. _placePicker = [[GMSPlacePicker alloc] initWithConfig:config];
  18. }
  19. return self;
  20. }
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. UITextView *textView = [[UITextView alloc] initWithFrame:self.view.bounds];
  24. textView.delegate = self;
  25. textView.editable = NO;
  26. [self.view addSubview:textView];
  27. __weak UITextView *weakResultView = textView;
  28. [_placePicker pickPlaceWithCallback:^(GMSPlace *place, NSError *error) {
  29. UITextView *resultView = weakResultView;
  30. if (resultView == nil) {
  31. return;
  32. }
  33. if (place) {
  34. NSMutableAttributedString *text =
  35. [[NSMutableAttributedString alloc] initWithString:[place description]];
  36. [text appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n\n"]];
  37. [text appendAttributedString:place.attributions];
  38. resultView.attributedText = text;
  39. } else if (error) {
  40. resultView.text =
  41. [NSString stringWithFormat:@"Place picking failed with error: %@", error];
  42. } else {
  43. resultView.text = @"Place picking cancelled.";
  44. }
  45. }];
  46. }
  47. #pragma mark - UITextViewDelegate
  48. - (BOOL)textView:(UITextView *)textView
  49. shouldInteractWithURL:(NSURL *)url
  50. inRange:(NSRange)characterRange {
  51. // Make links clickable.
  52. return YES;
  53. }
  54. @end