From 4cf5f48f1a5a4055643f1154007d82e80b074c6a Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Fri, 6 Mar 2015 19:19:18 -0500 Subject: [PATCH 1/8] Adds tap_to_add functionality --- lib/ProMotion/map/map_screen_module.rb | 51 ++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/lib/ProMotion/map/map_screen_module.rb b/lib/ProMotion/map/map_screen_module.rb index b0a585c..314203e 100644 --- a/lib/ProMotion/map/map_screen_module.rb +++ b/lib/ProMotion/map/map_screen_module.rb @@ -9,6 +9,7 @@ def screen_setup check_annotation_data @promotion_annotation_data = [] set_up_start_position + set_up_tap_to_add end def view_will_appear(animated) @@ -177,6 +178,39 @@ def set_up_start_position end end + def set_tap_to_add(params={}) + params[:length] ||= 2.0 + params[:target] ||= self + params[:action] ||= "gesture_drop_pin:" + @tap_to_add_annotation_params = (params[:annotation] || {}).merge({ + title: "Dropped Pin", + animates_drop: true + }) + + lpgr = UILongPressGestureRecognizer.alloc.initWithTarget(params[:target], action:params[:action]) + lpgr.minimumPressDuration = params[:length] + self.view.addGestureRecognizer(lpgr) + end + + def gesture_drop_pin(gesture_recognizer) + if gesture_recognizer.state == UIGestureRecognizerStateBegan + NSNotificationCenter.defaultCenter.postNotificationName("ProMotionMapWillAddPin", object:nil) + touch_point = gesture_recognizer.locationInView(self.view) + touch_map_coordinate = self.view.convertPoint(touch_point, toCoordinateFromView:self.view) + + add_annotation({ + coordinate: touch_map_coordinate + }.merge(@tap_to_add_annotation_params)) + NSNotificationCenter.defaultCenter.postNotificationName("ProMotionMapAddedPin", object:@promotion_annotation_data.last) + end + end + + def set_up_tap_to_add + if self.class.respond_to?(:get_tap_to_add) && self.class.get_tap_to_add + self.set_tap_to_add self.class.get_tap_to_add_params + end + end + # TODO: Why is this so complex? def zoom_to_fit_annotations(args={}) # Preserve backwards compatibility @@ -276,6 +310,7 @@ def type=(type) end module MapClassMethods + # Start Position def start_position(params={}) @start_position_params = params @start_position = true @@ -288,6 +323,22 @@ def get_start_position_params def get_start_position @start_position ||= false end + + # Tap to drop pin + def tap_to_add(params={}) + @tap_to_add_params = params + @tap_to_add = true + end + + def get_tap_to_add_params + @tap_to_add_params ||= nil + end + + def get_tap_to_add + @tap_to_add ||= false + end + + end def self.included(base) base.extend(MapClassMethods) From fe7097d9106628463209759eacdd9e51b4d82454 Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Sat, 7 Mar 2015 09:04:15 -0500 Subject: [PATCH 2/8] Rename spec to be mroe descriptive of what it does. --- spec/func_map_screen_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/func_map_screen_spec.rb b/spec/func_map_screen_spec.rb index 3746a3c..c626eb8 100644 --- a/spec/func_map_screen_spec.rb +++ b/spec/func_map_screen_spec.rb @@ -35,7 +35,7 @@ def add_image_annotation map_screen.navigationController.should.be.kind_of(UINavigationController) end - it "should have the map properly centered" do + it "should start the map in the correct location" do center_coordinate = map_screen.center center_coordinate.latitude.should.be.close 35.090648651123, 0.02 center_coordinate.longitude.should.be.close -82.965972900391, 0.02 From c51a26c04388e967c4515f2d26cb4a82430b3868 Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Sat, 7 Mar 2015 09:04:42 -0500 Subject: [PATCH 3/8] Fix .merge() reversal --- lib/ProMotion/map/map_screen_module.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/ProMotion/map/map_screen_module.rb b/lib/ProMotion/map/map_screen_module.rb index 314203e..0dc3a58 100644 --- a/lib/ProMotion/map/map_screen_module.rb +++ b/lib/ProMotion/map/map_screen_module.rb @@ -179,13 +179,16 @@ def set_up_start_position end def set_tap_to_add(params={}) - params[:length] ||= 2.0 - params[:target] ||= self - params[:action] ||= "gesture_drop_pin:" - @tap_to_add_annotation_params = (params[:annotation] || {}).merge({ + params = { + length: 2.0, + target: self, + action: "gesture_drop_pin:" + }.merge(params) + + @tap_to_add_annotation_params = { title: "Dropped Pin", animates_drop: true - }) + }.merge(params[:annotation] || {}) lpgr = UILongPressGestureRecognizer.alloc.initWithTarget(params[:target], action:params[:action]) lpgr.minimumPressDuration = params[:length] From 81771c45c17248df0e8568f9a6112fcec95b5ba2 Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Sat, 7 Mar 2015 09:05:14 -0500 Subject: [PATCH 4/8] Better default parameter setting --- lib/ProMotion/map/map_screen_module.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/ProMotion/map/map_screen_module.rb b/lib/ProMotion/map/map_screen_module.rb index 0dc3a58..76b0cb3 100644 --- a/lib/ProMotion/map/map_screen_module.rb +++ b/lib/ProMotion/map/map_screen_module.rb @@ -161,9 +161,11 @@ def annotation_view(map_view, annotation) end def set_start_position(params={}) - params[:latitude] ||= 37.331789 - params[:longitude] ||= -122.029620 - params[:radius] ||= 10 + params = { + latitude: 37.331789 + longitude: -122.029620 + radius: 10 + }.merge(params) meters_per_mile = 1609.344 @@ -355,6 +357,5 @@ def location_manager @location_manager end - end end From 4b630dcc1f4b87aaa6cdab9c5930b1426ca44e77 Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Sat, 7 Mar 2015 09:05:26 -0500 Subject: [PATCH 5/8] Adds tap_to_add to the TestMapScreen. --- app/test_screens/test_map_screen.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/test_screens/test_map_screen.rb b/app/test_screens/test_map_screen.rb index 89e3b37..a114025 100644 --- a/app/test_screens/test_map_screen.rb +++ b/app/test_screens/test_map_screen.rb @@ -3,6 +3,7 @@ class TestMapScreen < PM::MapScreen start_position latitude: 35.090648651123, longitude: -82.965972900391, radius: 4 title "Gorges State Park, NC" + tap_to_add length: 1.5, annotation: {animates_drop: false, title: "A new park?"} def on_load @action_called = false From 23161c0552cec2ee00051b3045669ca72d6d504a Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Sat, 7 Mar 2015 09:22:31 -0500 Subject: [PATCH 6/8] Fix syntax issue --- lib/ProMotion/map/map_screen_module.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ProMotion/map/map_screen_module.rb b/lib/ProMotion/map/map_screen_module.rb index 76b0cb3..b3d7041 100644 --- a/lib/ProMotion/map/map_screen_module.rb +++ b/lib/ProMotion/map/map_screen_module.rb @@ -162,8 +162,8 @@ def annotation_view(map_view, annotation) def set_start_position(params={}) params = { - latitude: 37.331789 - longitude: -122.029620 + latitude: 37.331789, + longitude: -122.029620, radius: 10 }.merge(params) From 676f0cfe70e4aaa3a71e3bf954a97ad4d0c1e7e9 Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Sat, 7 Mar 2015 09:25:02 -0500 Subject: [PATCH 7/8] Better setting of tap_to_add defaults. --- lib/ProMotion/map/map_screen_module.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/ProMotion/map/map_screen_module.rb b/lib/ProMotion/map/map_screen_module.rb index b3d7041..1c0c44c 100644 --- a/lib/ProMotion/map/map_screen_module.rb +++ b/lib/ProMotion/map/map_screen_module.rb @@ -184,13 +184,13 @@ def set_tap_to_add(params={}) params = { length: 2.0, target: self, - action: "gesture_drop_pin:" + action: "gesture_drop_pin:", + annotation: { + title: "Dropped Pin", + animates_drop: true + } }.merge(params) - - @tap_to_add_annotation_params = { - title: "Dropped Pin", - animates_drop: true - }.merge(params[:annotation] || {}) + @tap_to_add_annotation_params = params[:annotation] lpgr = UILongPressGestureRecognizer.alloc.initWithTarget(params[:target], action:params[:action]) lpgr.minimumPressDuration = params[:length] From 8e41956cee48e2b7ad1d8be7805989cbb1427633 Mon Sep 17 00:00:00 2001 From: Mark Rickert Date: Sat, 7 Mar 2015 09:25:16 -0500 Subject: [PATCH 8/8] Adds tap_to_add documentation in the readme. --- README.md | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/README.md b/README.md index f4fa2cc..4b32b4f 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Easily create a map screen, complete with annotations. class MyMapScreen < PM::MapScreen title "My Map" start_position latitude: 35.090648651123, longitude: -82.965972900391, radius: 4 + tap_to_add def annotation_data [{ @@ -231,6 +232,75 @@ end `radius` is the zoom level of the map in miles (default: 10). +#### tap_to_add(length: Float, target: Object, action: Selector, annotation: Hash) + +Lets a user long press the map to drop an annotation where they pressed. + +##### Default values: + +You can override any of these values. The `annotation` parameter can take any options specified in the annotation documentation above except `:latitude`, `:longitude`, and `:coordinate`. + +```ruby +length: 2.0, +target: self, +action: "gesture_drop_pin:", +annotation: { + title: "Dropped Pin", + animates_drop: true +} +``` + +##### Notifications + +This feature posts two different `NSNotificationCenter` notifications: + +**ProMotionMapWillAddPin:** Fired the moment the long press gesture is recognized, before the pin is added. + +**ProMotionMapAddedPin:** Fired after the pin has been added to the map. + +##### Example: + +```ruby +# Simple Example +class MyMapScreen < PM::MapScreen + title "My Map Screen" + tap_to_add length: 1.5 + def annotations + [] + end +end +``` + +```ruby +# A More Complex Example +class MyMapScreen < PM::MapScreen + title "My Map Screen" + tap_to_add length: 1.5, annotation: {animates_drop: true, title: "A Cool New Pin"} + def annotations + [] + end + + def will_appear + NSNotificationCenter.defaultCenter.addObserver(self, selector:"pin_adding:") , name:"ProMotionMapWillAddPin", object:nil) + NSNotificationCenter.defaultCenter.addObserver(self, selector:"pin_added:") , name:"ProMotionMapAddedPin", object:nil) + end + + def will_disappear + NSNotificationCenter.defaultCenter.removeObserver(self) + end + + def pin_adding(notification) + # We only want one pin on the map at a time + clear_annotations + end + + def pin_added(notification) + # Once the pin is dropped we want to select it + select_annotation_at(0) + end +end +``` + --- ### CocoaTouch Property Convenience Methods