In my one of location update application i have to display shortest path between multiple flierz[Property Locations].For that I used google direction api and calculate points between them using decoding them using formula If you have to do the same thing in your application you can use this formula.
eg.
eg.
http://maps.googleapis.com/maps/api/directions/json?origin=23.030064,72.546242&destination=23.030130,72.546310&sensor=true&mode=driving
you can run this link in browser and you can get structure like this
-routes
-legs
-steps
you will get steps array so you have to apply for loop and decode polylines.
-(void)calculateRoute{
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
NSMutableData *data = [NSMutableData dataWithContentsOfURL:apiUrl];
NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString *apiResponse=@"";
NSArray *arrSteps = [[[[[responseDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"steps"];
for (int l=0;l<arrSteps.count; l++) {
@autoreleasepool {
apiResponse = [[[arrSteps objectAtIndex:l] objectForKey:@"polyline"] objectForKey:@"points"];
[totalRoutes addObject:[self decodePolyLine:[apiResponse mutableCopy]] ];
}
}
you can grab decodePolyLine from here and then added totalRoutes and then can draw polylines between them.
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
[encoded replaceOccurrencesOfString:@"\\" withString:@"\/"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
[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];
}
Count = Count +[array count];
return array;
}
To draw polylines between these points you have to make array of CLLocationCoordinate2D like this:
-(void)updateRouteView{
@autoreleasepool {
pointArr = malloc(sizeof(CLLocationCoordinate2D) * Count);
NSUInteger k=0;
for(int j=0;j<totalRoutes.count;j++)
{
for(int i = 0; i < [[totalRoutes objectAtIndex:j] count]; i++) {
point = MKMapPointForCoordinate([[[totalRoutes objectAtIndex:j] objectAtIndex:i] coordinate]);
pointArr[k] = point;
k++;
}
}
routeLine = [MKPolyline polylineWithPoints:pointArr count:k];
[mapView addOverlay:routeLine];
}
}
then apply this method to draw polyline red color.
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *pv = [[MKPolylineView alloc] initWithPolyline:(MKPolyline*)overlay] ;
pv.strokeColor = [UIColor redColor];
return pv;
}
No comments:
Post a Comment