我需要在我的iOS应用程序中显示使用Maps API的多个路径。 我可以绘制单一路线,但我如何绘制多条路线?
我正在使用谷歌方向api获得单一路线
另外在ios 5 iPhone的原生地图应用程序中显示两个弹出窗口,表示Route1和Route 2,当用户触摸所选路线时会突出显示。那么我们也可以这样做吗?
答案 0 :(得分:0)
替代品(可选),如果设置为 true,指定方向 服务可能提供不止一个 响应中的路由替代方案。 请注意,提供路线选择 可能会增加响应时间 服务器。
您需要添加查询链接alternatives = true
答案 1 :(得分:0)
这是代码
编写此代码
#pragma mark - MapView Delegate
//-----------------------------------------------------------------------
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
// [self.mapView removeAnnotation:self.annotation];
NSString *baseUrl = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=true", self.mapView.userLocation.location.coordinate.latitude, self.mapView.userLocation.location.coordinate.longitude, [[self.dictData valueForKey:@"latitude"]doubleValue],[[self.dictData valueForKey:@"longitude"]doubleValue]];
NSURL *url = [NSURL URLWithString:[baseUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSError *error = nil;
NSDictionary *result = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSArray *routes = [result objectForKey:@"routes"];
NSDictionary *firstRoute = [routes objectAtIndex:0];
NSDictionary *leg = [[firstRoute objectForKey:@"legs"] objectAtIndex:0];
NSDictionary *end_location = [leg objectForKey:@"end_location"];
double latitude = [[end_location objectForKey:@"lat"] doubleValue];
double longitude = [[end_location objectForKey:@"lng"] doubleValue];
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(latitude, longitude);
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = coordinate;
point.title = [leg objectForKey:@"end_address"];
point.subtitle = @"Event Destinations !!!";
[self.mapView addAnnotation:point];
NSArray *steps = [leg objectForKey:@"steps"];
int stepIndex = 0;
CLLocationCoordinate2D stepCoordinates[1 + [steps count] + 1];
stepCoordinates[stepIndex] = userLocation.coordinate;
for (NSDictionary *step in steps) {
NSDictionary *start_location = [step objectForKey:@"start_location"];
stepCoordinates[++stepIndex] = [self coordinateWithLocation:start_location];
if ([steps count] == stepIndex){
NSDictionary *end_location = [step objectForKey:@"end_location"];
stepCoordinates[++stepIndex] = [self coordinateWithLocation:end_location];
}
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:stepCoordinates count:1 + stepIndex];
[self.mapView addOverlay:polyLine];
}];
}
#pragma mark - Custom Methods
//-----------------------------------------------------------------------
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay {
MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay];
polylineView.strokeColor = [UIColor colorWithRed:204/255. green:45/255. blue:70/255. alpha:1.0];
polylineView.lineWidth = 10.0;
return polylineView;
}