Вам необходимо вызвать службы направления Google.Прочитайте документацию по адресу
http://code.google.com/apis/maps/documentation/directions/
Используйте URL-адрес, который соответствует вашим потребностям.Тогда вам понадобится анализатор JSON или XML.У JSON есть API, вы можете скачать его здесь
https://github.com/stig/json-framework/
Затем вы можете получить точки полилинии маршрута, используя:
if (error == nil) {
NSError *JSONError = nil;
SBJsonParser *JSONParser = [[SBJsonParser alloc] init];
id directions = [JSONParser objectWithString:directionsInJSON error:&JSONError];
if (JSONError == nil) {
directions = (NSDictionary*)directions;
//NSLog(@"Directions: %@",directions);
NSString *status = [directions objectForKey:@"status"];
if ([status isEqualToString:@"OK"]) {
gotDirections = YES;
NSArray *routes = [directions objectForKey:@"routes"];
NSDictionary *route = [routes objectAtIndex:0];
NSDictionary *polylineOverview = [route objectForKey:@"overview_polyline"];
NSString *polylinePoints = [polylineOverview objectForKey:@"points"];
NSArray *decodedPolyline = [self decodePolyLine:polylinePoints];
}
}
}
//handle elses
- (NSMutableArray *)decodePolyLine: (NSString *)encoded {
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];
CLLocation *loc = [[[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]] autorelease];
[array addObject:loc];
}
return array;
}
Then display this array using mkpolyline:
CLLocationCoordinate2D coords[[routes count]];
for(int i = 0; i < route.count; i++) {
CLLocation* location = (CLLocation*)[NSKeyedUnarchiver unarchiveObjectWithData:[routes objectAtIndex:i]];
CLLocationCoordinate2D c;
c.latitude = location.coordinate.latitude;
c.longitude = location.coordinate.longitude;
coords[i] = c;
Annotation *ann = [[Annotation alloc] initWithTitle:@"" Subtitle:@"" andCoordinate:c];
[mapView addAnnotation:ann];
[ann release];
}
MKPolyline *line = [MKPolyline polylineWithCoordinates:(CLLocationCoordinate2D*)coords count:[route count]];
[self.mapView addOverlay:line];
Также реализуйте делегат mapView: viewForOverlayМетод:
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
if ([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.strokeColor = [UIColor redColor];
polylineView.lineWidth = 1.5;
return polylineView;
}
return [[MKOverlayView alloc] initWithOverlay:overlay];
}