I am trying to draw path on google map in ios7 . For that i used json data which is provided by google map.
- i am using google direction api and i able to draw path between two place.
- when i am using path for near place then it is coming fine but when i am using path for long distance then path not turning with google map.
- plz see in these image for more clear (before zoom).
- and after zoom , it is not turning correctly.
- For this i used following code .
NSError *error;
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://maps.googleapis.com/maps/api/directions/json?origin=chennai&destination=porur&sensor=false"]];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
//NSLog(@"the json data is %@",jsonDict);
//-- json path --
NSString *polyString = [[[[ jsonDict objectForKey:@"routes"] objectAtIndex:0] objectForKey:@"overview_polyline"] objectForKey:@"points"];
NSString *TotalEstimatedTime = [[[[[[jsonDict objectForKey:@"routes"]objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0] objectForKey:@"duration"] objectForKey:@"text"];
NSLog(@" %@ ", polyString);
NSLog(@" %@ ", TotalEstimatedTime);
//===================== drawing path ===============================
NSMutableArray *decodepolyString = [self decodePolyLine:polyString];
GMSMutablePath *path = [GMSMutablePath path];
for (int i= 0; i< [decodepolyString count]; i++)
{
CLLocation *location = [decodepolyString objectAtIndex:i];
CLLocationCoordinate2D coordinate = location.coordinate;
[path addCoordinate:coordinate];
}
GMSPolyline *overviewpolyline = [GMSPolyline polylineWithPath:path];
overviewpolyline.strokeColor = [UIColor blueColor];
overviewpolyline.strokeWidth = 4.f;
overviewpolyline.geodesic = YES;
overviewpolyline.map = self.GMSMapView1;
}
-(NSMutableArray *)decodePolyLine:(NSString *)encodedStr { NSMutableString *encoded = [[NSMutableString alloc] initWithCapacity:[encodedStr length]];
[encoded appendString:encodedStr];
[encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
options:NSLiteralSearch
range:NSMakeRange(0, [encoded length])];
float len = [encoded length];
float index = 0;
NSMutableArray *array = [[NSMutableArray alloc] init];
float lat=0;
float lng=0;
while (index < len) {
NSInteger b;
NSInteger shift = 0;
NSInteger result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = [encoded characterAtIndex:index++] - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
lng += dlng;
NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5];
CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
[array addObject:location];
}
return array;
}
- please tell why it is happening and what is the solution for this ?
- and if you help me in iOS (objective c ) its good for me.