Search code examples
iosdictionarynavigationmkpinannotationviewapple-maps

Getting Directions using only Apple map


I have a mapview in my app and i am using apple map's i read here that Mapkit can provide with the direction details as on the Mapview i am using the current location and the lat/long and i want that user can get the driving directions from his current location to the specified lat/long entered by him so that he can get the driving direction route easily on the nextview as i click on the button method or the callout method ...

-(void)getDirection

so the on the next view it should show me the driving directions to that place, please can any one help me out on this i am a beginner and i have never did that much with the mapview, can any one provide me some hint or code snippet that would be very much helpful to me. Or if i can do something thing like this but i don't know how to do that with my project.

enter image description here


Solution

  •    if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {
           NSString* addr = [NSString stringWithFormat:@"http://maps.apple.com/maps?daddr=%1.6f,%1.6f&saddr=%1.6f,%1.6f",pin_lat,pin_long,mapView.userLocation.coordinate.latitude,mapView.userLocation.coordinate.longitude];
    
            NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            [[UIApplication sharedApplication] openURL:url];
        }
    
        else {
            NSString* addr = [NSString stringWithFormat:@"http://maps.apple.com/maps?daddr=%1.6f,%1.6f&saddr=%1.6f,%1.6f",pin_lat,pin_long,mapView.userLocation.coordinate.latitude,mapView.userLocation.coordinate.longitude];
            NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            [[UIApplication sharedApplication] openURL:url];
    
    
        }   
    }
    else if ([buttonTitle isEqualToString:@"Google Maps"]) {
        NSLog(@"Other 2 pressed");
        //choose existing
    
        if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {
            NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=%1.6f,%1.6f",pin_lat,pin_long,mapView.userLocation.coordinate.latitude,mapView.userLocation.coordinate.longitude];
    
            NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            [[UIApplication sharedApplication] openURL:url];
        }
    
        else {
            NSString* addr = [NSString stringWithFormat:@"http://maps.google.com/maps?daddr=%1.6f,%1.6f&saddr=%1.6f,%1.6f",pin_lat,pin_long,mapView.userLocation.coordinate.latitude,mapView.userLocation.coordinate.longitude];
            NSURL* url = [[NSURL alloc] initWithString:[addr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
            [[UIApplication sharedApplication] openURL:url];
    
    
        }
    
    
    } 
    

    Where pin_lat and pin_long are float values which contains the value of place .