1

如果路线有航点,我在 MapView 中显示路线时遇到问题。我使用 DIRECTIONS GOOGLE API:

https://developers.google.com/maps/documentation/directions/

路线不是使用道路绘制的。

有什么帮助吗?谢谢!!

麻烦图片: 在此处输入图像描述

在此处输入图像描述

编码:

reponse = [self getRouteFromGoogleApiWithPoints:arrayOfCoords andMode:@"driving" error:error];

if(reponse == nil)
{
   UIAlertView *succes= [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"txtError", nil) message:NSLocalizedString(@"ServerError", nil) delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil];
        [succes show];
}
else
{
        NSArray *routes = [reponse objectForKey:@"routes"];
        NSDictionary *route = [routes objectAtIndex:0];
        NSDictionary *polylineOverview = [route objectForKey:@"overview_polyline"];

        NSString *polylinePoints = [polylineOverview objectForKey:@"points"];
        NSArray *decodedPolyline = [self decodePolyLine:polylinePoints];

        CLLocationCoordinate2D coords[[decodedPolyline count]];
        int i=0;
        for(CLLocation* loc in decodedPolyline)
        {
            CLLocationCoordinate2D c;
            c.latitude = loc.coordinate.latitude;
            c.longitude = loc.coordinate.longitude;

            coords[i]=c;
            i++;

        }

        MKPolyline *line = [MKPolyline polylineWithCoordinates:(CLLocationCoordinate2D*)coords count:decodedPolyline.count];

        [mapview addOverlay:line];
        [mapview setNeedsDisplay];
    }
}
}

+ (NSMutableArray *)decodePolyLine: (NSString *)encoded {

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];
    CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
    [array addObject:loc];
}

return array;
}

+(NSDictionary*)getRouteFromGoogleApiWithPoints:(NSArray*)arrayOfPoints andMode:(NSString*)mode error:(NSError **)error
{
NSDictionary *result = nil;

NSString *waypoints = @"";

CLLocation* origin = [arrayOfPoints objectAtIndex:0];
CLLocation* destination = [arrayOfPoints objectAtIndex:arrayOfPoints.count - 1];

// Create the waypoints
for(int i = 1; i < arrayOfPoints.count - 2; i++)
{
    CLLocation* current = [arrayOfPoints objectAtIndex:i];
    waypoints = [waypoints stringByAppendingString:[NSString stringWithFormat:@"%f,%f%%7C",current.coordinate.latitude,current.coordinate.longitude]];
}

CLLocation* lastWaypoint = [arrayOfPoints objectAtIndex:arrayOfPoints.count - 2];
waypoints = [waypoints stringByAppendingString:[NSString stringWithFormat:@"%f,%f",lastWaypoint.coordinate.latitude,lastWaypoint.coordinate.longitude]];

NSString *urlString =[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&waypoints=%@&sensor=true&mode=%@",origin.coordinate.latitude,origin.coordinate.longitude,destination.coordinate.latitude,destination.coordinate.longitude,waypoints,mode];

NSDictionary* response = [self getDictionaryFromURLString:urlString withParameters:nil error:error];

if (response != nil)
{
    if ([[response objectForKey:@"status"] isEqualToString:@"OK"])
    {
        result = response;
        NSLog(@"%@",response);

    }
    else
    {
        if (error)
        {
            NSMutableDictionary* details = [NSMutableDictionary dictionary];
            [details setValue:[response objectForKey:@"status"] forKey:NSLocalizedDescriptionKey];
            *error = [[NSError alloc] initWithDomain:@"AppName" code:2 userInfo:details];
        }
    }
}
else
{
    if(error)
    {
        NSMutableDictionary* details = [NSMutableDictionary dictionary];
        [details setValue:NSLocalizedString(@"ErrorServidor", nil) forKey:NSLocalizedDescriptionKey];
        *error = [NSError errorWithDomain:@"AppName" code:1 userInfo:details];
        return nil;
    }
}

return result;
}
4

3 回答 3

6

首先,根据 Google 条款和条件,您不得在其他人的地图上使用他们的路线/数据。

其次,第一个是部分强加的,因为人们拥有不同的数据,将道路放置在不同的地点,将道路分成不同的路段等,因此指令不排队。

如果您想在地图中显示方向,则必须找到另一个来源。也许你应该考虑 CloudMade API。

于 2013-01-22T19:40:27.140 回答
1

现在您可以使用Google Maps SDK for iOS 了。如果您需要在 iOS6 下显示 Google Directions API,这是唯一合法的选择。

于 2013-04-29T07:12:59.973 回答
0

您可以使用 Mapkit 方向请求 API。

方向请求 api 对连续调用有一些限制,否则它的工作方式类似于 google 方向 api。

有多种方法可以获取驾驶、步行和过境的方向。还有一种方法可以获得备用路线。

MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];

MKPlacemark *sourcePlacemark = [[MKPlacemark alloc] initWithCoordinate:SourceCLLocationCorodinate).coordinate addressDictionary:nil];
MKMapItem *sourceMapItem = [[MKMapItem alloc] initWithPlacemark:sourcePlacemark];
[request setSource:sourceMapItem];

MKPlacemark *destPlacemark = [[MKPlacemark alloc] initWithCoordinate:destCLLocationCoordinate addressDictionary:nil];
MKMapItem *destMapItem = [[MKMapItem alloc] initWithPlacemark:destPlacemark];
[request setDestination:destMapItem];
[request setTransportType:MKDirectionsTransportTypeAny];
request.requestsAlternateRoutes = NO;

MKDirections *directions = [[MKDirections alloc] initWithRequest:request];

if(![directions isCalculating])
{
    [directions calculateDirectionsWithCompletionHandler:
    ^(MKDirectionsResponse *response, NSError *error) {
     if (error)
     {
         // Handle Error
         NSLog(@"Error for this particular call");
     }
     else
     {
         for (MKRoute * route in response.routes)
         {
             //Add the route.polyline to the mapkit overlay
         }
     }];
}
于 2016-03-29T18:38:05.837 回答