- (void)pathCall {
if ([_txtAddressCity.text isEqualToString:@""] || [_txtaddressNumber.text isEqualToString:@""] || [_txtAddressState.text isEqualToString:@""] ||
[_txtAddressStreet.text isEqualToString:@""] || [_txtAddressZip.text isEqualToString:@""])
{
//[PCToastMessage toastWithDuration:2.0 andText:NSLocalizedString(@"All fields are compulsory in location.",Nil) inView:self.view];
return ;
}
else
{
_endingAddress=[[NSMutableString alloc] initWithString:_txtaddressNumber.text];
[_endingAddress appendString:[NSString stringWithFormat:@",+%@",_txtAddressStreet.text]];
[_endingAddress appendString:[NSString stringWithFormat:@",+%@",_txtAddressCity.text]];
[_endingAddress appendString:[NSString stringWithFormat:@",+%@",_txtAddressState.text]];
[_endingAddress appendString:[NSString stringWithFormat:@",+%@",_txtAddressZip.text]];
[_endingAddress appendString:[_endingAddress stringByReplacingOccurrencesOfString:@" " withString: @"+"]];
NSMutableString *mode= [[NSMutableString alloc]init];
if (_selectedRowOfPicker==1 || _selectedRowOfPicker==0) {
[mode appendString:@"driving"];
}
else if(_selectedRowOfPicker==2)
{
[mode appendString:@"bicycling"];
}
else if(_selectedRowOfPicker==3)
{
[mode appendString:@"walking"];
}
NSURL *url=[[NSURL alloc] initWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%@&destination=%@&mode=%@",[_startingAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[_endingAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],mode]];
NSURLResponse *res;
NSError *err;
NSData *data=[NSURLConnection sendSynchronousRequest:[[NSURLRequest alloc] initWithURL:url] returningResponse:&res error:&err];
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSMutableArray *totalSeconds;
totalSeconds = [[NSMutableArray alloc]init];
NSArray *routes=dic[@"routes"];
path1 = [GMSMutablePath path];
if (routes.count != 0) {
NSArray *legs=routes[0][@"legs"];
NSArray *steps=legs[0][@"steps"];
NSDictionary *duration=legs[0][@"duration"];
NSString *totalSeconds = [duration valueForKey:@"value"];
NSDictionary *start_address=legs[0][@"start_location"];
NSDictionary *end_address=legs[0][@"end_location"];
source.latitude = [[start_address valueForKey:@"lat"] doubleValue];
source.longitude = [[start_address valueForKey:@"lng"] doubleValue];
dest.latitude = [[end_address valueForKey:@"lat"] doubleValue];
dest.longitude = [[end_address valueForKey:@"lng"] doubleValue];
NSMutableArray *textsteps=[[NSMutableArray alloc] init];
NSMutableArray *latlong=[[NSMutableArray alloc]init];
for(int i=0; i< [steps count]; i++){
NSString *html=steps[i][@"html_instructions"];
[latlong addObject:steps[i][@"end_location"]];
[textsteps addObject:html];
NSString *polyLinePoints = [[steps[i] objectForKey:@"polyline"] objectForKey:@"points"];
GMSPath *polyLinePath = [GMSPath pathFromEncodedPath:polyLinePoints];
for (int p=0; p<polyLinePath.count; p++) {
[path1 addCoordinate:[polyLinePath coordinateAtIndex:p]];
}
}
self.detailedSteps=textsteps;
[self showDirection:latlong];
[_lblJourneyHours setTextColor:[UIColor colorWithWhite:0.33 alpha:1.0]];
_lblJourneyHours.text = [NSString stringWithFormat:@"%.02f", ([totalSeconds floatValue]/60)/60];
}
else
{
_lblJourneyHours.textColor = [UIColor redColor];
_lblJourneyHours.text = @"No route found.";
}
}
}
-(void)showDirection:(NSMutableArray*) latlong{
path = [GMSMutablePath path];
bounds = [[GMSCoordinateBounds alloc] init];
if (latlong.count !=0) {
for(int i=0; i<[latlong count]; i++){
double lat=[latlong[i][@"lat"] doubleValue];
double lng=[latlong[i][@"lng"] doubleValue];
CLLocationCoordinate2D temp;
temp.latitude=lat;
temp.longitude=lng;
bounds = [bounds includingCoordinate:temp];
[path addLatitude:lat longitude:lng];
}
}
}
- (IBAction)showMAP:(UIButton *)sender {
[self hideKeyboard];
if ([_txtAddressCity.text isEqualToString:@""] || [_txtaddressNumber.text isEqualToString:@""] || [_txtAddressState.text isEqualToString:@""] ||
[_txtAddressStreet.text isEqualToString:@""] || [_txtAddressZip.text isEqualToString:@""])
{
[PCToastMessage toastWithDuration:2.0 andText:NSLocalizedString(@"All fields are compulsory in location.",Nil) inView:self.view];
return ;
}
if ([_lblJourneyHours.text isEqualToString:@"No route found."])
{
[PCToastMessage toastWithDuration:2.0 andText:NSLocalizedString(@"No route found.",Nil) inView:self.view];
return ;
}
else
{
_mapButton.userInteractionEnabled = NO;
popUp = [[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x,0, self.view.frame.size.width, self.view.frame.size.height)];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:source.latitude longitude:source.longitude zoom:7];//23.0300° N, 72.5800° E
mapView = [GMSMapView mapWithFrame:CGRectMake(popUp.frame.origin.x+10, popUp.frame.origin.y+10, popUp.frame.size.width-20, popUp.frame.size.height-20) camera:camera];
[mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bounds withPadding:30.0f]];
popUp.backgroundColor = [UIColor whiteColor];
popUp.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
[self.view addSubview:popUp];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path1];
polyline.strokeColor = [UIColor blueColor];
polyline.strokeWidth = 3.f;
mapView.myLocationEnabled=YES;
polyline.map =mapView ;
GMSMarker *marker=[[GMSMarker alloc]init];
marker.position=CLLocationCoordinate2DMake(source.latitude, source.longitude);
marker.groundAnchor=CGPointMake(0.52,0.8);
GMSMarker *marker2=[[GMSMarker alloc]init];
marker2.position=CLLocationCoordinate2DMake(dest.latitude, dest.longitude);
marker2.groundAnchor=CGPointMake(0.52,0.8);
marker.map=mapView;
marker2.map=mapView;
[popUp addSubview:mapView];
UIButton *closeMap = [[UIButton alloc]initWithFrame:CGRectMake(mapView.frame.size.width-17,mapView.frame.origin.y-6 , 35, 35)];
[closeMap setBackgroundImage:[UIImage imageNamed:@"cross.png"] forState:UIControlStateNormal];
closeMap.backgroundColor = [UIColor clearColor];
[closeMap addTarget:self action:@selector(closeMapAction)forControlEvents:UIControlEventTouchUpInside];
[popUp addSubview:closeMap];
[UIView animateWithDuration:0.3/1.5 animations:^{
popUp.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.1, 1.1);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.3 animations:^{
popUp.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 animations:^{
popUp.transform = CGAffineTransformIdentity;
_mapButton.userInteractionEnabled = YES;
}];
}];
}];
}
}