Method: MapKit::ZoomLevel::ClassMethods#coordinate_span_with_map_view

Defined in:
lib/map-kit-wrapper/zoom_level.rb

#coordinate_span_with_map_view(map_view, center_coordinate, zoom_level) ⇒ Object

Get the coordiante span for the given zoom level

  • Args :

    • map_view -> A MKMapView

    • center_coordinates -> Coordinates as MKMapPoint

    • zoom_level -> Zoom level as Int

  • Returns :

    • Span as MKCoordinateSpan



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/map-kit-wrapper/zoom_level.rb', line 96

def coordinate_span_with_map_view(map_view, center_coordinate, zoom_level)
  # convert center coordiate to pixel space
  center_pixel_x = self.longitude_to_pixel_space_x(center_coordinate.longitude)
  center_pixel_y = self.latitude_to_pixel_space_y(center_coordinate.latitude)

  # determine the scale value from the zoom level
  zoom_exponent = 20 - zoom_level
  zoom_scale = 2 ** zoom_exponent

  # scale the map’s size in pixel space
  map_size_in_pixels = map_view.bounds.size
  scaled_map_width = map_size_in_pixels.width * zoom_scale
  scaled_map_height = map_size_in_pixels.height * zoom_scale

  # figure out the position of the top-left pixel
  top_left_pixel_x = center_pixel_x - (scaled_map_width / 2)
  top_left_pixel_y = center_pixel_y - (scaled_map_height / 2)

  # find delta between left and right longitudes
  min_lng = self.pixel_space_x_to_longitude(top_left_pixel_x)
  max_lng = self.pixel_space_x_to_longitude(top_left_pixel_x + scaled_map_width)
  longitude_delta = max_lng - min_lng

  # find delta between top and bottom latitudes
  min_lat = self.pixel_space_y_to_latitude(top_left_pixel_y)
  max_lat = self.pixel_space_y_to_latitude(top_left_pixel_y + scaled_map_height)
  latitude_delta = -1 * (max_lat - min_lat)

  # create and return the lat/lng span
  MKCoordinateSpanMake(latitude_delta, longitude_delta)
end