I am using GoogleMap API for showing draw direction between two points on GoogleMap.
i want to give support on iOS 6.1 so i use GoogleMap i know about iOS7 recover this.
using below code for parsing and get Steps for coordinates to draw polyline on Map:
NSString *str=@"http://maps.googleapis.com/maps/api/directions/json?origin=bharuch,gujarat&destination=vadodara,gujarat&sensor=false";
NSURL *url=[[NSURL alloc]initWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSArray* latestRoutes = [json objectForKey:@"routes"];
NSMutableDictionary *legs=[[[latestRoutes objectAtIndex:0] objectForKey:@"legs"] objectAtIndex:0];
NSArray *steps=[legs objectForKey:@"steps"];
NSString *startLocation,*endLocation,*totalDistance,*totalDuration;
CLLocationCoordinate2D startLoc,endLoc;
startLocation = [legs objectForKey:@"start_address"];
endLocation = [legs objectForKey:@"end_address"];
totalDistance = [[legs objectForKey:@"distance"] objectForKey:@"text"];
totalDuration = [[legs objectForKey:@"duration"] objectForKey:@"text"];
startLoc=CLLocationCoordinate2DMake([[[legs objectForKey:@"start_location"] objectForKey:@"lat"] doubleValue], [[[legs objectForKey:@"start_location"] objectForKey:@"lng"] doubleValue]);
endLoc=CLLocationCoordinate2DMake([[[legs objectForKey:@"end_location"] objectForKey:@"lat"] doubleValue], [[[legs objectForKey:@"end_location"] objectForKey:@"lng"] doubleValue]);
NSMutableDictionary *tempDict;
if ([steps count]!=0) {
GMSMutablePath *path = [GMSMutablePath path];
for(int idx = 0; idx < [steps count]+2; idx++){
CLLocationCoordinate2D workingCoordinate;
if (idx==0) {
workingCoordinate=startLoc;
[path addCoordinate:workingCoordinate];
}
else if (idx==[steps count]+1){
workingCoordinate=endLoc;
[path addCoordinate:workingCoordinate];
}
else{
workingCoordinate=CLLocationCoordinate2DMake([[[[steps objectAtIndex:idx-1] objectForKey:@"start_location"] objectForKey:@"lat"] floatValue], [[[[steps objectAtIndex:idx-1] objectForKey:@"start_location"] objectForKey:@"lng"] floatValue]);
[path addCoordinate:workingCoordinate];
}
tempDict = nil;
}
// create the polyline based on the array of points.
GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
rectangle.strokeWidth=5.0;
rectangle.map = mapView_;
}
its gives only 24 steps means only 24 Coordinates for create point and draw Line on Map which is shown as below image:
you can see that line is not on proper road so what can i do to solve this?
i also want to show direction on map too.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…