我正在使用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];
如何解决这两个问题?
答案 0 :(得分:4)
试试这个
删除文件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)
除机器人猫的答案外,请尝试执行以下操作:
RegexKitLite
文件夹。copy file to destination group
AND
已启用Add to target
个选项)#import RegexKitLite.h
添加到您的.m
文件中。问题最有可能解决。