kiran kumar
kiran kumar

Reputation: 1359

How to draw a route between two locations in a MapView in iPhone?

The below code is implemented it shows current location with blue color blinking and destination location with pin.

Now I need to draw route from my current location to destination location.

I don't know how to configure my current location to destination location to draw route in this code.

//NSString *latlong = [[NSString stringWithFormat:@"%f, %f", currentLocation.latitude, currentLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//NSString *dlatlong = [[NSString stringWithFormat:@"%f, %f", coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@&saddr=%@&daddr=%@",
//latlong, latlong, dlatlong];
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];


#import "MapViewController.h"

@implementation AddressAnnotation
@synthesize coordinate;
//@synthesize currentLocation;

- (NSString *)subtitle{
    //return @"Sub Title";
    return [NSString stringWithFormat:@"%lf, %lf", coordinate.latitude, coordinate.longitude];

}
- (NSString *)title{
    //return @"Title";
    return @"Allure-Exclusive";
        //return [NSString stringWithFormat:@"%lf, %lf", coordinate.latitude, coordinate.longitude];

}

-(id)initWithCoordinate:(CLLocationCoordinate2D) c{
    coordinate=c;
    NSLog(@"%f,%f",c.latitude,c.longitude);
    return self;
}

@end

@implementation MapViewController

@synthesize address;
@synthesize currentLocation;

static MapViewController *sharedInstance;

+(MapViewController *)sharedInstance{
    @synchronized (self)
    {
        if (!sharedInstance) 
        [[MapViewController alloc]init];
        }
    return sharedInstance;
}
+(id)alloc{
    @synchronized(self){
        NSAssert(sharedInstance==nil,"Attempted to allocate a second instance of a singleton LocationController."); 
        sharedInstance = [super alloc];
    }
    return sharedInstance;
}
-(id)init{
    if(self==[super init]){
        self.currentLocation=[[CLLocation alloc]init];
        locationManager=[[CLLocationManager alloc]init];
        locationManager.delegate=self;
        [self start];
    }
    return self;
}
-(void)start{
    NSLog(@"Start");
    mapView.showsUserLocation=YES;
    [locationManager startUpdatingLocation];
}
-(void)stop{
    mapView.showsUserLocation=NO;
    [locationManager stopUpdatingLocation];
}
-(BOOL)locationKnown{
    if (round(currentLocation.speed)==-1) 
        return NO;
        else return YES;

}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    if (abs([newLocation.timestamp timeIntervalSinceDate:[NSDate date]])<120){
        self.currentLocation=newLocation;
    }
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    UIAlertView *alert;
    alert=[[UIAlertView alloc]initWithTitle:@"Error" message:[error description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
} 

- (void)locationUpdate:(CLLocation *)location{
    [mapView setCenterCoordinate:location.coordinate];
    if ([mapView showsUserLocation]==NO) 
    {[mapView setShowsUserLocation:YES];

    }


}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
//  [self addressLocation];

    [super viewDidLoad];
    self.title=@"Map-View";
    mapView.showsUserLocation=YES;
    [self showAddress];
    NSLog(@"address is %@",address);

//NSString *latlong = [[NSString stringWithFormat:@"%f, %f", currentLocation.latitude, currentLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//NSString *dlatlong = [[NSString stringWithFormat:@"%f, %f", coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@&saddr=%@&daddr=%@",
//latlong, latlong, dlatlong];
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

-(void)showAddress{ 

    MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta=0.5f;
    span.longitudeDelta=0.5f;

    CLLocationCoordinate2D location = [self addressLocation];
    region.span=span;
    region.center=location;

    if(addAnnotation != nil) {
        [mapView removeAnnotation:addAnnotation];
        [addAnnotation release];
        addAnnotation = nil;
    }

    addAnnotation = [[AddressAnnotation alloc] initWithCoordinate:location];
    [mapView addAnnotation:addAnnotation];
    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];

}

-(CLLocationCoordinate2D) addressLocation {

    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", 
   [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//  NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString]];
    NSString *locationString=[NSString stringWithContentsOfURL:[NSURL URLWithString:urlString] encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"locationString %@",locationString);
    NSArray *listItems = [locationString componentsSeparatedByString:@","];
    double latitude = 0.0;
    double longitude = 0.0;
    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"])
    {
    latitude = [[listItems objectAtIndex:2] doubleValue];
    longitude = [[listItems objectAtIndex:3] doubleValue];
    NSLog(@"listItems %@",[listItems objectAtIndex:2]);
    }
    else {
    //Show error
    }
    CLLocationCoordinate2D location;
    location.latitude = latitude;
    location.longitude = longitude;
    return location;
}

- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>) annotation{

    if (annotation==mapView.userLocation)
    {
    mapView1.userLocation.title=@"Current Location";
    [mapView1 setRegion:MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 1000, 1000)animated:YES];
    return nil;
}
else {

    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    annView.pinColor = MKPinAnnotationColorRed;
    annView.animatesDrop=YES;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5,5);
    return annView;

}
}

//NSString *latlong = [[NSString stringWithFormat:@"%f, %f", currentLocation.latitude, currentLocation.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//NSString *dlatlong = [[NSString stringWithFormat:@"%f, %f", coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@&saddr=%@&daddr=%@",
//               latlong, latlong, dlatlong];
//[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
//  [self stop];
    [super viewDidUnload];

    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [address release];
    [currentLocation release];
    [locationManager release];
    [super dealloc];
}

@end

Upvotes: 0

Views: 589

Answers (2)

kiran kumar
kiran kumar

Reputation: 1359

NSString *latlong = [[NSString stringWithFormat:@"%f, %f", mapView.userLocation.location.coordinate.latitude, mapView.userLocation.location.coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *dlatlong = [[NSString stringWithFormat:@"%f, %f",52.366428f,4.896349f] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@&saddr=%@&daddr=%@",
latlong, latlong, dlatlong];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]]; 

I went with this code... it shows the destination perfect and userlocation is at 0.000000,0.000000 values.

I need to see current location too.

alt text

Upvotes: 1

Satya
Satya

Reputation: 3330

There is no feature to draw directions from source to destination in the map view using MKMapKit frame work.

You have to show directions using Google Maps in the iPhone by using the following code.

NSURL *url =[NSURL URLWithString:[NSString stringWithFormat:@"http://maps.google.com/maps?f=d&source=s_d&saddr=%@&daddr=%@&hl=en",[currentLoc stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ,[destLocation stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];

[[UIApplication sharedApplication] openURL:url];

If you wrote this code when the directions button clicked. Then the App will close and Google Maps will open and it will shows the direction.

Upvotes: 1

Related Questions