gpt4 book ai didi

ios - iPhone map 路线问题

转载 作者:行者123 更新时间:2023-12-01 18:50:46 24 4
gpt4 key购买 nike

我正在尝试按照以下代码获取Google Maps API响应

NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=false", saddr, daddr];
NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];

NSLog(@"api url: %@", apiUrl);

NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSStringEncodingConversionAllowLossy error:Nil];

我从谷歌API得到了这个apiResponse json数据:
   {
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 23.0236292,
"lng" : 72.60381579999999
},
"southwest" : {
"lat" : 22.990296,
"lng" : 72.5293059
}
},
"copyrights" : "Map data ©2015 Google",
"legs" : [
{
"distance" : {
"text" : "10.8 km",
"value" : 10797
},
"duration" : {
"text" : "23 mins",
"value" : 1403
},
"end_address" : "35, Shivranjani, Jodhpur, Ahmedabad, Gujarat 380015, India",
"end_location" : {
"lat" : 23.0234672,
"lng" : 72.5293059
},
"start_address" : "6, Natvarlal Raval Marg, Archana Society, Bhairavnath, Maninagar, Ahmedabad, Gujarat 380008, India",
"start_location" : {
"lat" : 22.990296,
"lng" : 72.60381579999999
},
"steps" : [
{
"distance" : {
"text" : "0.9 km",
"value" : 898
},
"duration" : {
"text" : "2 mins",
"value" : 123
},
"end_location" : {
"lat" : 22.9943614,
"lng" : 72.5962808
},
"html_instructions" : "Head \u003cb\u003enorthwest\u003c/b\u003e on \u003cb\u003eNatvarlal Raval Marg\u003c/b\u003e toward \u003cb\u003eNatvarlal Raval Marg\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Nelsons International School (on the right)\u003c/div\u003e",
"polyline" : {
"points" : "khikC{lczLMBu@jBSd@}AtDg@hA_@z@o@rAYl@O\\yArCgA|BMXQ`@q@bBm@zAcAjCSf@kArCMZ"
},
"start_location" : {
"lat" : 22.990296,
"lng" : 72.60381579999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 km",
"value" : 192
},
"duration" : {
"text" : "1 min",
"value" : 40
},

我正在使用以下代码来获取多边形线点
   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]];
return [self decodePolyLine:[encodedPoints mutableCopy]];

但它返回@“”编码点

正则表达式有什么问题吗?

最佳答案

当您在字典中得到响应时,请执行此操作,

        NSMutableArray *pathArray = [NSMutableArray new];
NSArray *routes = [[directionResponse directionResponse] objectForKey:@"routes"];
NSDictionary *route = [routes lastObject];
DLog(@"Array routes :%@",routes);

if (route) {
NSString *overviewPolyline = [[route objectForKey: @"overview_polyline"] objectForKey:@"points"];
pathArray = [self decodePolyLine:overviewPolyline];
}
NSInteger numberOfSteps = pathArray.count;

CLLocationCoordinate2D coordinates[numberOfSteps];
for (NSInteger index = 0; index < numberOfSteps; index++)
{
CLLocation *location = [pathArray objectAtIndex:index];
CLLocationCoordinate2D coordinate = location.coordinate;
coordinates[index] = coordinate;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps];

关于ios - iPhone map 路线问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31071181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com