Reputation: 267
I am trying following code to get google maps api response
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];
I got this apiResponse json data from google api:
{
"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
},
I am using following code to get poly line points
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]];
but it returns @"" for encodedPoints
is there any problem in regular expression ?
Upvotes: 1
Views: 114
Reputation: 764
when you got the response in dictionary then do this,
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];
Upvotes: 2
Reputation: 5932
I dont think you have to use regex for this, you could use a simple json parser in objetive c and access the poyline and its points, refer :- http://pivotallabs.com/parsing-json-in-objective-c/
Upvotes: 1