Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code> #import "ViewController.h" #import "JSONKit.h" @interface ViewController () @end @implementation ViewController NSMutableArray *locationarry ; NSMutableDictionary * citywiselatlong ; - (void)viewDidLoad { locationarry = [[NSMutableArray alloc]initWithObjects:@"Ahmedabad",@"Rajkot",@"Baroda",@"Surat", nil]; citywiselatlong = [[NSMutableDictionary alloc]init]; [self locationfind]; NSMutableString* TotalXMLString = [[NSMutableString alloc]init]; [TotalXMLString appendFormat:@"http://maps.googleapis.com/maps/api/distancematrix/json?origins="]; [TotalXMLString appendFormat:@"%@,%@&amp;destinations=",[[citywiselatlong valueForKey:[locationarry objectAtIndex:0]] valueForKey:@"lat"] ,[[citywiselatlong valueForKey:[locationarry objectAtIndex:0]] valueForKey:@"lng"]]; for (int i = 1; i &lt; [locationarry count]; i++) { [TotalXMLString appendFormat:@"%@,%@",[[citywiselatlong valueForKey:[locationarry objectAtIndex:i]] valueForKey:@"lat"] ,[[citywiselatlong valueForKey:[locationarry objectAtIndex:i]] valueForKey:@"lng"]]; if (i != [locationarry count] -1 ) { [TotalXMLString appendFormat:@"|"]; } else { [TotalXMLString appendFormat:@"&amp;mode=driving&amp;sensor=false"]; } } NSURL* apiUrl = [NSURL URLWithString:[TotalXMLString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; NSError* error = nil; NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:&amp;error]; NSDictionary *dic=[apiResponse objectFromJSONString]; NSLog(@"%@",dic); NSMutableArray * distance = [[NSMutableArray alloc]initWithArray:[[[[dic valueForKey:@"rows"] objectAtIndex:0] valueForKey:@"elements"]valueForKey:@"distance"]]; for (int j= 0 ; j &lt; [distance count]; j++) { NSMutableDictionary *data = [[NSMutableDictionary alloc]initWithDictionary:[distance objectAtIndex:j]]; } NSLog(@"%@",distance); [__mapView setShowsUserLocation:YES]; [__mapView setZoomEnabled:YES]; [__mapView setScrollEnabled:YES]; [__mapView setMapType:MKMapTypeHybrid]; [__mapView setDelegate:self]; routes=[[NSArray alloc] init]; [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark- MKMapView delegate method - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id &lt;MKOverlay&gt;)overlay { MKPolylineView *polylineView = [[MKPolylineView alloc] initWithPolyline:overlay]; polylineView.strokeColor = [UIColor purpleColor]; polylineView.lineWidth = 5.0; NSLog(@"polyline in viewFor overlay: %@",polylineView); return polylineView; } - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation { NSLog(@"did update user location called "); MKCoordinateRegion region; region.span.latitudeDelta = .005; region.span.longitudeDelta = .005; region.center = userLocation.coordinate; [mapView setRegion:region animated:YES]; } - (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 &lt; len) { NSInteger b; NSInteger shift = 0; NSInteger result = 0; do { b = [encoded characterAtIndex:index++] - 63; result |= (b &amp; 0x1f) &lt;&lt; shift; shift += 5; } while (b &gt;= 0x20); NSInteger dlat = ((result &amp; 1) ? ~(result &gt;&gt; 1) : (result &gt;&gt; 1)); lat += dlat; shift = 0; result = 0; do { b = [encoded characterAtIndex:index++] - 63; result |= (b &amp; 0x1f) &lt;&lt; shift; shift += 5; } while (b &gt;= 0x20); NSInteger dlng = ((result &amp; 1) ? ~(result &gt;&gt; 1) : (result &gt;&gt; 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; } -(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t { NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude]; NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude]; NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%@&amp;destination=%@&amp;sensor=false&amp;avoid=highways&amp;mode=",saddr,daddr]; NSLog(@"Fired API %@",apiUrlStr); NSURL* apiUrl = [NSURL URLWithString:apiUrlStr]; NSError* error = nil; NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:&amp;error]; NSDictionary *dic=[apiResponse objectFromJSONString]; if (dic!=nil) { NSArray *routPoints=[[NSArray alloc] initWithArray:[dic valueForKey:@"routes"]]; NSDictionary *polylineOverview=[[routPoints objectAtIndex:0] valueForKey:@"overview_polyline"]; NSString *polylinePoints = [polylineOverview objectForKey:@"points"]; return [self decodePolyLine:[polylinePoints mutableCopy]]; } return nil; } - (IBAction)drawPath:(id)sender { dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ //Background Thread [self showRouteFrom:startLocation to:endLocation]; dispatch_async(dispatch_get_main_queue(), ^(void){ //Run UI Updates if (polyLine) { [__mapView setVisibleMapRect:[polyLine boundingMapRect]]; [__mapView addOverlay:polyLine]; } NSLog(@"drawing path completed"); }); }); } -(void) showRouteFrom: (id&lt;MKAnnotation&gt;)source to:(id&lt;MKAnnotation&gt;)destination; { routes = [self calculateRoutesFrom:source.coordinate to:destination.coordinate]; NSLog(@"routes are %@",routes); NSInteger numberOfSteps = routes.count; CLLocationCoordinate2D coordinates[numberOfSteps]; for (NSInteger index = 0; index &lt; numberOfSteps; index++) { CLLocation *location = [routes objectAtIndex:index]; CLLocationCoordinate2D coordinate = location.coordinate; coordinates[index] = coordinate; } polyLine = [MKPolyline polylineWithCoordinates:coordinates count:numberOfSteps]; [polyLineArray addObject:polyLine]; } - (IBAction)drawMultiple:(id)sender { polyLineArray=[[NSMutableArray alloc] init]; MKPointAnnotation *start=[[MKPointAnnotation alloc] init]; MKPointAnnotation *end=[[MKPointAnnotation alloc] init]; dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){ //Background Thread for (int i=0;i&lt;locationarry.count-1;i++) { NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i] componentsSeparatedByString:@","]]; NSDictionary *dic=[citywiselatlong objectForKey:[part objectAtIndex:0]]; [start setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:@"lat"] floatValue], [[dic valueForKey:@"lng"] floatValue])]; part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i +1 ] componentsSeparatedByString:@","]]; dic=[citywiselatlong objectForKey:[part objectAtIndex:0]]; [end setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:@"lat"] floatValue], [[dic valueForKey:@"lng"] floatValue])]; [self showRouteFrom:start to:end]; } dispatch_async(dispatch_get_main_queue(), ^(void){ //Run UI Updates NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:0] componentsSeparatedByString:@","]]; NSDictionary *tempDic=[[NSDictionary alloc] initWithDictionary:[citywiselatlong objectForKey:[part objectAtIndex:0]]]; [start setCoordinate:CLLocationCoordinate2DMake([[tempDic valueForKey:@"lat"] floatValue], [[tempDic valueForKey:@"lng"] floatValue])]; if (polyLine) { [__mapView setVisibleMapRect:[polyLine boundingMapRect]]; for (MKPolyline *pLine in polyLineArray) { [__mapView addOverlay:pLine]; } } for (int i =0 ; i&lt; [locationarry count]; i ++) { NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i] componentsSeparatedByString:@","]]; NSDictionary *dic=[citywiselatlong objectForKey:[part objectAtIndex:0]]; MKPointAnnotation *first=[[MKPointAnnotation alloc] init]; MKPointAnnotation *second=[[MKPointAnnotation alloc] init]; [first setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:@"lat"] floatValue], [[dic valueForKey:@"lng"] floatValue])]; if(i+1&lt;[locationarry count]) { NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i + 1] componentsSeparatedByString:@","]]; dic=[citywiselatlong objectForKey:[part objectAtIndex:0]]; [second setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:@"lat"] floatValue], [[dic valueForKey:@"lng"] floatValue])]; } else { NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i] componentsSeparatedByString:@","]]; dic=[citywiselatlong objectForKey:[part objectAtIndex:0]];; [second setCoordinate:CLLocationCoordinate2DMake([[dic valueForKey:@"lat"] floatValue], [[dic valueForKey:@"lng"] floatValue])]; } if (i==0) { [__mapView addAnnotation:first]; [__mapView addAnnotation:second]; } else { [__mapView addAnnotation:second]; } } NSLog(@"drawing path completed"); }); }); } -(void) centerMap { MKCoordinateRegion region; CLLocationDegrees maxLat = -90.0; CLLocationDegrees maxLon = -180.0; CLLocationDegrees minLat = 90.0; CLLocationDegrees minLon = 180.0; for(int idx = 0; idx &lt; routes.count; idx++) { CLLocation* currentLocation = [routes objectAtIndex:idx]; if(currentLocation.coordinate.latitude &gt; maxLat) maxLat = currentLocation.coordinate.latitude; if(currentLocation.coordinate.latitude &lt; minLat) minLat = currentLocation.coordinate.latitude; if(currentLocation.coordinate.longitude &gt; maxLon) maxLon = currentLocation.coordinate.longitude; if(currentLocation.coordinate.longitude &lt; minLon) minLon = currentLocation.coordinate.longitude; } region.center.latitude = (maxLat + minLat) / 2.0; region.center.longitude = (maxLon + minLon) / 2.0; region.span.latitudeDelta = 0.01; region.span.longitudeDelta = 0.01; region.span.latitudeDelta = ((maxLat - minLat)&lt;0.0)?100.0:(maxLat - minLat); region.span.longitudeDelta = ((maxLon - minLon)&lt;0.0)?100.0:(maxLon - minLon); [__mapView setRegion:region animated:YES]; } #pragma mark- UITextField delegate method -(void)locationfind { for (int i = 0; i &lt; [locationarry count]; i++) { NSDictionary *locationDic=[self getLatLongOfPlace:[[locationarry objectAtIndex:i] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]]; NSDictionary *latLongDic=[[[[locationDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"geometry"]valueForKey:@"location"]; NSArray *part = [[NSArray alloc]initWithArray:[[locationarry objectAtIndex:i] componentsSeparatedByString:@","]]; [citywiselatlong setObject:latLongDic forKey:[part objectAtIndex:0]]; NSLog(@"%@",citywiselatlong); } } #pragma mark Getting lat long from place name. -(NSDictionary*)getLatLongOfPlace:(NSString*)place { NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&amp;sensor=true",place]; NSLog(@"Fired URL:- %@",apiUrlStr); NSURL* apiUrl = [NSURL URLWithString:[apiUrlStr stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; NSError* error = nil; NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl encoding:NSASCIIStringEncoding error:&amp;error]; NSDictionary *dic=[apiResponse objectFromJSONString]; return dic; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload