Method: MapKit::ZoomLevel::ClassMethods#coordinate_region_with_map_view
- Defined in:
- lib/map-kit-wrapper/zoom_level.rb
#coordinate_region_with_map_view(map_view, center_coordinate, zoom_level) ⇒ Object
Get the coordiante region for the given zoom level
This would involve wrapping the map from top to bottom, something that a Mercator projection just cannot do.
-
Args :
-
map_view-> A MKMapView -
center_coordinates-> Coordinates as MKMapPoint -
zoom_level-> Zoom level as Int
-
-
Returns :
-
Region as MKCoordinateRegion
-
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/map-kit-wrapper/zoom_level.rb', line 141 def coordinate_region_with_map_view(map_view, center_coordinate, zoom_level) # clamp lat/long values to appropriate ranges center_coordinate.latitude = [[-90.0, center_coordinate.latitude].max, 90.0].min center_coordinate.longitude = center_coordinate.longitude % 180.0 # 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 left pixel top_left_pixel_x = center_pixel_x - (scaled_map_width / 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 # if we’re at a pole then calculate the distance from the pole towards the equator # as MKMapView doesn’t like drawing boxes over the poles top_pixel_y = center_pixel_y - (scaled_map_height / 2) bottom_pixel_y = center_pixel_y + (scaled_map_height / 2) adjusted_center_point = false if top_pixel_y > MERCATOR_OFFSET * 2 top_pixel_y = center_pixel_y - scaled_map_height bottom_pixel_y = MERCATOR_OFFSET * 2 adjusted_center_point = true end # find delta between top and bottom latitudes min_lat = self.pixel_space_y_to_latitude(top_pixel_y) max_lat = self.pixel_space_y_to_latitude(bottom_pixel_y) latitude_delta = -1 * (max_lat - min_lat) # create and return the lat/lng span span = MKCoordinateSpanMake(latitude_delta, longitude_delta) region = MKCoordinateRegionMake(center_coordinate, span) # once again, MKMapView doesn’t like drawing boxes over the poles # so adjust the center coordinate to the center of the resulting region if adjusted_center_point region.center.latitude = self.pixel_space_y_to_latitude((bottom_pixel_y + top_pixel_y) / 2.0) end region end |