map-kit-wrapper 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  MapKitWrapper
2
2
  =============
3
3
 
4
- This is a MapKit wrapper for RubyMotion. It's purpose is to make make dealing with MapKit less painfull
4
+ This is a MapKit wrapper for RubyMotion. It's purpose is to make make dealing with MapKit less painful.
5
5
 
6
- It is still work in progress, but right now there are wrappers for the Map Kit Data Types and the Core Location Data Types. Those will save you a lot of typing.
6
+ MapKitWrapper is work in progress. Right now there are wrappers for the Map Kit Data Types and the Core Location Data Types. Those will save you a lot of typing.
7
7
 
8
8
  ## Installation
9
9
  ```ruby
@@ -33,14 +33,248 @@ def loadView
33
33
  # map.set_region(region, :animated => true)
34
34
  map.showsUserLocation = true
35
35
  self.view.addSubview(map)
36
+ map.set_zoom_level = 3
36
37
  end
37
- ```
38
- Check if the users location has been found
38
+ ```
39
+
40
+ ## MapView: Convenient subclass of MKMapView
41
+
42
+ Include the module
43
+ ```ruby
44
+ include MapKit
45
+ ```
46
+
47
+ Initializer
48
+
49
+ ```ruby
50
+ MapView.new
51
+ ```
52
+
53
+ ### Methods on MapView
54
+
55
+ Zoom methods
56
+
57
+ ```ruby
58
+ >> map = MapView.new
59
+ => MapView
60
+ >> map.zoom_enabled?
61
+ => false
62
+ >> map.zoom_enabled = true
63
+ => true
64
+ ```
65
+
66
+ Scroll methods
67
+
68
+ ```ruby
69
+ >> map = MapView.new
70
+ => MapView
71
+ >> map.scroll_enabled?
72
+ => false
73
+ >> map.scroll_enabled = true
74
+ => true
75
+ ```
76
+
77
+ Location methods
78
+
79
+ ```ruby
80
+ >> map.user_located?
81
+ => false
82
+ >> map.shows_user_location?
83
+ => false
84
+ >> map.user_coordinates
85
+ => nil
86
+ >> map.shows_user_location = true
87
+ => true
88
+ # wait for a bit
89
+ >> map.user_located?
90
+ => true
91
+ >> map.user_coordinates
92
+ => LocationCoordinate
93
+ ```
94
+
95
+ ### MapView zoom level methods
96
+
97
+ MapView includes calculations to easily get and set the zoom level as seen on Google Maps.
98
+ It's a Ruby adaption of http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/
99
+
100
+ ```ruby
101
+ >> map.zoom_level
102
+ => 5
103
+ >> map.set_zoom_level(13)
104
+ => 13
105
+ # set the zoom level with animation
106
+ >> map.set_zoom_level(13, true)
107
+ => 13
108
+ ```
109
+
110
+ ## Wrappers for the CoreLocation data types
111
+
112
+ Include the module
113
+ ```ruby
114
+ include CoreLocation::DataTypes
115
+ ```
116
+
117
+ ### LocationCoordinate: The wrapper for CLLocationCoordinate2D
118
+
119
+ The `LocationCoordinate` initializer takes a variety of argument types
120
+ ```ruby
121
+ LocationCoordinate.new(1,2)
122
+ LocationCoordinate.new([1,2])
123
+ LocationCoordinate.new(LocationCoordinate)
124
+ LocationCoordinate.new(CLLocationCoordinate2D)
125
+ ```
126
+
127
+ Methods on `LocationCoordinate`
128
+ ```ruby
129
+ >> lc = LocationCoordinate.new(1, 2)
130
+ => [1, 2]
131
+ >> lc.latitude
132
+ => 1
133
+ >> lc.latitude = 10
134
+ => 10
135
+ >> lc.longitude
136
+ => 2
137
+ >> lc.longitude = 15
138
+ => 15
139
+ >> lc.to_array
140
+ => [10, 15]
141
+ >> lc.sdk
142
+ => CLLocationCoordinate2D
143
+ ```
144
+
145
+ ## Wrappers for the MapKit data types
146
+
147
+ Include the module
148
+
149
+ ```ruby
150
+ include MapKit::DataTypes
151
+ ```
152
+
153
+ ### CoordinateSpan: The wrapper for MKCoordinateSpan
154
+
155
+ The `CoordinateSpan` initializer takes a variety of argument types
156
+
157
+ ```ruby
158
+ CoordinateSpan.new(1, 2)
159
+ CoordinateSpan.new([1, 2])
160
+ CoordinateSpan.new(MKCoordinateSpan)
161
+ ```
162
+
163
+ Methods on `CoordinateSpan`
164
+
165
+ ```ruby
166
+ >> cs = CoordinateSpan.new(1, 2)
167
+ => [1, 2]
168
+ >> cs.latitude_delta
169
+ => 1
170
+ >> cs.longitude_delta
171
+ => 2
172
+ >> cs.to_array
173
+ => [1, 2]
174
+ >> cs.sdk
175
+ => MKCoordinateSpan
176
+ ```
177
+
178
+ ### CoordinateRegion: The wrapper for MKCoordinateRegion
179
+
180
+ The `CoordinateRegion` initializer takes a variety of argument types
181
+
39
182
  ```ruby
40
- @map.user_located?
41
- ```
42
- Get the users coordinates
183
+ CoordinateRegion.new(CoordinateRegion)
184
+ CoordinateRegion.new(MKCoordinateRegion)
185
+ CoordinateRegion.new([56, 10.6], [3.1, 3.1])
186
+ CoordinateRegion.new(LocationCoordinate, CoordinateSpan)
187
+ CoordinateRegion.new(CLLocationCoordinate2D, MKCoordinateSpan)
188
+ ```
189
+
190
+ Methods on `CoordinateRegion`
191
+
43
192
  ```ruby
44
- @map.user_coordinates
45
- ```
193
+ >> cr = CoordinateRegion.new([56, 10.6], [3.1, 5.1])
194
+ => {:center => [56, 10.6], :span => [3.1, 5.1]}
195
+ >> cs.center
196
+ => LocationCoordinate([56, 10.6])
197
+ >> cs.region
198
+ => CoordinateSpan([3.1, 5.1])
199
+ >> cs.to_hash
200
+ => {:center => [56, 10.6], :span => [3.1, 5.1]}
201
+ >> cs.sdk
202
+ => MKCoordinateRegion
203
+ ```
204
+
205
+ ### MapPoint: The wrapper for MKMapPoint
206
+
207
+ The `MapPoint` initializer takes a variety of argument types
208
+
209
+ ```ruby
210
+ MapPoint.new(50, 45)
211
+ MapPoint.new([50, 45])
212
+ MapPoint.new(MKMapPoint)
213
+ ```
214
+
215
+ Methods on `MapPoint`
216
+
217
+ ```ruby
218
+ >> mp = MapPoint.new(50, 45)
219
+ => [50, 45]
220
+ >> mp.x
221
+ => 50
222
+ >> mp.y
223
+ => 45
224
+ >> mp.to_array
225
+ => [50, 45]
226
+ >> mp.sdk
227
+ => MKMapPoint
228
+ ```
229
+
230
+ ### MapSize: The wrapper for MKMapSize
231
+
232
+ The `MapSize` initializer takes a variety of argument types
233
+
234
+ ```ruby
235
+ MapSize.new(10, 12)
236
+ MapSize.new([10, 12])
237
+ MapSize.new(MKMapSize)
238
+ ```
239
+
240
+ Methods on `MapSize`
241
+
242
+ ```ruby
243
+ >> ms = MapSize.new(10, 12)
244
+ => [10, 12]
245
+ >> ms.width
246
+ => 10
247
+ >> ms.height
248
+ => 12
249
+ >> ms.to_array
250
+ => [50, 45]
251
+ >> ms.sdk
252
+ => MKMapSize
253
+ ```
254
+
255
+ ### MapRect: The wrapper for MKMapRect
256
+
257
+ The `MapRect` initializer takes a variety of argument types
258
+
259
+ ```ruby
260
+ MapRect.new(x, y, width, height)
261
+ MapRect.new([x, y], [width, height])
262
+ MapRect.new(MapPoint, MapSize)
263
+ MapRect.new(MKMapPoint, MKMapSize)
264
+ ```
265
+
266
+ Methods on `MapRect`
267
+
268
+ ```ruby
269
+ >> mr = MapRect.new(2, 3, 10, 12)
270
+ => {:origin => [2, 3], :size => [10, 12]}
271
+ >> mr.origin
272
+ => MapRect([2, 3])
273
+ >> mr.size
274
+ => MapSize([10, 12])
275
+ >> mr.to_hash
276
+ => {:origin => [2, 3], :size => [10, 12]}
277
+ >> mr.sdk
278
+ => MKMapRect
279
+ ```
46
280
 
@@ -7,7 +7,7 @@ Motion::Project::App.setup do |app|
7
7
  base_path = "#{File.dirname(__FILE__)}/map-kit-wrapper"
8
8
  graph = Codependency::Graph.new("#{base_path}/map_view.rb")
9
9
 
10
- files = (graph.files + Dir.glob(File.join(File.dirname(__FILE__), 'map-kit-wrapper/*.rb')).map { |file| file }).uniq
10
+ files = (graph.files + Dir.glob(File.join(File.dirname(__FILE__), 'map-kit-wrapper/*.rb'))).uniq
11
11
  files.reverse.each do |file|
12
12
  app.files.unshift(file)
13
13
  end
@@ -1,12 +1,14 @@
1
1
  module CoreLocation
2
+ ##
2
3
  # Wrappers for the Core Location Data Types
3
4
  # http://developer.apple.com/library/mac/#documentation/CoreLocation/Reference/CoreLocationDataTypesRef/Reference/reference.html
4
5
  module DataTypes
5
-
6
+ ##
6
7
  # Ruby wrapper for CLLocationCoordinate2D
7
8
  class LocationCoordinate
8
9
  attr_reader :sdk
9
10
 
11
+ ##
10
12
  # LocationCoordinate.new(1,2)
11
13
  # LocationCoordinate.new([1,2])
12
14
  # LocationCoordinate.new(LocationCoordinate)
@@ -1,14 +1,15 @@
1
1
  # require core_location_data_types
2
2
 
3
3
  module MapKit
4
+ ##
4
5
  # Wrappers for the Map Kit Data Types
5
6
  # http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MapKitDataTypesReference/Reference/reference.html
6
7
  module DataTypes
7
-
8
+ ##
8
9
  # Wrapper for MKCoordinateSpan
9
10
  class CoordinateSpan
10
11
  attr_reader :sdk
11
-
12
+ ##
12
13
  # CoordinateSpan.new(1,2)
13
14
  # CoordinateSpan.new([1,2])
14
15
  # CoordinateSpan.new(MKCoordinateSpan)
@@ -33,12 +34,12 @@ module MapKit
33
34
  [latitude_delta, longitude_delta]
34
35
  end
35
36
  end
36
-
37
+ ##
37
38
  # Wrapper for MKCoordinateRegion
38
39
  class CoordinateRegion
39
40
  include CoreLocation::DataTypes
40
41
  attr_reader :sdk
41
-
42
+ ##
42
43
  # CoordinateRegion.new(CoordinateRegion)
43
44
  # CoordinateRegion.new(MKCoordinateRegion)
44
45
  # CoordinateRegion.new([56, 10.6], [3.1, 3.1])
@@ -70,11 +71,11 @@ module MapKit
70
71
  {:center => center.to_array, :span => span.to_array}
71
72
  end
72
73
  end
73
-
74
+ ##
74
75
  # Wrapper for MKMapPoint
75
76
  class MapPoint
76
77
  attr_reader :sdk
77
-
78
+ ##
78
79
  # MapPoint.new(50,45)
79
80
  # MapPoint.new([50,45])
80
81
  # MapPoint.new(MKMapPoint)
@@ -99,11 +100,11 @@ module MapKit
99
100
  [x, y]
100
101
  end
101
102
  end
102
-
103
+ ##
103
104
  # Wrapper for MKMapSize
104
105
  class MapSize
105
106
  attr_reader :sdk
106
-
107
+ ##
107
108
  # MapSize.new(10,12)
108
109
  # MapSize.new([10,12])
109
110
  # MapSize.new(MKMapSize)
@@ -128,11 +129,11 @@ module MapKit
128
129
  [width, height]
129
130
  end
130
131
  end
131
-
132
+ ##
132
133
  # Wrapper for MKMapRect
133
134
  class MapRect
134
135
  attr_reader :sdk
135
-
136
+ ##
136
137
  # MapRect.new(x, y, width, height)
137
138
  # MapRect.new([x, y], [width, height])
138
139
  # MapRect.new(MapPoint, MapSize)
@@ -8,6 +8,8 @@ module MapKit
8
8
  :satellite
9
9
  when MKMapTypeHybrid
10
10
  :hybrid
11
+ else
12
+ raise "Unknown map type: #{map_type.inspect}"
11
13
  end
12
14
  end
13
15
 
@@ -19,6 +21,8 @@ module MapKit
19
21
  MKMapTypeSatellite
20
22
  when :hybrid
21
23
  MKMapTypeHybrid
24
+ else
25
+ raise "Unknown map type: #{map_type.inspect}"
22
26
  end
23
27
  end
24
28
  end
@@ -6,6 +6,7 @@ module MapKit
6
6
  include CoreLocation::DataTypes
7
7
  include MapKit::DataTypes
8
8
 
9
+ ##
9
10
  # Wrapper for MKMapView
10
11
  class MapView < MKMapView
11
12
  include CoreLocation::DataTypes
@@ -16,58 +17,74 @@ module MapKit
16
17
  self.alloc.init
17
18
  end
18
19
 
19
- # zoom methods
20
-
20
+ ##
21
+ # Show if zoom is enabled
21
22
  def zoom_enabled?
22
23
  self.isZoomEnabled
23
24
  end
24
25
 
26
+ ##
27
+ # Set zoom on/off
25
28
  def zoom_enabled=(enabled)
26
29
  self.setZoomEnabled(enabled)
27
30
  end
28
31
 
29
- # scroll methods
30
-
32
+ ##
33
+ # Show if scroll is enbaled
31
34
  def scroll_enabled?
32
35
  self.isScrollEnabled
33
36
  end
34
37
 
38
+ ##
39
+ # Set scroll on/off
35
40
  def scroll_enabled=(enabled)
36
41
  self.setScrollEnabled(enabled)
37
42
  end
38
43
 
39
- # user location methods
40
-
44
+ ##
45
+ # Show if the users location is on the map
41
46
  def shows_user_location?
42
47
  self.showsUserLocation
43
48
  end
44
49
 
50
+ ##
51
+ # Set visible user location on/off
45
52
  def shows_user_location=(enabled)
46
53
  self.showsUserLocation = enabled
47
54
  end
48
55
 
56
+ ##
57
+ # Show if the users has been located yet
49
58
  def user_located?
50
59
  self.shows_user_location? && self.userLocation.location ? true : false
51
60
  end
52
61
 
62
+ ##
63
+ # Show the users coordinates
64
+ # defaults to nil if the functionality is turned off
65
+ # or the location has not yet been found
53
66
  def user_coordinates
54
67
  self.user_located? ? LocationCoordinate.new(self.userLocation.location.coordinate) : nil
55
68
  end
56
69
 
57
- # region methods
58
-
70
+ ##
71
+ # Get the maps region
59
72
  def get_region
60
73
  CoordinateRegion.new(self.region.center, self.region.span)
61
74
  end
62
75
 
76
+ ##
77
+ # Set the maps region
63
78
  def region=(*args)
64
79
  self.set_region(CoordinateRegion.new(args.first), false)
65
80
  end
66
81
 
82
+ ##
83
+ # Set the maps region including animation
67
84
  def set_region(coordinate_region, *args)
68
85
  opts = {:animated => false}
69
86
  opts.merge!(args.first) if args.first
70
- self.setRegion(coordinate_region.sdk, animated:opts[:animated])
87
+ self.setRegion(coordinate_region.sdk, animated: opts[:animated])
71
88
  end
72
89
  end
73
90
  end
@@ -1,3 +1,3 @@
1
1
  module MapKit
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,13 +1,15 @@
1
1
  module MapKit
2
- # Ruby conversion of http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/
2
+ ##
3
+ # Ruby adaption of http://troybrant.net/blog/2010/01/set-the-zoom-level-of-an-mkmapview/
3
4
  module ZoomLevel
4
5
  include Math
5
6
  MERCATOR_OFFSET = 268435456.0
6
7
  MERCATOR_RADIUS = 85445659.44705395
7
8
 
9
+ ##
10
+ # Map conversion methods
8
11
  module ClassMethods
9
12
  include Math
10
- # Map conversion methods
11
13
 
12
14
  def longitude_to_pixel_space_x(longitude)
13
15
  (MERCATOR_OFFSET + MERCATOR_RADIUS * longitude * PI / 180.0).round
@@ -63,6 +65,7 @@ module MapKit
63
65
  MKCoordinateSpanMake(latitude_delta, longitude_delta)
64
66
  end
65
67
 
68
+ ##
66
69
  # KMapView cannot display tiles that cross the pole
67
70
  # This would involve wrapping the map from top to bottom, something that a Mercator projection just cannot do.
68
71
  def coordinate_region_with_map_view(map_view, center_coordinate, zoom_level)
@@ -127,9 +130,9 @@ module MapKit
127
130
 
128
131
  # Public methods
129
132
 
130
- def set_center_coordinates(center_coordinate, zoom_level, animated)
131
- # clamp large numbers to 28
132
- zoom_level = [zoom_level, 28].min
133
+ def set_center_coordinates(center_coordinate, zoom_level, animated = false)
134
+ # clamp large numbers to 18
135
+ zoom_level = [zoom_level, 18].min
133
136
 
134
137
  # use the zoom level to compute the region
135
138
  span = self.class.coordinate_span_with_map_view(self, center_coordinate, zoom_level)
@@ -139,11 +142,14 @@ module MapKit
139
142
  self.setRegion(region, animated: animated)
140
143
  end
141
144
 
142
- def set_map_lat_lon(latitude, longitude, zoom_level, animated)
145
+ def set_map_lat_lon(latitude, longitude, zoom_level, animated = false)
143
146
  coordinates = CLLocationCoordinate2DMake(latitude, longitude)
144
147
  self.set_center_coordinates(coordinates, zoom_level, animated)
145
148
  end
146
149
 
150
+ ##
151
+ # Get the current zoom level
152
+
147
153
  def zoom_level
148
154
  region = self.region
149
155
 
@@ -158,5 +164,12 @@ module MapKit
158
164
  zoom_level
159
165
  end
160
166
 
167
+ ##
168
+ # Set the current zoom level
169
+
170
+ def set_zoom_level(zoom_level, animated = false)
171
+ self.set_center_coordinates(self.region.center, zoom_level, animated)
172
+ end
173
+
161
174
  end
162
175
  end
@@ -40,6 +40,8 @@ describe 'ZoomLevel' do
40
40
  span.longitude_delta.should.equal 56.25
41
41
  center.latitude.should.equal 55.9737854003906
42
42
  center.longitude.should.equal 10.01953125
43
+ @map.set_center_coordinates(CLLocationCoordinate2DMake(0, 0), 50, false)
44
+ @map.zoom_level.should.equal 18
43
45
  end
44
46
 
45
47
  it 'should return set_map_lat_lon' do
@@ -68,4 +70,9 @@ describe 'ZoomLevel' do
68
70
  it 'should have a zoom_level' do
69
71
  @map.zoom_level.should.equal 5.00000381469727
70
72
  end
73
+
74
+ it 'should set a zoom_level' do
75
+ @map.set_zoom_level(15, false)
76
+ @map.zoom_level.should.equal 15
77
+ end
71
78
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: map-kit-wrapper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kasper Weibel Nielsen-Refs