Search code examples
iosmkmapviewcllocation

ReverseGeocode CLLocation on touching MKMapView


So I'm trying to get a CLLocation based on touching a MKMapView. I'm then trying to reverse geocode the location.

    if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
        // Not sure if this is the right method to get the location or not
        CLLocationCoordinate2D coordinate = [self.mapView convertPoint:[gestureRecognizer locationInView:self.mapView] toCoordinateFromView:self.mapView];

        CLLocation *pinLocation = [[CLLocation alloc] initWithLatitude:coordinate.latitude longitude:coordinate.longitude];
        CLGeocoder *geocoder = [[CLGeocoder alloc] init];

        [geocoder reverseGeocodeLocation:pinLocation completionHandler:^(NSArray *placemarks, NSError *error) {
            if (placemarks && placemarks.count > 0) {
                CLPlacemark *topResult = [placemarks objectAtIndex:0];
                AddressAnnotation *anAddress = [[AddressAnnotation alloc] initWithPlacemark:topResult];     

                [self.mapView addAnnotation:anAddress];
                [self.addressesArray addObject:anAddress];
            }
            else {
                AddressAnnotation *anAddress = [[AddressAnnotation alloc] initWithCoordinate:coordinate];
                [self.mapView addAnnotation:anAddress];
                [self.addressesArray addObject:anAddress];
            }
        }];

With my if/else statement, what I want to do is touch the map and get the CLLocation. If I can reverse geocode the location, drop the pin with the address. If I can't reverse geocode the location, then drop the pin and just show the latitude and longitude in the map callout.

It doesn't seem to work. It seems like even though I touch the map at a certain location, the reverse geocoding makes my pin go somewhere else where the reverse geocode can locate the location. This isn't the behavior I want though. I want to drop the pin no matter what, and if I can't show the address in the callout, just show the lat/long.

If I remove the revseGeocodeLocation:pinLocation code altogether, and just use what's in the else block, then I get the lat/long and the pin gets dropped there no matter what. Is there a reason why the reverse geocoder stops me from dropping the pin where I want?

As a side note, can anyone confirm if the way I calculate the CLLocationCoordinate2D based on the UITapGestureRecognizer correct?

Thanks!


Solution

  • The coordinate calculation based on the tap point looks correct.

    The problem is that when coordinate is reverse-geocoded, the placemark returned may be some place near those coordinates and the CLPlacemark object itself (topResult) contains that place's exact coordinates in topResult.location.coordinate (which is the coordinate that the annotation is using when created using initWithPlacemark).

    Unless the user happens to tap exactly on a reverse-geocode-able coordinate, the placemark will not be exactly where they tapped.

    Since you want to place the pin exactly where the user tapped regardless of the nearest found placemark, what you can do is override the coordinate used by AddressAnnotation when it is initialized using a placemark.

    For example, after calling initWithPlacemark (assuming that method sets the annotation's coordinate property using the placemark's location), you could afterwards override it:

    AddressAnnotation *anAddress = [[AddressAnnotation alloc] initWithPlacemark...
    anAddress.coordinate = coordinate; // <-- replace with tapped coordinate
    

    Alternatively, you could use initWithCoordinate instead and set the title, etc manually using the information from topResult.

    Also, if the placemark found is more than some distance x from the tapped coordinate, consider adding a prefix such as "Near " in front of the title/subtitle to indicate to the user that the pin is not exactly at the tapped location (eg. "Near Eiffel Tower").