@interface RouteController : UIViewController <MKMapViewDelegate, MKReverseGeocoderDelegate> {
MKPointAnnotation * _pointAnnotaion;
NSString * _selfAddress;
}
- ( id ) initWithPlaceCoordinate : ( CLLocationCoordinate2D ) coorinate
placeName : ( NSString * ) name
placeAddress : ( NSString * ) address;
@end
@implementation RouteController
- ( id ) initWithPlaceCoordinate : ( CLLocationCoordinate2D ) coorinate placeName : ( NSString * ) name placeAddress : ( NSString * ) address {
self = [ super init ] ;
if ( self ) {
self.navigationItem.rightBarButtonItem = [ [ [ UIBarButtonItem alloc ] initWithTitle : @ ""
style : UIBarButtonItemStyleBordered
target : self
action : @selector ( routeAction ) ]
autorelease ] ;
self.navigationItem.rightBarButtonItem.enabled = NO ;
_pointAnnotaion = [ MKPointAnnotation new ] ;
_pointAnnotaion.title = name;
_pointAnnotaion.subtitle = address;
_pointAnnotaion.coordinate = coorinate;
}
return self;
}
- ( void ) dealloc {
[ _pointAnnotaion release ] ;
[ _selfAddress release ] ;
[ super dealloc ] ;
}
- ( void ) loadView {
MKMapView * mapView = [ [ [ MKMapView alloc ] initWithFrame : [ UIScreen mainScreen ] .bounds ] autorelease ] ;
mapView.showsUserLocation = YES ;
mapView.delegate = self;
[ mapView addAnnotation : _pointAnnotaion ] ;
self.view = mapView;
}
#pragma mark - Map view delegate
- ( void ) mapView : ( MKMapView * ) mapView didUpdateUserLocation : ( MKUserLocation * ) userLocation {
MKReverseGeocoder * geocoder = [ [ MKReverseGeocoder alloc ] initWithCoordinate : userLocation.coordinate ] ;
geocoder.delegate = self;
[ geocoder start ] ;
}
#pragma mark - Reverse geocoder delegate
- ( void ) reverseGeocoder : ( MKReverseGeocoder * ) geocoder didFindPlacemark : ( MKPlacemark * ) placemark {
[ geocoder autorelease ] ;
NSArray * formattedAddress = [ placemark.addressDictionary objectForKey : @ "FormattedAddressLines" ] ;
[ _selfAddress release ] ;
_selfAddress = [ [ formattedAddress componentsJoinedByString : @ "," ] retain ] ;
MKMapView * mapView = ( MKMapView * ) self.view;
for ( id<MKAnnotation> annotation in [ mapView annotations ] ) {
if ( [ annotation isKindOfClass : [ MKUserLocation class ] ] ) {
( ( MKUserLocation * ) annotation ) .subtitle = _selfAddress;
}
}
self.navigationItem.rightBarButtonItem.enabled = YES ;
}
- ( void ) reverseGeocoder : ( MKReverseGeocoder * ) geocoder didFailWithError : ( NSError * ) error {
[ geocoder autorelease ] ;
}
#pragma mark - Private methods
- ( NSString * ) escapedStringFromString : ( NSString * ) string {
NSString * result = ( NSString * ) CFURLCreateStringByAddingPercentEscapes (
NULL , /* allocator */
( CFStringRef ) string ,
NULL , /* charactersToLeaveUnescaped */
( CFStringRef ) @ "!*'();:@&=+$,/?%#[]" ,
kCFStringEncodingUTF8 ) ;
return [ result autorelease ] ;
}
#pragma mark - Public methods
- ( void ) routeAction {
NSString * daddr = [ self escapedStringFromString : [ _pointAnnotaion subtitle ] ] ;
NSString * saddr = [ self escapedStringFromString : _selfAddress ] ;
NSString * routeURL = [ NSString stringWithFormat : @ "maps.google.com/maps?daddr=%@&saddr=%@" , daddr, saddr ] ;
[ [ UIApplication sharedApplication ] openURL : [ NSURL URLWithString : routeURL ] ] ;
}
@end
Source: https://habr.com/ru/post/126586/
All Articles