I am new in iphone development, i want to draw path between 5 location how i can draw path in google map.
I want path from location1 to location2 , location2 to location3, location3 to location4 and location4 to location5.
here is my code
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *d1,*d2,*d3,*d4,*d5;
d1=[NSDictionary dictionaryWithObjectsAndKeys:#"location1",#"comments",#"1",#"id",#"23.02941395",#"latitude",#"72.54620655",#"longitude",nil];
d2=[NSDictionary dictionaryWithObjectsAndKeys:#"location2",#"comments",#"2",#"id",#"23.028359049999995",#"latitude",#"72.54537318333334",#"longitude",nil];
d3=[NSDictionary dictionaryWithObjectsAndKeys:#"location3",#"comments",#"3",#"id",#"23.029545",#"latitude",#"72.546036",#"longitude",nil];
d4=[NSDictionary dictionaryWithObjectsAndKeys:#"location4",#"comments",#"4",#"id",#"23.030050",#"latitude",#"72.546226",#"longitude",nil];
d5=[NSDictionary dictionaryWithObjectsAndKeys:#"location5",#"comments",#"5",#"id",#"23.030050",#"latitude",#"72.546022",#"longitude",nil];
self.reports=[NSArray arrayWithObjects:d1,d2,d3,d4,d5,nil];
for (NSDictionary *d in self.reports) {
float latitude=[[d valueForKey:#"latitude"] floatValue];
float longitude=[[d valueForKey:#"longitude"] floatValue];
Place* home = [[[Place alloc] init] autorelease];
home.name = [d valueForKey:#"comments"];
home.latitude = latitude;
home.longitude = longitude;
PlaceMark *from = [[[PlaceMark alloc] initWithPlace:home] autorelease];
[mapView addAnnotation:from];
}
[self centerMap];
}
-(void) centerMap
{
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 120;
CLLocationDegrees minLon = 150;
NSMutableArray *temp=[NSArray arrayWithArray:self.reports];
NSLog(#"%#",temp);
for (int i=0; i<[temp count];i++) {
Place* home = [[[Place alloc] init] autorelease];
home.latitude = [[[temp objectAtIndex:i] valueForKey:#"latitude"]floatValue];
home.longitude =[[[temp objectAtIndex:i] valueForKey:#"longitude"]floatValue];
PlaceMark* from = [[[PlaceMark alloc] initWithPlace:home] autorelease];
CLLocation* currentLocation = (CLLocation*)from ;
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;
}
[mapView setRegion:region animated:YES];
}
- (MKAnnotationView *)mapView:(MKMapView *)map viewForAnnotation:(id <MKAnnotation>)annotation
{
if (annotation == mapView.userLocation)
return nil;
MKPinAnnotationView *pin = (MKPinAnnotationView *) [mapView dequeueReusableAnnotationViewWithIdentifier: #"asdf"];
if (pin == nil)
pin = [[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier: #"asdf"] autorelease];
else
pin.annotation = annotation;
pin.userInteractionEnabled = YES;
UIButton *disclosureButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[disclosureButton setFrame:CGRectMake(0, 0, 30, 30)];
pin.rightCalloutAccessoryView = disclosureButton;
pin.pinColor = MKPinAnnotationColorRed;
//pin.animatesDrop = YES;
[pin setEnabled:YES];
[pin setCanShowCallout:YES];
return pin;
}
- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSString *strTitle = [NSString stringWithFormat:#"%#",[view.annotation title]];
NSMutableDictionary *d;
NSMutableArray *temp=[NSArray arrayWithArray:self.reports];
for (int i = 0; i<[temp count]; i++)
{
d = (NSMutableDictionary*)[temp objectAtIndex:i];
NSString *strAddress = [NSString stringWithFormat:#"%#",[d valueForKey:#"comments"]];
if([strAddress isEqualToString:strTitle]) {
[self presentModalViewController:self.nxtDetailsVCtr animated:YES];
[self.nxtDetailsVCtr.lblDetail setText:strAddress];
break;
}
}
}
You can use NVPlolineAnnotation to draw line inside the map. Here is the Source Code.
Frameworks needed
QuartzCore.framework
CoreLocation.framework
MapKit.framework
Files Needed
JSONKit.h
JSONKit.m
Set -fno-objc-arc in compile source for JSON in case of ARC.
In header File
__block IBOutlet MKMapView *__mapView;
NSArray* routes;
MKPointAnnotation *startLocation;
MKPointAnnotation *endLocation;
MKPolyline *polyLine;
Implementation piece of code...
- (void)viewDidLoad
{
// [__mapView setShowsUserLocation:YES];
[__mapView setZoomEnabled:YES];
[__mapView setScrollEnabled:YES];
[__mapView setMapType:MKMapTypeStandard];
[__mapView setDelegate:self];
routes=[[NSArray alloc] init];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)drawPath:(id)sender {
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
[self showRouteFrom:startLocation to:endLocation];
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
if (polyLine) {
[__mapView setVisibleMapRect:[polyLine boundingMapRect]];
[__mapView addOverlay:polyLine];
[self centerMap];
}
NSLog(#"drawing path completed");
});
});
}
-(void) showRouteFrom: (id<MKAnnotation>)source to:(id<MKAnnotation>)destination;
{
if(routes)
{
[__mapView removeAnnotations:[__mapView annotations]];
}
[__mapView addAnnotation:source];
[__mapView addAnnotation:destination];
NSLog(#"source title is %#",source.title);
NSLog(#"destination title is %#",destination.title);
routes = [self calculateRoutesFrom:source.coordinate to:destination.coordinate];
NSLog(#"routes are %#",routes);
NSInteger numberOfSteps = routes.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++)
{
CLLocation *location = [routes objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
NSLog(#"poly line to be added as ovelay is %#",polyLine);
}
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t
{
NSString* saddr = [NSString stringWithFormat:#"%f,%f", f.latitude, f.longitude];
NSString* daddr = [NSString stringWithFormat:#"%f,%f", t.latitude, t.longitude];
NSString* apiUrlStr = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/directions/json?origin=%#&destination=%#&sensor=false&avoid=highways&mode=",saddr,daddr];
NSLog(#"Fired API %#",apiUrlStr);
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSError* error = nil;
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:&error];
NSDictionary *dic=[apiResponse objectFromJSONString];
if (dic!=nil) {
NSArray *routPoints=[[NSArray alloc] initWithArray:[dic valueForKey:#"routes"]];
NSDictionary *polylineOverview=[[routPoints objectAtIndex:0] valueForKey:#"overview_polyline"];
NSString *polylinePoints = [polylineOverview objectForKey:#"points"];
return [self decodePolyLine:[polylinePoints mutableCopy]];
}
return nil;
}
- (NSMutableArray *)decodePolyLine: (NSMutableString *)encoded
{
[encoded replaceOccurrencesOfString:#"\\\\" withString:#"\\" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger lat=0;
NSInteger lng=0;
while (index < len)
{
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do
{
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do
{
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
//printf("[%f,", [latitude doubleValue]);
//printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
[array addObject:loc];
}
return array;
}
#pragma mark- MKMapView delegate method
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.strokeColor = [UIColor purpleColor];
polylineView.lineWidth = 5.0;
NSLog(#"polyline in viewFor overlay: %#",polylineView);
return polylineView;
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"did update user location called ");
MKCoordinateRegion region;
region.span.latitudeDelta = .005;
region.span.longitudeDelta = .005;
region.center = userLocation.coordinate;
[mapView setRegion:region animated:YES];
}
-(void) centerMap
{
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90.0;
CLLocationDegrees maxLon = -180.0;
CLLocationDegrees minLat = 90.0;
CLLocationDegrees minLon = 180.0;
for(int idx = 0; idx < routes.count; idx++)
{
CLLocation* currentLocation = [routes objectAtIndex:idx];
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
}
region.center.latitude = (maxLat + minLat) / 2.0;
region.center.longitude = (maxLon + minLon) / 2.0;
region.span.latitudeDelta = 0.01;
region.span.longitudeDelta = 0.01;
region.span.latitudeDelta = ((maxLat - minLat)<0.0)?100.0:(maxLat - minLat);
region.span.longitudeDelta = ((maxLon - minLon)<0.0)?100.0:(maxLon - minLon);
[__mapView setRegion:region animated:YES];
}
#pragma mark- UITextField delegate method
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
[self.view bringSubviewToFront:textField];
CGRect rect=textField.frame;
[[NSUserDefaults standardUserDefaults] setValue:NSStringFromCGRect(rect) forKey:#"textFieldRect"];
rect.origin.y=self.view.frame.size.height-260;
[UIView animateWithDuration:0.3 animations:^{
[textField setFrame:rect];
}];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *rectStr=[[NSUserDefaults standardUserDefaults] valueForKey:#"textFieldRect"];
NSArray *rectArray=[rectStr componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#"{,} "]];
CGRect rect;
rect.origin.x=[[rectArray objectAtIndex:2] floatValue];
rect.origin.y=[[rectArray objectAtIndex:4] floatValue];
rect.size.width=[[rectArray objectAtIndex:8] floatValue];
rect.size.height=[[rectArray objectAtIndex:10] floatValue];
[UIView animateWithDuration:0.3 animations:^{
[textField setFrame:rect];
}];
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSDictionary *locationDic=[self getLatLongOfPlace:[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
NSDictionary *latLongDic=[[[[[locationDic valueForKey:#"results"] objectAtIndex:0] valueForKey:#"geometry"]valueForKey:#"viewport"] valueForKey:#"northeast"];
if (textField.tag==0) {
startLocation=[[MKPointAnnotation alloc] init];
[startLocation setCoordinate:CLLocationCoordinate2DMake([[latLongDic valueForKey:#"lat"] floatValue], [[latLongDic valueForKey:#"lng"] floatValue])];
startLocation.title=[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}else{
endLocation=[[MKPointAnnotation alloc] init];
[endLocation setCoordinate:CLLocationCoordinate2DMake([[latLongDic valueForKey:#"lat"] floatValue], [[latLongDic valueForKey:#"lng"] floatValue])];
endLocation.title=[textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
NSLog(#"object at index is viewport %#",[[[[[locationDic valueForKey:#"results"] objectAtIndex:0] valueForKey:#"geometry"]valueForKey:#"viewport"] valueForKey:#"northeast"]);
}
#pragma mark Getting lat long from place name.
-(NSDictionary*)getLatLongOfPlace:(NSString*)place
{
NSString* apiUrlStr = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?address=%#&sensor=true",place];
NSLog(#"Fired URL:- %#",apiUrlStr);
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSError* error = nil;
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:&error];
NSDictionary *dic=[apiResponse objectFromJSONString];
return dic;
}
Here is SampleCode I made
NOTE:--- In case if sample doesn't work for u jsut this line in viewDidLoad Method
// [__mapView setShowsUserLocation:YES];
Screenshot
#import "ViewController.h"
#import "JSONKit.h"
#interface ViewController ()
#end
#implementation ViewController
NSMutableArray *locationarry ;
NSMutableDictionary * citywiselatlong ;
- (void)viewDidLoad
{
locationarry = [[NSMutableArray alloc]initWithObjects:#"Ahmedabad",#"Rajkot",#"Baroda",#"Surat", nil];
citywiselatlong = [[NSMutableDictionary alloc]init];
[self locationfind];
NSMutableString* TotalXMLString = [[NSMutableString alloc]init];
[TotalXMLString appendFormat:#"http://maps.googleapis.com/maps/api/distancematrix/json?origins="];
[TotalXMLString appendFormat:#"%#,%#&destinations=",[[citywiselatlong valueForKey:[locationarry objectAtIndex:0]] valueForKey:#"lat"] ,[[citywiselatlong valueForKey:[locationarry objectAtIndex:0]] valueForKey:#"lng"]];
for (int i = 1; i < [locationarry count]; i++) {
[TotalXMLString appendFormat:#"%#,%#",[[citywiselatlong valueForKey:[locationarry objectAtIndex:i]] valueForKey:#"lat"] ,[[citywiselatlong valueForKey:[locationarry objectAtIndex:i]] valueForKey:#"lng"]];
if (i != [locationarry count] -1 ) {
[TotalXMLString appendFormat:#"|"];
}
else
{
[TotalXMLString appendFormat:#"&mode=driving&sensor=false"];
}
}
NSURL* apiUrl = [NSURL URLWithString:[TotalXMLString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSError* error = nil;
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:&error];
NSDictionary *dic=[apiResponse objectFromJSONString];
NSLog(#"%#",dic);
NSMutableArray * distance = [[NSMutableArray alloc]initWithArray:[[[[dic valueForKey:#"rows"] objectAtIndex:0] valueForKey:#"elements"]valueForKey:#"distance"]];
for (int j= 0 ; j < [distance count]; j++) {
NSMutableDictionary *data = [[NSMutableDictionary alloc]initWithDictionary:[distance objectAtIndex:j]];
}
NSLog(#"%#",distance);
[__mapView setShowsUserLocation:YES];
[__mapView setZoomEnabled:YES];
[__mapView setScrollEnabled:YES];
[__mapView setMapType:MKMapTypeHybrid];
[__mapView setDelegate:self];
routes=[[NSArray alloc] init];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark- MKMapView delegate method
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.strokeColor = [UIColor purpleColor];
polylineView.lineWidth = 5.0;
NSLog(#"polyline in viewFor overlay: %#",polylineView);
return polylineView;
}
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
NSLog(#"did update user location called ");
MKCoordinateRegion region;
region.span.latitudeDelta = .005;
region.span.longitudeDelta = .005;
region.center = userLocation.coordinate;
[mapView setRegion:region animated:YES];
}
- (NSMutableArray *)decodePolyLine: (NSMutableString *)encoded
{
[encoded replaceOccurrencesOfString:#"\\\\" withString:#"\\" options:NSLiteralSearch range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
NSInteger lat=0;
NSInteger lng=0;
while (index < len)
{
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do
{
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do
{
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
//printf("[%f,", [latitude doubleValue]);
//printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
[array addObject:loc];
}
return array;
}
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t
{
NSString* saddr = [NSString stringWithFormat:#"%f,%f", f.latitude, f.longitude];
NSString* daddr = [NSString stringWithFormat:#"%f,%f", t.latitude, t.longitude];
NSString* apiUrlStr = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/directions/json?origin=%#&destination=%#&sensor=false&avoid=highways&mode=",saddr,daddr];
NSLog(#"Fired API %#",apiUrlStr);
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSError* error = nil;
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:&error];
NSDictionary *dic=[apiResponse objectFromJSONString];
if (dic!=nil) {
NSArray *routPoints=[[NSArray alloc] initWithArray:[dic valueForKey:#"routes"]];
NSDictionary *polylineOverview=[[routPoints objectAtIndex:0] valueForKey:#"overview_polyline"];
NSString *polylinePoints = [polylineOverview objectForKey:#"points"];
return [self decodePolyLine:[polylinePoints mutableCopy]];
}
return nil;
}
- (IBAction)drawPath:(id)sender {
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
[self showRouteFrom:startLocation to:endLocation];
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
if (polyLine) {
[__mapView setVisibleMapRect:[polyLine boundingMapRect]];
[__mapView addOverlay:polyLine];
}
NSLog(#"drawing path completed");
});
});
}
-(void) showRouteFrom: (id<MKAnnotation>)source to:(id<MKAnnotation>)destination;
{
routes = [self calculateRoutesFrom:source.coordinate to:destination.coordinate];
NSLog(#"routes are %#",routes);
NSInteger numberOfSteps = routes.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++)
{
CLLocation *location = [routes objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[polyLineArray addObject:polyLine];
}
- (IBAction)drawMultiple:(id)sender
{
polyLineArray=[[NSMutableArray alloc] init];
MKPointAnnotation *start=[[MKPointAnnotation alloc] init];
MKPointAnnotation *end=[[MKPointAnnotation alloc] init];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
for (int i=0;i<locationarry.count-1;i++)
{
NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i] componentsSeparatedByString:#","]];
NSDictionary *dic=[citywiselatlong objectForKey:[part objectAtIndex:0]];
[start setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:#"lat"] floatValue], [[dic valueForKey:#"lng"] floatValue])];
part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i +1 ] componentsSeparatedByString:#","]];
dic=[citywiselatlong objectForKey:[part objectAtIndex:0]];
[end setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:#"lat"] floatValue], [[dic valueForKey:#"lng"] floatValue])];
[self showRouteFrom:start to:end];
}
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:0] componentsSeparatedByString:#","]];
NSDictionary *tempDic=[[NSDictionary alloc] initWithDictionary:[citywiselatlong objectForKey:[part objectAtIndex:0]]];
[start setCoordinate:CLLocationCoordinate2DMake([[tempDic valueForKey:#"lat"] floatValue], [[tempDic valueForKey:#"lng"] floatValue])];
if (polyLine) {
[__mapView setVisibleMapRect:[polyLine boundingMapRect]];
for (MKPolyline *pLine in polyLineArray) {
[__mapView addOverlay:pLine];
}
}
for (int i =0 ; i< [locationarry count]; i ++)
{
NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i] componentsSeparatedByString:#","]];
NSDictionary *dic=[citywiselatlong objectForKey:[part objectAtIndex:0]];
MKPointAnnotation *first=[[MKPointAnnotation alloc] init];
MKPointAnnotation *second=[[MKPointAnnotation alloc] init];
[first setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:#"lat"] floatValue], [[dic valueForKey:#"lng"] floatValue])];
if(i+1<[locationarry count])
{
NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i + 1] componentsSeparatedByString:#","]];
dic=[citywiselatlong objectForKey:[part objectAtIndex:0]];
[second setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:#"lat"] floatValue], [[dic valueForKey:#"lng"] floatValue])];
}
else
{
NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i] componentsSeparatedByString:#","]];
dic=[citywiselatlong objectForKey:[part objectAtIndex:0]];;
[second setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:#"lat"] floatValue], [[dic valueForKey:#"lng"] floatValue])];
}
if (i==0)
{
[__mapView addAnnotation:first];
[__mapView addAnnotation:second];
}
else
{
[__mapView addAnnotation:second];
}
}
NSLog(#"drawing path completed");
});
});
}
-(void) centerMap
{
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90.0;
CLLocationDegrees maxLon = -180.0;
CLLocationDegrees minLat = 90.0;
CLLocationDegrees minLon = 180.0;
for(int idx = 0; idx < routes.count; idx++)
{
CLLocation* currentLocation = [routes objectAtIndex:idx];
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
}
region.center.latitude = (maxLat + minLat) / 2.0;
region.center.longitude = (maxLon + minLon) / 2.0;
region.span.latitudeDelta = 0.01;
region.span.longitudeDelta = 0.01;
region.span.latitudeDelta = ((maxLat - minLat)<0.0)?100.0:(maxLat - minLat);
region.span.longitudeDelta = ((maxLon - minLon)<0.0)?100.0:(maxLon - minLon);
[__mapView setRegion:region animated:YES];
}
#pragma mark- UITextField delegate method
-(void)locationfind
{
for (int i = 0; i < [locationarry count]; i++) {
NSDictionary *locationDic=[self getLatLongOfPlace:[[locationarry objectAtIndex:i] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
NSDictionary *latLongDic=[[[[locationDic valueForKey:#"results"] objectAtIndex:0] valueForKey:#"geometry"]valueForKey:#"location"];
NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i] componentsSeparatedByString:#","]];
[citywiselatlong setObject:latLongDic forKey:[part objectAtIndex:0]];
NSLog(#"%#",citywiselatlong);
}
}
#pragma mark Getting lat long from place name.
-(NSDictionary*)getLatLongOfPlace:(NSString*)place
{
NSString* apiUrlStr = [NSString stringWithFormat:#"http://maps.googleapis.com/maps/api/geocode/json?address=%#&sensor=true",place];
NSLog(#"Fired URL:- %#",apiUrlStr);
NSURL* apiUrl = [NSURL URLWithString:[apiUrlStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
NSError* error = nil;
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:&error];
NSDictionary *dic=[apiResponse objectFromJSONString];
return dic;
}
#end
For drawing multiple points you can use it as
need to declare mutable array in header file
NSMutableArray *polyLineArray;
Implementation file need minor changes as ===
-(void) showRouteFrom: (id<MKAnnotation>)source to:(id<MKAnnotation>)destination;
{
NSLog(#"source title is %#",source.title);
NSLog(#"destination title is %#",destination.title);
routes = [self calculateRoutesFrom:source.coordinate to:destination.coordinate];
NSLog(#"routes are %#",routes);
NSInteger numberOfSteps = routes.count;
CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++)
{
CLLocation *location = [routes objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];
[polyLineArray addObject:polyLine];
// NSLog(#"poly line to be added as ovelay is %#",polyLine);
}
- (IBAction)drawMultiple:(id)sender
{
polyLineArray=[[NSMutableArray alloc] init];
NSDictionary *kolkata,*delhi,*chennai,*mumbai,*pune;
kolkata=[NSDictionary dictionaryWithObjectsAndKeys:#"location1",#"comments",#"1",#"id",#"23.0083321",#"latitude",#"88.54276659999",#"longitude",nil];
delhi=[NSDictionary dictionaryWithObjectsAndKeys:#"location2",#"comments",#"2",#"id",#"28.889816",#"latitude",#"77.3418147",#"longitude",nil];
chennai=[NSDictionary dictionaryWithObjectsAndKeys:#"location3",#"comments",#"3",#"id",#"13.23339843",#"latitude",#"80.3322912",#"longitude",nil];
mumbai=[NSDictionary dictionaryWithObjectsAndKeys:#"location4",#"comments",#"4",#"id",#"19.2716339",#"latitude",#"72.9864994",#"longitude",nil];
pune=[NSDictionary dictionaryWithObjectsAndKeys:#"location5",#"comments",#"5",#"id",#"18.6346965",#"latitude",#"73.9894867",#"longitude",nil];
NSArray *arrayOfLocations=[[NSArray alloc] initWithObjects:kolkata,delhi,chennai,mumbai,pune,nil];
MKPointAnnotation *start=[[MKPointAnnotation alloc] init];
MKPointAnnotation *end=[[MKPointAnnotation alloc] init];
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
//Background Thread
for (int i=0;i<arrayOfLocations.count-1;i++)
{
NSDictionary *dic=[arrayOfLocations objectAtIndex:i];
[start setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:#"latitude"] floatValue], [[dic valueForKey:#"longitude"] floatValue])];
dic=[arrayOfLocations objectAtIndex:i+1];
[end setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:#"latitude"] floatValue], [[dic valueForKey:#"longitude"] floatValue])];
[self showRouteFrom:start to:end];
}
dispatch_async(dispatch_get_main_queue(), ^(void){
//Run UI Updates
NSDictionary *tempDic=[[NSDictionary alloc] initWithDictionary:[arrayOfLocations objectAtIndex:0]];
[start setCoordinate:CLLocationCoordinate2DMake([[tempDic valueForKey:#"latitude"] floatValue], [[tempDic valueForKey:#"longitude"] floatValue])];
[__mapView addAnnotation:start];
[__mapView addAnnotation:end];
if (polyLine) {
[__mapView setVisibleMapRect:[polyLine boundingMapRect]];
for (MKPolyline *pLine in polyLineArray) {
[__mapView addOverlay:pLine];
}
}
NSLog(#"drawing path completed");
});
});
}
If u need sample code here it is
Output is --------
Use BreadCrumbs here is the sample code ..it is very useful ...u can customize the code according to ur requirement
https://developer.apple.com/library/ios/samplecode/Breadcrumb/Introduction/Intro.html
I was looking for many code samples of how to draw a polyline on MKMapView like the second answer (from above) in this link. However, will that solution work on iOS 6 Apple maps? If not, can you provide a link to a sample code that draws the route on Apple maps?
Yes, it'll work.
Google maps and apple maps are different a little bit, could be some little inaccuracies.
All the API's are almost same.
P.S. I use google routes in my app since iOS5 and haven't got any problems when updated app for iOS6.
-(void)load_mapView{
Place* home = [[[Place alloc] init] autorelease];
home.name = strAdd;
CLLocationCoordinate2D loc=[self geoCodeUsingAddress:strAdd Status:1];
home.latitude =loc.latitude;
home.longitude =loc.longitude;
Place* office = [[[Place alloc] init] autorelease];
office.name = endAdd;
CLLocationCoordinate2D loc2=[self geoCodeUsingAddress:endAdd Status:2];;
office.latitude = loc2.latitude;
office.longitude = loc2.longitude;
//*******
routeView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, mapView.frame.size.width, mapView.frame.size.height)];
routeView.userInteractionEnabled = NO;
[mapView addSubview:routeView];
lineColor = [UIColor colorWithWhite:0.2 alpha:0.5];
[self showRouteFrom:home to:office];
[activityIndicator stopAnimating];
}
- (CLLocationCoordinate2D) geoCodeUsingAddress:(NSString *)address Status:(NSInteger )sts
{
double latitude = 0, longitude = 0;
NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *req = [NSString stringWithFormat:#"http://maps.google.com/maps/api/geocode/json?sensor=false&address=%#", esc_addr];
NSString *result = [NSString stringWithContentsOfURL:[NSURL URLWithString:req] encoding:NSUTF8StringEncoding error:NULL];
if (result) {
NSScanner *scanner = [NSScanner scannerWithString:result];
if ([scanner scanUpToString:#"\"lat\" :" intoString:nil] && [scanner scanString:#"\"lat\" :" intoString:nil]) {
[scanner scanDouble:&latitude];
if ([scanner scanUpToString:#"\"lng\" :" intoString:nil] && [scanner scanString:#"\"lng\" :" intoString:nil]) {
[scanner scanDouble:&longitude];
}
}
}
CLLocationCoordinate2D center;
center.latitude = latitude;
center.longitude = longitude;
return center;
}
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
[encoded replaceOccurrencesOfString:#"\\\\" withString:#"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
NSInteger len = [encoded length];
NSInteger index = 0;
NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
NSInteger lat=0;
NSInteger lng=0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[[NSNumber alloc] initWithFloat:lat * 1e-5] autorelease];
NSNumber *longitude = [[[NSNumber alloc] initWithFloat:lng * 1e-5] autorelease];
// printf("[%f,", [latitude doubleValue]);
// printf("%f]", [longitude doubleValue]);
CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];
[array addObject:loc];
}
return array;
}
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
NSString* saddr = [NSString stringWithFormat:#"%f,%f", f.latitude, f.longitude];
NSString* daddr = [NSString stringWithFormat:#"%f,%f", t.latitude, t.longitude];
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus != NotReachable){
NSString* apiUrlStr = [NSString stringWithFormat:#"http://maps.google.com/maps?output=dragdir&saddr=%#&daddr=%#", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
// NSLog(#"api url: %#", apiUrl);
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];
NSString* encodedPoints = [apiResponse stringByMatching:#"points:\\\"([^\\\"]*)\\\"" capture:1L];
// NSLog(#"encodedPoints.....%#",encodedPoints);
// NSLog(#"self decodePolyLine:[encodedPoints mutableCopy].....%#",[self decodePolyLine:[encodedPoints mutableCopy]]);
NSLog(#"return %# ",[encodedPoints mutableCopy]);
return [self decodePolyLine:[encodedPoints mutableCopy]];
}else{
[activityIndicator stopAnimating];
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:#"Oops!" message:#"Railway server is either busy or not responding at the moment. Try again later." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
return 0;
}
}
-(void) centerMap {
MKCoordinateRegion region;
CLLocationDegrees maxLat = -90;
CLLocationDegrees maxLon = -180;
CLLocationDegrees minLat = 90;
CLLocationDegrees minLon = 180;
for(int idx = 0; idx < routes.count; idx++)
{
CLLocation* currentLocation = [routes objectAtIndex:idx];
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
}
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = maxLat - minLat;
region.span.longitudeDelta = maxLon - minLon;
[mapView setRegion:region animated:YES];
}
-(void) showRouteFrom: (Place*) f to:(Place*) t {
if(routes) {
[mapView removeAnnotations:[mapView annotations]];
[routes release];
}
PlaceMark* from = [[[PlaceMark alloc] initWithPlace:f] autorelease];
PlaceMark* to = [[[PlaceMark alloc] initWithPlace:t] autorelease];
[mapView addAnnotation:from];
[mapView addAnnotation:to];
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus != NotReachable){
routes = [[self calculateRoutesFrom:from.coordinate to:to.coordinate] retain];
[self drawRoute];
[self centerMap];
}else{
[activityIndicator stopAnimating];
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:#"Oops!" message:#"Railway server is either busy or not responding at the moment. Try again later." delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
- (void)drawRoute
{
int numPoints = [routes count];
if (numPoints > 1)
{
CLLocationCoordinate2D* coords = malloc(numPoints * sizeof(CLLocationCoordinate2D));
for (int i = 0; i < numPoints; i++)
{
CLLocation* current = [routes objectAtIndex:i];
coords[i] = current.coordinate;
}
objPolyline = [MKPolyline polylineWithCoordinates:coords count:numPoints];
free(coords);
[mapView addOverlay:objPolyline];
[mapView setNeedsDisplay];
}
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *thePolylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
thePolylineView.strokeColor = [UIColor colorWithRed:((float) 204.0 / 255.0f)green:((float) 7.0 / 255.0f)blue:((float) 40.0 / 255.0f)alpha:1.0f]; // <-- So important stuff here
thePolylineView.lineWidth = 5.0;
return thePolylineView;
}
I want to add detail view having Two buttons and three labels for information by taping on annotation pin. Is there any tutorial or sample code link for this. I have search but get only canShowCallout sub title which is old ways.
Edited:- I have parsed URL then on success condition I have put this code to put annotation. What should I make change in MKAnnotation view also tell me.....
for (int i = 0; i < [appDelegate.markers count]; i++)
{
marker *aMarker = [appDelegate.markers objectAtIndex:i];
location.latitude = [aMarker.lat floatValue];
location.longitude =[aMarker.lng floatValue];
AddressAnnotation *annobj = [[AddressAnnotation alloc] initWithCoordinate:location];
//[annobj setAnnotationType:AddressAnnotationTypeHettich];
//annobj.title = aMarker.name;
//annobj.subTitle = aMarker.address;
[mapView addAnnotation:annobj];
[annobj release];
CLLocationCoordinate2D ausLoc = {location.latitude,location.longitude};
//for zoom in the showroom results region
MKCoordinateSpan ausSpan = MKCoordinateSpanMake(0.108889, 0.169922);
MKCoordinateRegion ausRegion = MKCoordinateRegionMake(ausLoc, ausSpan);
NSLog(#"No Errors");
mapView.region = ausRegion;
}
indexRow = 0;
mapAnnonations = [[[NSMutableArray alloc] init] retain];
for (int i = indexRow; i<[lat ,longi count]; i++)
{
region.center.latitude = [[lat objectAtIndex:indexRow] floatValue];
region.center.longitude = [[longi objectAtIndex:indexRow] floatValue];
region.span.longitudeDelta = 0.04f;
region.span.latitudeDelta = 0.04f;
[mymapView setRegion:region animated:YES];
coordinate.latitude = [[lat objectAtIndex:indexRow] floatValue];
coordinate.longitude = [[longi objectAtIndex:indexRow]floatValue];
NSLog(#"Latitude & Longitude:---->%f,%f",coordinate.latitude,coordinate.longitude);
mapAnnotation = [[AllMapAnnotation alloc] initWithCoordinate:coordinate];
NSString *title = [[[resData valueForKey:#"name"] mutableCopy] objectAtIndex:indexRow];
NSString *titleStr = [title stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSLog(#"Pin Title:--->%#",titleStr);
[mapAnnotation setTitle:titleStr];
//[mapAnnotation setSubtitle:[NSString stringWithFormat:#"Ph: %#",pantries.phone]];
mapAnnotation.coordinate = region.center;
[mapAnnonations insertObject:mapAnnotation atIndex:indexRow];
[mymapView addAnnotations:mapAnnonations];
//[mymapView addAnnotation:mapAnnotation];
indexRow+= 1;
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *pinView = nil;
if(annotation!= mymapView.userLocation)
{
static NSString *defaultPinID = #"pantryIdentifier";
pinView = (MKPinAnnotationView *)[mymapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID] autorelease];
pinView.pinColor = MKPinAnnotationColorGreen;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
NSInteger annotationValue = [mapAnnonations indexOfObject:annotation];
NSLog(#"Annotation Value:--->%d",annotationValue);
rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(40, 5, 20, 22);
[rightButton setBackgroundImage:[UIImage imageNamed:#"listtab_arrow.png"] forState:UIControlStateNormal];
rightButton.tag = annotationValue;
[rightButton addTarget:self action:#selector(showDetails:)
forControlEvents:UIControlEventTouchUpInside];
pinView.rightCalloutAccessoryView = rightButton;
}
else
{
[mymapView.userLocation setTitle:#"I am here"];
}
return pinView;
}
- (IBAction)showDetails:(UIView *)sender
{
NSInteger selectedIndex = sender.tag;
list = [[ListView alloc] initWithNibName:#"ListView" bundle:nil];
details = [[resData valueForKey:#"reference"] mutableCopy];
NSLog(#"Reference:--->%#",details);
list.refernceIndex = [NSString stringWithFormat:#"%#",[details objectAtIndex:selectedIndex]];
[appDelegate.navigationController pushViewController:list animated:YES];
}