title: Optimize coordinate calculations in two nested arrays
body: <p>I'm working on an application in which there is an array of coordinates on the <code>Google</code> map by which the route is drawn. There is a second array of filling stations coordinates. In two arrays of 10,000 points.  I need to show the points from the second array that fall on the route with a deviation of 5 miles. I made this requirement in nested arrays, but this calculation is very lengthy, it takes about five minutes. How can it be optimized and speed up the calculation? I will be very grateful for any advice:</p>
  
  <p><strong>Here is my code</strong></p>
  
  <pre><code>- (void)addRandomPointsOnMap {
  CGFloat upperBoundLatitude = 46.80;
  CGFloat lowerBoundLatitude = 29.76;
  CGFloat upperBoundLongitude = -118.64;
  CGFloat lowerBoundLongitude = -75.6;
  
  for (int i = 0; i &lt; 10000; i++) {
      TSPoint *randomPoint = [[TSPoint alloc] init];
      randomPoint.latitude = [self randomFloatBetween:upperBoundLatitude
                                            and:lowerBoundLatitude];
      randomPoint.longitude = [self randomFloatBetween:upperBoundLongitude
                                             and:lowerBoundLongitude];
      CLLocation *randomPointLocation = [[CLLocation alloc] initWithLatitude:randomPoint.latitude longitude:randomPoint.longitude];
  
      for (int i = 0; i &lt; _routePoints.count; i++) {
          TSPoint *routePoint = _routePoints[i];
          CLLocation *routePointLocation = [[CLLocation alloc] initWithLatitude:routePoint.latitude longitude:routePoint.longitude];
          NSInteger distanceInMeters = [routePointLocation distanceFromLocation:randomPointLocation];
  
          NSInteger distanceInMiles = distanceInMeters / 1609.344;
          if (distanceInMiles &lt; 5) {
  
              GMSMarker *markerT1 = [[GMSMarker alloc] init];
              markerT1.position = CLLocationCoordinate2DMake(randomPoint.latitude, randomPoint.longitude);
              markerT1.icon = [UIImage imageNamed:@"blue_pin"];
              markerT1.groundAnchor = CGPointMake(0.5, 0.5);
              markerT1.map = _mapView;
          }
      }
  } }
  </code></pre>
  
  <p>and the method of calculating random coordinates in bounded ranges:</p>
  
  <pre><code>- (float)randomFloatBetween:(float)lowerBound and:(float)upperBound {
  float diff = upperBound - lowerBound;
  return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + lowerBound; }
  </code></pre>