2012-09-02 59 views
0

我正在使用mapKit绘制从点到点的路线。我做的。 但我想得到的路线长度不是直线距离。如何在mapKit中获取路径长度

nextView.startPoint = [NSString stringWithFormat:@"%f,%f", userLatitude , userLongitude]; 
nextView.endPoint = [NSString stringWithFormat:@"%f,%f", 30.793636, 31.009641]; 
[diretions loadWithStartPoint:startPoint endPoint:endPoint options:options]; 

Aloso我想给它一个通过路径的中点。

回答

3

要做到这一点,你将不得不使用方向API,最好是Google Directions API。您应该查看该链接并阅读它,Apple没有内置方向API。您可以发送请求并要求提供JSON响应,因此我会使用AFNetworking to make like easier (on Github)JSONKit also on Github。然后发送请求并解析JSON响应。在响应中你需要编码点,这是一组基本上跟踪路线的坐标。然后您需要在叠加层上显示。下面是一些示例代码,但您复制并粘贴在此之前在请务必阅读GDirections对象API网站,你就会明白一切更容易,可以学习如何做更多:

// DRAG IN AFNETWORKING FILES AND JSON KIT FILES TO YOUR PROJECT AND ALSO IMPORT THE MAP KIT AND CORE LOCATION FRAMEWORKS 

// IMPORT FILES 

#import "StringHelper.h" 
#import "JSONKit.h" 
#import "AFJSONRequestOperation.h" 
#import "AFHTTPClient.h" 
#import <MapKit/MapKit.h> 
#import <CoreLocation/CoreLocation.h> 

// DECLARE MUTABLE ARRAY IN .H: 

NSMutableArray *_path; 

// ADD THIS CODE TO WHEN YOU WANT TO REQUEST FOR DIRECTIONS 

    AFHTTPClient *_httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://maps.googleapis.com/"]]; 

    [_httpClient registerHTTPOperationClass: [AFJSONRequestOperation class]]; 

    [_httpClient setDefaultHeader:@"Accept" value:@"application/json"]; 

    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; 

    [parameters setObject:[NSString stringWithFormat:@"%f,%f", location.coordinate.latitude, location.coordinate.longitude] forKey:@"origin"]; 

    [parameters setObject:[NSString stringWithFormat:@"%f,%f", location2.coordinate.latitude, location2.coordinate.longitude] forKey:@"destination"]; 

    [parameters setObject:@"false" forKey:@"sensor"]; 

    [parameters setObject:@"driving" forKey:@"mode"]; 

    [parameters setObject:@"metric" forKey: @"units"]; 

    NSMutableURLRequest *request = [_httpClient requestWithMethod:@"GET" path: @"maps/api/directions/json" parameters:parameters]; 

    request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData; 

    AFHTTPRequestOperation *operation = [_httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { 

     NSInteger statusCode = operation.response.statusCode; 

     if (statusCode == 200) { 

      [self parseResponse:responseObject]; 

     } 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { }]; 

    [_httpClient enqueueHTTPRequestOperation:operation]; 

    // NOW ADD THE PARSERESPONSE METHOD 
- (void)parseResponse:(NSDictionary *)response { 

NSString *status = [response objectForKey: @"status"]; 

NSArray *routes = [response objectForKey:@"routes"]; 

NSDictionary *routePath = [routes lastObject]; 

if (routePath) { 

    NSString *overviewPolyline = [[routePath objectForKey: @"overview_polyline"] objectForKey:@"points"]; 

    _path = [self decodePolyLine:overviewPolyline]; 

    NSInteger numberOfSteps = _path.count; 

    CLLocationCoordinate2D coordinates[numberOfSteps]; 
    for (NSInteger index = 0; index < numberOfSteps; index++) { 
     CLLocation *location = [_path objectAtIndex:index]; 
     CLLocationCoordinate2D coordinate = location.coordinate; 

     coordinates[index] = coordinate; 
    } 

    polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps]; 
    [self.mapView addOverlay:polyLine]; 
} 

} 

// IMPLEMENTING THE DECODEPOLYLINE METHOD: 

-(NSMutableArray *)decodePolyLine:(NSString *)encodedStr { 

NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]]; 
[encoded appendString:encodedStr]; 
[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]; 

    CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]]; 
    [array addObject:location]; 
} 

return array; 

} 


// IMPLEMENTING THE VIEWFOROVERLAY DELEGATE METHOD (MAKE SURE TO SET YOUR MAP VIEW'S DELEGATE TO SELF OR THIS WONT GET CALLED) 

- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay { 

MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay]; 

polylineView.strokeColor = [UIColor blueColor]; 
polylineView.lineWidth = 5.0; 
polylineView.alpha = 0.7; 

return polylineView; 

} 

,并应得到您的方向路线开始运行!

+0

感谢您的合作 –

+0

如果它帮助你可以upvote或打勾 – MCKapur

+0

Vote Up需要15声望,我是新的堆栈..对不起 –