i working on application want path between 2 locations. implemented code. fine short distance path when creating path long distance jaipur, rajasthan, india jorhat, aasam, india, application crashed.
this code
-(void)findpath{ [rectangle setmap:nil]; nsstring* str = [nsmutablestring stringwithformat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&sensor=true", self.fromsearch, self.tosearch]; if (_travelmode==uicgtravelmodewalking) { str=[str stringbyappendingformat:@"&mode=walking"]; } 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"]; nsstring *points=[[[latestroutes objectatindex:0] objectforkey:@"overview_polyline"] objectforkey:@"points"]; if ([latestroutes iskindofclass:[nsarray class]]&&latestroutes.count==0) { uialertview *alrt=[[uialertview alloc]initwithtitle:@"alert" message:@"didn't find direction" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil, nil]; [alrt show]; return; } arrdistance =[[[json valueforkeypath:@"routes.legs.steps.distance.text"] objectatindex:0]objectatindex:0]; totalduration = [[[json valueforkeypath:@"routes.legs.duration.text"] objectatindex:0]objectatindex:0]; totaldistance = [[[json valueforkeypath:@"routes.legs.distance.text"] objectatindex:0]objectatindex:0]; arrdescription =[[[json valueforkeypath:@"routes.legs.steps.html_instructions"] objectatindex:0] objectatindex:0]; dictrouteinfo=[nsdictionary dictionarywithobjectsandkeys:totaldistance,@"totaldistance",totalduration,@"totalduration",arrdistance ,@"distance",arrdescription,@"description", nil]; double srclat=[[[[json valueforkeypath:@"routes.legs.start_location.lat"] objectatindex:0] objectatindex:0] doublevalue]; double srclong=[[[[json valueforkeypath:@"routes.legs.start_location.lng"] objectatindex:0] objectatindex:0] doublevalue]; totallat = [[nsmutablearray alloc]init]; totallong = [[nsmutablearray alloc]init]; //for (int = 0; i<arrdistance.count; i++) { totallat = [[[json valueforkeypath:@"routes.legs.steps.start_location.lat"] objectatindex:0] objectatindex:0]; totallong = [[[json valueforkeypath:@"routes.legs.steps.start_location.lng"] objectatindex:0] objectatindex:0]; [self saveinfoforpath]; // } cllocationcoordinate2d location; location.latitude = srclat; location.longitude = srclong; // mapview.camera = [gmscameraposition camerawithtarget:location // zoom:10]; //gmscoordinatebounds *bounds = [[gmscoordinatebounds alloc]initwithpath:<#(gmspath *)#>] // [[gmscoordinatebounds alloc] initwithcoordinate:vancouver coordinate:calgary]; @try { // todo: better parsing. regular expression? nsarray *temp= [self decodepolyline:[points mutablecopy]]; gmsmutablepath *path = [gmsmutablepath path]; for(int idx = 0; idx < [temp count]; idx++) { cllocation *location=[temp objectatindex:idx]; [path addcoordinate:location.coordinate]; } // create polyline based on array of points. rectangle = [gmspolyline polylinewithpath:path]; rectangle.strokewidth=5.0; rectangle.map = mapview; gmscoordinatebounds *bounds = [[gmscoordinatebounds alloc]initwithpath:path]; [mapview movecamera:[gmscameraupdate fitbounds:bounds]]; } @catch (nsexception * e) { // todo: show error } } decodepolyline() method coding
-(nsmutablearray *)decodepolyline: (nsmutablestring *)encoded { [encoded replaceoccurrencesofstring:@"\\\\" withstring:@"\\" options:nsliteralsearch range:nsmakerange(0, [encoded length])]; nsinteger len = [encoded length]; nsinteger index = 0; nsmutablearray *array = [[nsmutablearray alloc] init] ; nsinteger lat=0; nsinteger lng=0; while (index < len) { nsinteger b; nsinteger shift = 0; nsinteger result = 0; { 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; { 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] ; printf("[%f,", [latitude doublevalue]); printf("%f]", [longitude doublevalue]); cllocation *loc = [[cllocation alloc] initwithlatitude:[latitude floatvalue] longitude:[longitude floatvalue]] ; [array addobject:loc]; } return array; } what should do. please suggest.
you can try pathfromencodedpath method in place of decodepolyline getting path encodestring.
nsstring *points=[[[latestroutes objectatindex:0] objectforkey:@"overview_polyline"] objectforkey:@"points"]; polyline = [gmspolyline polylinewithpath: [gmspath pathfromencodedpath: points]]; polyline.map = mapview; polyline.strokecolor = [uicolor colorwithred:0/255.0 green:4/255.0 blue:255/255.0 alpha:1.0]; polyline.strokewidth = 4.0f;
Comments
Post a Comment