Module: ProMotion::MapScreenModule

Included in:
MapScreen
Defined in:
lib/ProMotion/map/map_screen_module.rb

Defined Under Namespace

Modules: MapClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mapviewObject

Returns the value of attribute mapview.



3
4
5
# File 'lib/ProMotion/map/map_screen_module.rb', line 3

def mapview
  @mapview
end

Class Method Details

.included(base) ⇒ Object



259
260
261
# File 'lib/ProMotion/map/map_screen_module.rb', line 259

def self.included(base)
  base.extend(MapClassMethods)
end

Instance Method Details

#add_annotation(annotation) ⇒ Object



99
100
101
102
# File 'lib/ProMotion/map/map_screen_module.rb', line 99

def add_annotation(annotation)
  @promotion_annotation_data << MapScreenAnnotation.new(annotation)
  self.mapview.addAnnotation @promotion_annotation_data.last
end

#add_annotations(annotations) ⇒ Object



104
105
106
107
# File 'lib/ProMotion/map/map_screen_module.rb', line 104

def add_annotations(annotations)
  @promotion_annotation_data = Array(annotations).map{|a| MapScreenAnnotation.new(a)}
  self.mapview.addAnnotations @promotion_annotation_data
end

#annotation_view(map_view, annotation) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ProMotion/map/map_screen_module.rb', line 116

def annotation_view(map_view, annotation)
  return if annotation.is_a? MKUserLocation

  params = annotation.params

  identifier = params[:identifier]
  if view = map_view.dequeueReusableAnnotationViewWithIdentifier(identifier)
    view.annotation = annotation
  else
    # Set the pin properties
    if params[:image]
      view = MKAnnotationView.alloc.initWithAnnotation(annotation, reuseIdentifier:identifier)
    else
      view = MKPinAnnotationView.alloc.initWithAnnotation(annotation, reuseIdentifier:identifier)
    end
  end
  view.image = params[:image] if view.respond_to?("image=") && params[:image]
  view.animatesDrop = params[:animates_drop] if view.respond_to?("animatesDrop=")
  view.pinColor = params[:pin_color] if view.respond_to?("pinColor=")
  view.canShowCallout = params[:show_callout] if view.respond_to?("canShowCallout=")

  if params[:left_accessory]
    view.leftCalloutAccessoryView = params[:left_accessory]
  end
  if params[:right_accessory]
    view.rightCalloutAccessoryView = params[:right_accessory]
  end

  if params[:action]
    button_type = params[:action_button_type] || UIButtonTypeDetailDisclosure

    action_button = UIButton.buttonWithType(button_type)
    action_button.addTarget(self, action: params[:action], forControlEvents:UIControlEventTouchUpInside)

    view.rightCalloutAccessoryView = action_button
  end
  view
end

#annotationsObject



75
76
77
# File 'lib/ProMotion/map/map_screen_module.rb', line 75

def annotations
  @promotion_annotation_data
end

#centerObject



34
35
36
# File 'lib/ProMotion/map/map_screen_module.rb', line 34

def center
  self.mapview.centerCoordinate
end

#center=(params = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ProMotion/map/map_screen_module.rb', line 38

def center=(params={})
  PM.logger.error "Missing #:latitude property in call to #center=." unless params[:latitude]
  PM.logger.error "Missing #:longitude property in call to #center=." unless params[:longitude]
  params[:animated] ||= true

  # Set the new region
  self.mapview.setCenterCoordinate(
    CLLocationCoordinate2D.new(params[:latitude], params[:longitude]),
    animated:params[:animated]
  )
end

#check_annotation_dataObject



21
22
23
# File 'lib/ProMotion/map/map_screen_module.rb', line 21

def check_annotation_data
  PM.logger.error "Missing #annotation_data method in MapScreen #{self.class.to_s}." unless self.respond_to?(:annotation_data)
end

#clear_annotationsObject



109
110
111
112
113
114
# File 'lib/ProMotion/map/map_screen_module.rb', line 109

def clear_annotations
  @promotion_annotation_data.each do |a|
    self.mapview.removeAnnotation(a)
  end
  @promotion_annotation_data = []
end

#deselect_annotations(animated = false) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/ProMotion/map/map_screen_module.rb', line 91

def deselect_annotations(animated=false)
  unless selected_annotations.nil?
    selected_annotations.each do |annotation|
      self.mapview.deselectAnnotation(annotation, animated:animated)
    end
  end
end

#hide_user_locationObject



54
55
56
# File 'lib/ProMotion/map/map_screen_module.rb', line 54

def hide_user_location
  set_show_user_location false
end

#look_up_address(args = {}, &callback) ⇒ Object



223
224
225
226
227
228
229
230
# File 'lib/ProMotion/map/map_screen_module.rb', line 223

def look_up_address(args={}, &callback)
  args[:address] = args if args.is_a? String # Assume if a string is passed that they want an address

  geocoder = CLGeocoder.new
  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

#mapObject



30
31
32
# File 'lib/ProMotion/map/map_screen_module.rb', line 30

def map
  self.mapview
end

#mapView(map_view, didUpdateUserLocation: userLocation) ⇒ Object

Cocoa touch methods #################



233
234
235
# File 'lib/ProMotion/map/map_screen_module.rb', line 233

def mapView(map_view, viewForAnnotation:annotation)
  annotation_view(map_view, annotation)
end

#region(params) ⇒ Object



212
213
214
215
216
217
218
219
220
221
# File 'lib/ProMotion/map/map_screen_module.rb', line 212

def region(params)
  return nil unless params.is_a? Hash

  params[:coordinate] = CLLocationCoordinate2D.new(params[:coordinate][:latitude], params[:coordinate][:longitude]) if params[:coordinate].is_a? Hash
  params[:span] = MKCoordinateSpanMake(params[:span][0], params[:span][1]) if params[:span].is_a? Array

  if params[:coordinate] && params[:span]
    MKCoordinateRegionMake( params[:coordinate], params[:span] )
  end
end

#screen_setupObject



5
6
7
8
9
10
11
12
13
14
# File 'lib/ProMotion/map/map_screen_module.rb', line 5

def screen_setup
  self.mapview ||= add MKMapView.new, {
    frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height),
    delegate: self
  }

  check_annotation_data
  @promotion_annotation_data = []
  set_up_start_position
end

#select_annotation(annotation, animated = true) ⇒ Object



79
80
81
# File 'lib/ProMotion/map/map_screen_module.rb', line 79

def select_annotation(annotation, animated=true)
  self.mapview.selectAnnotation(annotation, animated:animated)
end

#select_annotation_at(annotation_index, animated = true) ⇒ Object



83
84
85
# File 'lib/ProMotion/map/map_screen_module.rb', line 83

def select_annotation_at(annotation_index, animated=true)
  select_annotation(annotations[annotation_index], animated:animated)
end

#selected_annotationsObject



87
88
89
# File 'lib/ProMotion/map/map_screen_module.rb', line 87

def selected_annotations
  self.mapview.selectedAnnotations
end

#set_region(region, animated = true) ⇒ Object



208
209
210
# File 'lib/ProMotion/map/map_screen_module.rb', line 208

def set_region(region, animated=true)
  self.mapview.setRegion(region, animated:animated)
end

#set_show_user_location(show) ⇒ Object



58
59
60
# File 'lib/ProMotion/map/map_screen_module.rb', line 58

def set_show_user_location(show)
  self.mapview.showsUserLocation = show
end

#set_start_position(params = {}) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ProMotion/map/map_screen_module.rb', line 155

def set_start_position(params={})
  params[:latitude] ||= 37.331789
  params[:longitude] ||= -122.029620
  params[:radius] ||= 10

  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)
  set_region(region, animated:false)
end

#set_up_start_positionObject



167
168
169
170
171
# File 'lib/ProMotion/map/map_screen_module.rb', line 167

def set_up_start_position
  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

#show_user_locationObject



50
51
52
# File 'lib/ProMotion/map/map_screen_module.rb', line 50

def show_user_location
  set_show_user_location true
end

#showing_user_location?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/ProMotion/map/map_screen_module.rb', line 62

def showing_user_location?
  self.mapview.showsUserLocation
end

#update_annotation_dataObject



25
26
27
28
# File 'lib/ProMotion/map/map_screen_module.rb', line 25

def update_annotation_data
  clear_annotations
  add_annotations annotation_data
end

#user_locationObject



66
67
68
# File 'lib/ProMotion/map/map_screen_module.rb', line 66

def user_location
  self.mapview.userLocation.location.nil? ? nil : self.mapview.userLocation.location.coordinate
end

#view_will_appear(animated) ⇒ Object



16
17
18
19
# File 'lib/ProMotion/map/map_screen_module.rb', line 16

def view_will_appear(animated)
  super
  update_annotation_data
end

#zoom_to_fit_annotations(animated = true) ⇒ Object

TODO: Why is this so complex?



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/ProMotion/map/map_screen_module.rb', line 174

def zoom_to_fit_annotations(animated=true)
  #Don't attempt the rezoom of there are no pins
  return if annotations.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|
    topLeft.longitude = [topLeft.longitude, a.coordinate.longitude].min
    topLeft.latitude = [topLeft.latitude, a.coordinate.latitude].max
    bottomRight.longitude = [bottomRight.longitude, a.coordinate.longitude].max
    bottomRight.latitude = [bottomRight.latitude, a.coordinate.latitude].min
  end

  #Find the bounds of all the pins and set the map_view
  coord = CLLocationCoordinate2D.new(
    topLeft.latitude - (topLeft.latitude - bottomRight.latitude) * 0.5,
    topLeft.longitude + (bottomRight.longitude - topLeft.longitude) * 0.5
  )

  # Add some padding to the edges
  span = MKCoordinateSpanMake(
    ((topLeft.latitude - bottomRight.latitude) * 1.075).abs,
    ((bottomRight.longitude - topLeft.longitude) * 1.075).abs
  )

  region = MKCoordinateRegionMake(coord, span)
  fits = self.mapview.regionThatFits(region);

  set_region(fits, animated:animated)
end

#zoom_to_user(radius = 0.05, animated = true) ⇒ Object



70
71
72
73
# File 'lib/ProMotion/map/map_screen_module.rb', line 70

def zoom_to_user(radius = 0.05, animated=true)
  show_user_location unless showing_user_location?
  set_region(MKCoordinateRegionMake(user_location, [radius, radius]), animated)
end