Basically I have used locationSearch / MKLocationSearch library that works similar to google places to give user autocomplete option prediction results. Prior to this appoach I request user to input their destination on a UITextField then CCLocationDistance would calculate the distance between current location and their input location. That was working fine, until adding the locationSearch.
Now my issue is that I don't get the result of the distance between A and B. My assumption because I am not very well versed in Objective-C, I think in this line of class;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.searchDisplayController setActive:NO animated:YES];
MKMapItem *item = results.mapItems[indexPath.row];
[mapView addAnnotation:item.placemark];
[mapView selectAnnotation:item.placemark animated:YES];
[mapView setCenterCoordinate:item.placemark.location.coordinate animated:YES];
[mapView setUserTrackingMode:MKUserTrackingModeNone];
}
I should get the item selected to able to calculate the distance, but I don't know how to do that.
This is my declaration in the mapviewcontroller.m;
CLLocationDistance distance;
and the reversegeoforwarding I use this snippet below; - (void)requestForwardGeoCoding:(NSString *)address { CLGeocoder geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressString: address completionHandler:^(NSArray placemarks, NSError* error){ for (CLPlacemark* aPlacemark in placemarks) {
CLLocationCoordinate2D fromCoordinate = CLLocationCoordinate2DMake(self.mapView.userLocation.location.coordinate.latitude,self.mapView.userLocation.location.coordinate.longitude);
CLLocationCoordinate2D toCoordinate = CLLocationCoordinate2DMake(aPlacemark.location.coordinate.latitude ,aPlacemark.location.coordinate.longitude);
NSLog(@"my location lat %f lon %f",self.mapView.userLocation.location.coordinate.latitude,self.mapView.userLocation.location.coordinate.longitude);
MKPlacemark *fromPlacemark = [[MKPlacemark alloc] initWithCoordinate:fromCoordinate
addressDictionary:nil];
MKPlacemark *toPlacemark = [[MKPlacemark alloc] initWithCoordinate:toCoordinate
addressDictionary:nil];
NSLog(@"Search Location lat %f long %f", aPlacemark.location.coordinate.latitude , aPlacemark.location.coordinate.longitude );
MKMapItem *fromItem = [[MKMapItem alloc] initWithPlacemark:fromPlacemark];
MKMapItem *toItem = [[MKMapItem alloc] initWithPlacemark:toPlacemark];
[self findDirectionsFrom:fromItem
to:toItem];
CLLocationDegrees latitude, longitude;
latitude = aPlacemark.location.coordinate.latitude;
longitude = aPlacemark.location.coordinate.longitude;
CLLocation *to = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
CLLocationCoordinate2D location;
location.latitude = latitude;
location.longitude = longitude;
VBAnnotation *ann1 = [[VBAnnotation alloc] initWithPosition:location];
[ann1 setCoordinate:location];
ann1.title = address;
ann1.subtitle = @"Destination";
[self.mapView addAnnotation:ann1];
latitude = self.mapView.userLocation.location.coordinate.latitude;
longitude = self.mapView.userLocation.location.coordinate.longitude;
CLLocation *from = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
distance= [to distanceFromLocation:from];
NSLog(@"Distance %.1f", distance/ 1000);
location.latitude = latitude;
location.longitude = longitude;
VBAnnotation *ann = [[VBAnnotation alloc] initWithPosition:location];
[ann setCoordinate:location];
ann.title = @"My Location";
ann.subtitle = @"Where you are";
[self.mapView addAnnotation:ann];
goThere = CLLocationCoordinate2DMake(aPlacemark.location.coordinate.latitude,aPlacemark.location.coordinate.longitude);
}
}];
}
I am not sure how to get the distance of the selected item from the search display controller tableView.
Please guide me how to get the distance when select location from predicted results.
Thanks in advance.