daily pastebin goal
20%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //find rect that encloses all coords
  2.  
  3. float maxLat = -200;
  4. float maxLong = -200;
  5. float minLat = MAXFLOAT;
  6. float minLong = MAXFLOAT;
  7.  
  8. for (int i=0 ; i<[locations count] ; i++) {
  9.     CLLocationCoordinate2D location = [locations objectAtIndex:i];
  10.  
  11.     if (location.latitude < minLat) {
  12.         minLat = location.latitude;
  13.     }
  14.  
  15.     if (location.longitude < minLong) {
  16.         minLong = location.longitude;
  17.     }
  18.  
  19.     if (location.latitude > maxLat) {
  20.         maxLat = location.latitude;
  21.     }
  22.  
  23.     if (location.longitude > maxLong) {
  24.         maxLong = location.longitude;
  25.     }
  26. }
  27.  
  28. //Center point
  29.  
  30. CLLocationCoordinate2D center = CLLocationCoordinate2DMake((maxLat + minLat) * 0.5, (maxLong + minLong) * 0.5);
  31.    
  32. . = point
  33. X = centre
  34.  
  35. .....               X                   .
  36.    
  37. CLLocationDegrees minLat,minLng,maxLat,maxLng;
  38. for(CLLocationCoordinate2D coordinate in coordinates) {
  39.     minLat = MIN(minLat, coordinate.latitude);
  40.     minLng = MIN(minLng, coordinate.longitude);
  41.  
  42.     maxLat = MAX(maxLat, coordinate.latitude);
  43.     maxLng = MAX(maxLng, coordinate.longitude);
  44. }
  45.  
  46. CLLocationCoordinate2D coordinateOrigin = CLLocationCoordinate2DMake(minLat, minLng);
  47. CLLocationCoordinate2D coordinateMax = CLLocationCoordinate2DMake(maxLat, maxLng);
  48.  
  49. MKMapPoint upperLeft = MKMapPointForCoordinate(coordinateOrigin);
  50. MKMapPoint lowerRight = MKMapPointForCoordinate(coordinateMax);
  51.  
  52. //Create the map rect
  53. MKMapRect mapRect = MKMapRectMake(upperLeft.x,
  54.                                   upperLeft.y,
  55.                                   lowerRight.x - upperLeft.x,
  56.                                   lowerRight.y - upperLeft.y);
  57.  
  58. //Create the region
  59. MKCoordinateRegion region = MKCoordinateRegionForMapRect(mapRect);
  60.  
  61. //THIS HAS THE CENTER, it should include spread
  62. CLLocationCoordinate2D centerCoordinate = region.center;
  63.    
  64. //find rect that encloses all coords
  65.  
  66. float maxLat = -200;
  67. float maxLong = -200;
  68. float minLat = MAXFLOAT;
  69. float minLong = MAXFLOAT;
  70.  
  71. for (int i=0 ; i<[locations count] ; i++) {
  72. CLLocationCoordinate2D location = [locations objectAtIndex:i];
  73.  
  74.     if (location.latitude < minLat) {
  75.         minLat = location.latitude;
  76.     }
  77.  
  78.     if (location.longitude < minLong) {
  79.         minLong = location.longitude;
  80.     }
  81.  
  82.     if (location.latitude > maxLat) {
  83.         maxLat = location.latitude;
  84.     }
  85.  
  86.     if (location.longitude > maxLong) { //Change to be greater than
  87.         maxLong = location.longitude;
  88.     }
  89. }
  90.  
  91. //Center point
  92. //The min's and max's should be ADDED not subtracted
  93. CLLocationCoordinate2D center = CLLocationCoordinate2DMake((maxLat + minLat) * 0.5, (maxLong + minLong) * 0.5);
  94.    
  95. extension MKCoordinateRegion {
  96.  
  97.   init(coordinates: [CLLocationCoordinate2D]) {
  98.     var minLatitude: CLLocationDegrees = 90.0
  99.     var maxLatitude: CLLocationDegrees = -90.0
  100.     var minLongitude: CLLocationDegrees = 180.0
  101.     var maxLongitude: CLLocationDegrees = -180.0
  102.  
  103.     for coordinate in coordinates {
  104.       let lat = Double(coordinate.latitude)
  105.       let long = Double(coordinate.longitude)
  106.       if lat < minLatitude {
  107.         minLatitude = lat
  108.       }
  109.       if long < minLongitude {
  110.         minLongitude = long
  111.       }
  112.       if lat > maxLatitude {
  113.         maxLatitude = lat
  114.       }
  115.       if long > maxLongitude {
  116.         maxLongitude = long
  117.       }
  118.     }
  119.  
  120.     let span = MKCoordinateSpanMake(maxLatitude - minLatitude, maxLongitude - minLongitude)
  121.     let center = CLLocationCoordinate2DMake((maxLatitude - span.latitudeDelta / 2), (maxLongitude - span.longitudeDelta / 2))
  122.     self.init(center: center, span: span)
  123.   }
  124. }
  125.    
  126. let region = MKCoordinateRegion(coordinates: coordinates)
  127. region.center
  128. region.span
  129.    
  130. MKCoordinateRegion mapRegion;  
  131.         mapRegion.center = locationCoordinates;
  132.             mapRegion.span.latitudeDelta = 0.2;
  133.             mapRegion.span.longitudeDelta =0.2;
  134.  
  135.          mapRegion = [self.mymap regionThatFits:mapRegion];
  136.         [mymap setRegion:mapRegion animated: YES];
RAW Paste Data
Top