lib/ProMotion/map/map_screen_module.rb in ProMotion-map-0.5.0 vs lib/ProMotion/map/map_screen_module.rb in ProMotion-map-0.6.0

- old
+ new

@@ -7,10 +7,11 @@ self.view.delegate = self check_annotation_data @promotion_annotation_data = [] set_up_start_position + set_up_tap_to_add end def view_will_appear(animated) super update_annotation_data @@ -158,13 +159,15 @@ end view 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 initialLocation = CLLocationCoordinate2D.new(params[:latitude], params[:longitude]) region = MKCoordinateRegionMakeWithDistance(initialLocation, params[:radius] * meters_per_mile, params[:radius] * meters_per_mile) @@ -175,10 +178,46 @@ if self.class.respond_to?(:get_start_position) && self.class.get_start_position self.set_start_position self.class.get_start_position_params end end + def set_tap_to_add(params={}) + params = { + length: 2.0, + target: self, + action: "gesture_drop_pin:", + annotation: { + title: "Dropped Pin", + animates_drop: true + } + }.merge(params) + @tap_to_add_annotation_params = params[:annotation] + + 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 args = {animated: args} if args == true || args == false args = {animated: true, include_user: false}.merge(args) @@ -240,10 +279,22 @@ return geocoder.geocodeAddressDictionary(args[:address], completionHandler: callback) if args[:address].is_a?(Hash) return geocoder.geocodeAddressString(args[:address].to_s, completionHandler: callback) unless args[:region] return geocoder.geocodeAddressString(args[:address].to_s, inRegion:args[:region].to_s, completionHandler: callback) if args[:region] end + def look_up_location(location, &callback) + location = CLLocation.alloc.initWithLatitude(location.latitude, longitude:location.longitude) if location.is_a?(CLLocationCoordinate2D) + + if location.kind_of?(CLLocation) + geocoder = CLGeocoder.new + geocoder.reverseGeocodeLocation(location, completionHandler: callback) + else + PM.logger.info("You're trying to reverse geocode something that isn't a CLLocation") + callback.call nil, nil + end + end + ########## Cocoa touch methods ################# def mapView(map_view, viewForAnnotation:annotation) annotation_view(map_view, annotation) end @@ -274,10 +325,11 @@ map.send("#{meth}Enabled=", argument) end end module MapClassMethods + # Start Position def start_position(params={}) @start_position_params = params @start_position = true end @@ -286,10 +338,26 @@ end 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) end @@ -298,9 +366,8 @@ def location_manager @location_manager ||= CLLocationManager.alloc.init @location_manager.delegate ||= self @location_manager end - end end