line on map

First of all Add frame work
1 Foundation.framework
2 CoreGraphics.framework
3 CoreLocation.framework
4 MapKit.framework
Then create nsobject file Like see.... TrailsMap.h File

#import 
#import 
@interface TrailsMap : NSObject
{
CLLocationCoordinate2D coordinate;

NSString *title;
NSString *image;
NSString *subtitle;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,copy) NSString *image;
@property (nonatomic,copy) NSString *subtitle;

- (id)initWithLocation:(CLLocationCoordinate2D)coord;
TrailsMap.m

#import "TrailsMap.h"

@implementation TrailsMap
@synthesize coordinate,title,image,subtitle;

- (id)initWithLocation:(CLLocationCoordinate2D)coord{

self = [super init];
if (self) {
coordinate = coord;

}
return self;
}
Now Create coding in mainview Please see..

ViewController.h

#import 
#import 


@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet MKMapView *MapView;
@property (nonatomic, retain) MKPolyline *routeLine;
@property (nonatomic, retain) MKPolylineView *routeLineView;

-(void)LoadMapRoute;
@end

Finally create coding in mainview.m file

ViewController.m

#import "ViewController.h"
#import "TrailsMap.h"

@interface ViewController (){
NSData *alldata;
NSMutableDictionary *data1;

NSMutableArray *RouteLocation;
NSMutableArray *RouteName;
}

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
self.MapView.delegate=self;
RouteName = [[NSMutableArray alloc] initWithObjects:@"Ahmedabad",@"Rajkot", nil];
RouteLocation = [[NSMutableArray alloc] initWithObjects:@"17.372140, 78.348945",@"17.368239, 78.350529",@"17.359355, 78.354373", nil];
[self LoadMapRoute];
}

//-------------------------------------
// ************* Map ******************
//-------------------------------------

-(void)LoadMapRoute
{
MKCoordinateSpan span = MKCoordinateSpanMake(0.8, 0.8);
MKCoordinateRegion region;
region.span = span;
region.center= CLLocationCoordinate2DMake(17.368239, 78.350529);


// Distance between two address
NSArray *coor1=[[RouteLocation objectAtIndex:0] componentsSeparatedByString:@","];
CLLocation *locA = [[CLLocation alloc] initWithLatitude:[[coor1 objectAtIndex:0] doubleValue] longitude:[[coor1 objectAtIndex:1] doubleValue]];

NSArray *coor2=[[RouteLocation objectAtIndex:1] componentsSeparatedByString:@","];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:[[coor2 objectAtIndex:0] doubleValue] longitude:[[coor2 objectAtIndex:1] doubleValue]];
CLLocationDistance distance = [locA distanceFromLocation:locB];
NSLog(@"Distance :%.0f Meters",distance);


NSString *baseUrl = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=true", [RouteLocation objectAtIndex:0],[RouteLocation objectAtIndex:1] ];

NSURL *url = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
alldata = [[NSData alloc] initWithContentsOfURL:url];

NSError *err;
data1 =[NSJSONSerialization JSONObjectWithData:alldata options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:&err];

if (err)
{
NSLog(@" %@",[err localizedDescription]);
}

NSArray *routes = [data1 objectForKey:@"routes"];
NSDictionary *firstRoute = [routes objectAtIndex:0];
NSDictionary *leg = [[firstRoute objectForKey:@"legs"] objectAtIndex:0];
NSArray *steps = [leg objectForKey:@"steps"];

int stepIndex = 0;
CLLocationCoordinate2D stepCoordinates[[steps count]+1 ];

for (NSDictionary *step in steps)
{

NSDictionary *start_location = [step objectForKey:@"start_location"];
double latitude = [[start_location objectForKey:@"lat"] doubleValue];
double longitude = [[start_location objectForKey:@"lng"] doubleValue];
stepCoordinates[stepIndex] = CLLocationCoordinate2DMake(latitude, longitude);

if (stepIndex==0)
{
TrailsMap *point=[[TrailsMap alloc] initWithLocation:stepCoordinates[stepIndex]];
point.title =[RouteName objectAtIndex:0];
point.subtitle=[NSString stringWithFormat:@"Distance :%.0f Meters",distance];
[self.MapView addAnnotation:point];
}
if (stepIndex==[steps count]-1)
{
stepIndex++;
NSDictionary *end_location = [step objectForKey:@"end_location"];
double latitude = [[end_location objectForKey:@"lat"] doubleValue];
double longitude = [[end_location objectForKey:@"lng"] doubleValue];
stepCoordinates[stepIndex] = CLLocationCoordinate2DMake(latitude, longitude);

TrailsMap *point=[[TrailsMap alloc] initWithLocation:stepCoordinates[stepIndex]];
point.title = [RouteName objectAtIndex:1];
point.subtitle=[NSString stringWithFormat:@"Distance :%.0f Meters",distance];

[self.MapView addAnnotation:point];
}
stepIndex++;
}

MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:stepCoordinates count: stepIndex];
[self.MapView addOverlay:polyLine];
[self.MapView setRegion:region animated:YES];
}

-(MKOverlayRenderer *)mapView:(MKMapView *)mapView
rendererForOverlay:(id)overlay {

MKPolylineRenderer *renderer = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
renderer.strokeColor = [UIColor redColor];
renderer.lineWidth = 5.0;

return renderer;
}

@end



Previous
Next Post »