StructuredGeocoderViewController.m 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2016 Google Inc. All rights reserved.
  3. *
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  6. * file except in compliance with the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed under
  11. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  12. * ANY KIND, either express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #import "GoogleMapsDemos/Samples/StructuredGeocoderViewController.h"
  16. #import <GoogleMaps/GoogleMaps.h>
  17. @interface StructuredGeocoderViewController () <GMSMapViewDelegate>
  18. @end
  19. @implementation StructuredGeocoderViewController {
  20. GMSMapView *_mapView;
  21. GMSGeocoder *_geocoder;
  22. }
  23. - (void)viewDidLoad {
  24. [super viewDidLoad];
  25. GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.868
  26. longitude:151.2086
  27. zoom:12];
  28. _mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  29. _mapView.delegate = self;
  30. _geocoder = [[GMSGeocoder alloc] init];
  31. self.view = _mapView;
  32. }
  33. #pragma mark - GMSMapViewDelegate
  34. - (void)mapView:(GMSMapView *)mapView didLongPressAtCoordinate:(CLLocationCoordinate2D)coordinate {
  35. // On a long press, reverse geocode this location.
  36. __weak typeof(self) weakSelf = self;
  37. GMSReverseGeocodeCallback handler = ^(GMSReverseGeocodeResponse *response, NSError *error) {
  38. [weakSelf handleResponse:response coordinate:coordinate error:error];
  39. };
  40. [_geocoder reverseGeocodeCoordinate:coordinate completionHandler:handler];
  41. }
  42. #pragma mark - StructuredGeocoderViewController Private Category
  43. - (void)handleResponse:(nullable GMSReverseGeocodeResponse *)response
  44. coordinate:(CLLocationCoordinate2D)coordinate
  45. error:(nullable NSError *)error {
  46. GMSAddress *address = response.firstResult;
  47. if (address) {
  48. NSLog(@"Geocoder result: %@", address);
  49. GMSMarker *marker = [GMSMarker markerWithPosition:address.coordinate];
  50. marker.title = address.thoroughfare;
  51. NSMutableString *snippet = [[NSMutableString alloc] init];
  52. if (address.subLocality != NULL) {
  53. [snippet appendString:[NSString stringWithFormat:@"subLocality: %@\n", address.subLocality]];
  54. }
  55. if (address.locality != NULL) {
  56. [snippet appendString:[NSString stringWithFormat:@"locality: %@\n", address.locality]];
  57. }
  58. if (address.administrativeArea != NULL) {
  59. [snippet appendString:[NSString stringWithFormat:@"administrativeArea: %@\n",
  60. address.administrativeArea]];
  61. }
  62. if (address.country != NULL) {
  63. [snippet appendString:[NSString stringWithFormat:@"country: %@\n", address.country]];
  64. }
  65. marker.snippet = snippet;
  66. marker.appearAnimation = kGMSMarkerAnimationPop;
  67. marker.map = _mapView;
  68. _mapView.selectedMarker = marker;
  69. } else {
  70. NSLog(@"Could not reverse geocode point (%f,%f): %@", coordinate.latitude, coordinate.longitude,
  71. error);
  72. }
  73. }
  74. @end