1

我正在寻找方向mkmapview,我从这个答案中得到了代码。

在函数中

- (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.解析问题

Expected a type 

- (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];

如何解决这两个问题?

4

3 回答 3

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]];
    }
    
于 2015-01-19T09:56:07.697 回答
3

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

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

于 2013-04-08T10:35:14.817 回答
2

除了Robotic Cat的回答,请尝试执行以下操作:

  1. Goto:这个 code.google 项目(因为,我从那里得到了那个)
  2. 打开封闭文件夹
  3. 在里面找到RegexKitLite文件夹。
  4. 将其添加到您的项目中(启用copy file to destination groupAND 选项)Add to target
  5. 添加#import RegexKitLite.h到您的.m文件中。

很可能问题会得到解决。

于 2013-04-08T11:31:36.937 回答