diff --git a/README.md b/README.md index 8a7c65e..0de3ab6 100644 --- a/README.md +++ b/README.md @@ -177,9 +177,9 @@ Adds more than one annotation at a time to the map. Removes all annotations from the `MapScreen`. -#### zoom_to_fit_annotations(animated=true) +#### zoom_to_fit_annotations({animated:true, include_user:false}) -Changes the zoom and center point of the `MapScreen` to fit all the annotations. +Changes the zoom and center point of the `MapScreen` to fit all the annotations. Passing `include_user` as `true` will cause the zoom to not only include the annotations from `annotation_data` but also the user pin in the zoom region calculation. #### set_region(region, animated=true) diff --git a/lib/ProMotion/map/map_screen_module.rb b/lib/ProMotion/map/map_screen_module.rb index b04f25a..b0a585c 100644 --- a/lib/ProMotion/map/map_screen_module.rb +++ b/lib/ProMotion/map/map_screen_module.rb @@ -67,7 +67,11 @@ def showing_user_location? end def user_location - self.view.userLocation.location.nil? ? nil : self.view.userLocation.location.coordinate + user_annotation.nil? ? nil : user_annotation.coordinate + end + + def user_annotation + self.view.userLocation.location.nil? ? nil : self.view.userLocation.location end def zoom_to_user(radius = 0.05, animated=true) @@ -174,16 +178,22 @@ def set_up_start_position end # TODO: Why is this so complex? - def zoom_to_fit_annotations(animated=true) + def zoom_to_fit_annotations(args={}) + # Preserve backwards compatibility + args = {animated: args} if args == true || args == false + args = {animated: true, include_user: false}.merge(args) + + ann = args[:include_user] ? (annotations + [user_annotation]).compact : annotations + #Don't attempt the rezoom of there are no pins - return if annotations.count == 0 + return if ann.count == 0 #Set some crazy boundaries topLeft = CLLocationCoordinate2D.new(-90, 180) bottomRight = CLLocationCoordinate2D.new(90, -180) #Find the bounds of the pins - annotations.each do |a| + ann.each do |a| topLeft.longitude = [topLeft.longitude, a.coordinate.longitude].min topLeft.latitude = [topLeft.latitude, a.coordinate.latitude].max bottomRight.longitude = [bottomRight.longitude, a.coordinate.longitude].max @@ -203,9 +213,9 @@ def zoom_to_fit_annotations(animated=true) ) region = MKCoordinateRegionMake(coord, span) - fits = self.view.regionThatFits(region); + fits = self.view.regionThatFits(region) - set_region(fits, animated:animated) + set_region(fits, animated: args[:animated]) end def set_region(region, animated=true)