在mkmapview中获取路线的问题

时间:2013-04-08 10:15:28

标签: iphone ios objective-c mkmapview directions

我正在使用mkmapview获取路线,我从此answer获取了代码。

在功能

- (NSArray*)getRoutePointFrom:(Annotation *)origin to:(Annotation *)destination
{
NSString* saddr = [NSString stringWithFormat:@"%f,%f", origin.coordinate.latitude, origin.coordinate.longitude];
NSString* daddr = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];

NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];

NSError *error;
NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error];
NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];

return [self decodePolyLine:[encodedPoints mutableCopy]];
}

我遇到了一些小问题,寻找解决方案,但我不能。

问题是

1.Parse Issue

Expected a type 

in

- (NSArray*)getRoutePointFrom:(Annotation *)origin to:(Annotation *)destination

2.自动参考计数问题

 'NSString' for instance message does not declare a method with selector 'stringByMatching:capture:'

NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];

如何解决这两个问题?

3 个答案:

答案 0 :(得分:4)

试试这个

  1. 删除文件RegexKitLite.h和RegexKitLite.m

    -(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
        NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
        NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];
    
        NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@", saddr, daddr];
        NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
        NSLog(@"api url: %@", apiUrl);
        NSError *error;
        NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSUTF8StringEncoding error:&error];
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"points:\\\"([^\\\"]*)\\\"" options:0 error:NULL];
        NSTextCheckingResult *match = [regex firstMatchInString:apiResponse options:0 range:NSMakeRange(0, [apiResponse length])];
        NSString *encodedPoints = [apiResponse substringWithRange:[match rangeAtIndex:1]];
        //NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];
    
        return [self decodePolyLine:[encodedPoints mutableCopy]];
    }
    

答案 1 :(得分:3)

1:听起来像是一个导入问题。请在头文件和实现文件中向我们展示您的导入。

2:这不是ARC问题。您正在调用NSString上不存在的方法。同样,我建议这是一个导入问题,您忘记导入一个将方法添加到NSString的类别。将其导入您的实现文件。如果您没有此类别代码,那么您需要在网络上找到它。

答案 2 :(得分:2)

机器人猫的答案外,请尝试执行以下操作:

  1. 转到:This code.google Project(因为,我从那里得到了)
  2. 打开封闭文件夹
  3. 在其中找到RegexKitLite文件夹。
  4. 将其添加到您的项目中(使用copy file to destination group AND 已启用Add to target个选项)
  5. #import RegexKitLite.h添加到您的.m文件中。
  6. 问题最有可能解决。