这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# ProMotion-map

ProMotion-map provides a PM::MapScreen, extracted from the
ProMotion-map provides a PM::MapScreen, extracted from the
popular RubyMotion gem [ProMotion](https://github.com/clearsightstudio/ProMotion).

## Installation
Expand All @@ -19,11 +19,11 @@ 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

def on_appear
update_annotation_data
end

def annotation_data
[{
longitude: -82.965972900391,
Expand Down Expand Up @@ -59,7 +59,7 @@ class MyMapScreen < PM::MapScreen
}]
end


end
```

Expand All @@ -77,7 +77,7 @@ end
### Methods

#### annotation_data

Method that is called to get the map's annotation data and build the map. If you do not want any annotations, simply return an empty array.

```ruby
Expand Down Expand Up @@ -121,6 +121,8 @@ You may pass whatever properties you want in the annotation hash, but `:longitud

Use `:image` to specify a custom image. Pass in a string to conserve memory and it will be converted using `UIImage.imageNamed(your_string)`. If you pass in a `UIImage`, we'll use that, but keep in mind that there will be another unnecessary copy of the UIImage in memory.

Use `:left_accessory` and `:right_accessory` to specify a custom accessory, like a button.

You can access annotation data you've arbitrarily stored in the hash by calling `annotation_instance.annotation_params[:your_param]`.

#### update_annotation_data
Expand Down Expand Up @@ -182,7 +184,7 @@ Helper method to create an `MKCoordinateRegion`. Expects a hash in the form of:
```ruby
my_region = region({
coordinate:{
latitude: 35.0906,
latitude: 35.0906,
longitude: -82.965
},
# span is the latitude and longitude delta
Expand All @@ -195,7 +197,7 @@ my_region = region({
### Class Methods

#### start_position(latitude: Float, longitude: Float, radius: Float)

Class method to set the initial starting position of the `MapScreen`.

```ruby
Expand Down
1 change: 0 additions & 1 deletion lib/ProMotion-map.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# encoding: utf-8

unless defined?(Motion::Project::Config)
raise "ProMotion-map must be required within a RubyMotion project."
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ProMotion/map/map_screen_annotation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def set_defaults
@params = {
title: "Title",
pin_color: MKPinAnnotationColorRed,
identifier: "Annotation-#{@params[:pin_color] || @params[:image]}",
identifier: "Annotation-#{@params[:pin_color]}-#{@params[:image]}",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch there.

show_callout: true,
animates_drop: false
}.merge(@params)
Expand Down
29 changes: 19 additions & 10 deletions lib/ProMotion/map/map_screen_module.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module MapScreenModule
def screen_setup
self.mapview ||= add MKMapView.new, {
frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height),
resize: [ :width, :height ],
delegate: self
}

Expand Down Expand Up @@ -114,23 +113,29 @@ def clear_annotations
@promotion_annotation_data = []
end

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

identifier = annotation.annotation_params[:identifier]
if view = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if view = map_view.dequeueReusableAnnotationViewWithIdentifier(identifier)
view.annotation = annotation
else
#Set the pin properties
# Set the pin properties
if annotation.annotation_params[:image]
view = MKAnnotationView.alloc.initWithAnnotation(annotation, reuseIdentifier:identifier)
view.image = annotation.annotation_params[:image]
else
view = MKPinAnnotationView.alloc.initWithAnnotation(annotation, reuseIdentifier:identifier)
view.animatesDrop = annotation.annotation_params[:animates_drop]
view.pinColor = annotation.annotation_params[:pin_color]
end
view.canShowCallout = annotation.annotation_params[:show_callout]
end
view.image = annotation.annotation_params[:image] if view.respond_to?("image=") && annotation.annotation_params[:image]
view.animatesDrop = annotation.annotation_params[:animates_drop] if view.respond_to?("animatesDrop=")
view.pinColor = annotation.annotation_params[:pin_color] if view.respond_to?("pinColor=")
view.canShowCallout = annotation.annotation_params[:show_callout] if view.respond_to?("canShowCallout=")
if annotation.annotation_params[:left_accessory]
view.leftCalloutAccessoryView = annotation.annotation_params[:left_accessory]
end
if annotation.annotation_params[:right_accessory]
view.rightCalloutAccessoryView = annotation.annotation_params[:right_accessory]
end
view
end
Expand Down Expand Up @@ -170,7 +175,7 @@ def zoom_to_fit_annotations(animated=true)
bottomRight.latitude = [bottomRight.latitude, a.coordinate.latitude].min
end

#Find the bounds of all the pins and set the mapView
#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
Expand Down Expand Up @@ -213,7 +218,11 @@ def look_up_address(args={}, &callback)
end

########## Cocoa touch methods #################
def mapView(mapView, didUpdateUserLocation:userLocation)
def mapView(map_view, viewForAnnotation:annotation)
annotation_view(map_view, annotation)
end

def mapView(map_view, didUpdateUserLocation:userLocation)
if self.respond_to?(:on_user_location)
on_user_location(userLocation)
else
Expand Down
138 changes: 85 additions & 53 deletions spec/func_map_screen_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
describe "ProMotion::TestMapScreen functionality" do
tests PM::TestMapScreen
tests TestMapScreen

def map_screen
@map_screen ||= TestMapScreen.new(nav_bar: true)
end

# Override controller to properly instantiate
def controller
rotate_device to: :portrait, button: :bottom
@map ||= TestMapScreen.new(nav_bar: true)
@map.will_appear
@map.navigationController
map_screen.navigationController
end

def add_image_annotation
Expand All @@ -17,73 +17,73 @@ def add_image_annotation
subtitle: "Image pin subtitle",
image: UIImage.imageNamed("test.png")
}
@map.annotations.count.should == 5
@map.add_annotation ann
@map.set_region @map.region(coordinate: @map.annotations.last.coordinate, span: [0.05, 0.05])
map_screen.annotations.count.should == 5
map_screen.add_annotation ann
map_screen.set_region map_screen.region(coordinate: map_screen.annotations.last.coordinate, span: [0.05, 0.05])
end

after do
@map = nil
map_screen = nil
end

it "should have a navigation bar" do
@map.navigationController.should.be.kind_of(UINavigationController)
map_screen.navigationController.should.be.kind_of(UINavigationController)
end

it "should have the map properly centered" do
center_coordinate = @map.center
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
end

it "should move the map center" do
@map.center = {latitude: 35.07496, longitude: -82.95916, animated: true}
map_screen.center = {latitude: 35.07496, longitude: -82.95916, animated: true}

wait 0.75 do
center_coordinate = @map.center
center_coordinate = map_screen.center
center_coordinate.latitude.should.be.close 35.07496, 0.001
center_coordinate.longitude.should.be.close -82.95916, 0.001
end
end

it "should select an annotation" do
@map.selected_annotations.should == nil
@map.select_annotation @map.annotations.first
map_screen.selected_annotations.should == nil
map_screen.select_annotation map_screen.annotations.first
wait 0.75 do
@map.selected_annotations.count.should == 1
map_screen.selected_annotations.count.should == 1
end
end

it "should select an annotation by index" do
@map.selected_annotations.should == nil
@map.select_annotation_at 2
map_screen.selected_annotations.should == nil
map_screen.select_annotation_at 2
wait 0.75 do
@map.selected_annotations.count.should == 1
@map.selected_annotations[0].should == @map.promotion_annotation_data[2]
map_screen.selected_annotations.count.should == 1
map_screen.selected_annotations[0].should == map_screen.promotion_annotation_data[2]
end
end

it "should select another annotation and check that the title is correct" do
@map.selected_annotations.should == nil
@map.select_annotation @map.annotations[1]
map_screen.selected_annotations.should == nil
map_screen.select_annotation map_screen.annotations[1]
wait 0.75 do
@map.selected_annotations.count.should == 1
map_screen.selected_annotations.count.should == 1
end

@map.selected_annotations.first.title.should == "Turtleback Falls"
@map.selected_annotations.first.subtitle.should == "Nantahala National Forest"
map_screen.selected_annotations.first.title.should == "Turtleback Falls"
map_screen.selected_annotations.first.subtitle.should == "Nantahala National Forest"

end

it "should deselect selected annotations" do
@map.select_annotation @map.annotations.last
map_screen.select_annotation map_screen.annotations.last
wait 0.75 do
# @map.selected_annotations.count.should == 1
# map_screen.selected_annotations.count.should == 1
end

@map.deselect_annotations
map_screen.deselect_annotations
wait 0.75 do
@map.selected_annotations.should == nil
map_screen.selected_annotations.should == nil
end
end

Expand All @@ -93,11 +93,11 @@ def add_image_annotation
latitude: 35.092520895652,
title: "Something Else"
}
@map.annotations.count.should == 5
@map.add_annotation ann
@map.annotations.count.should == 6
@map.set_region @map.region(coordinate: @map.annotations.last.coordinate, span: [0.05, 0.05])
@map.select_annotation @map.annotations.last
map_screen.annotations.count.should == 5
map_screen.add_annotation ann
map_screen.annotations.count.should == 6
map_screen.set_region map_screen.region(coordinate: map_screen.annotations.last.coordinate, span: [0.05, 0.05])
map_screen.select_annotation map_screen.annotations.last
end

it "should be able to overwrite all annotations" do
Expand All @@ -110,53 +110,85 @@ def add_image_annotation
latitude: 35.2187218,
title: "My Cool Pin"
}]
@map.annotations.count.should == 5
@map.add_annotations anns
@map.annotations.count.should == 2
map_screen.annotations.count.should == 5
map_screen.add_annotations anns
map_screen.annotations.count.should == 2
end

it "should add an image based annotation" do
add_image_annotation
@map.annotations.count.should == 6
map_screen.annotations.count.should == 6

# Checking that it conforms to the MKAnnotation protocol manually since this doesn't work in iOS 7:
# @map.annotations.last.conformsToProtocol(MKAnnotation).should.be.true
# map_screen.annotations.last.conformsToProtocol(MKAnnotation).should.be.true
# See this 8 month old bug - https://github.com/siuying/rubymotion-protocol-bug

checking = @map.annotations.last
checking = map_screen.annotations.last
%w(title subtitle coordinate).each do |method|
defined?(checking.send(method.to_sym)).nil?.should.be.false
end
end

it "should select an image annotation" do
add_image_annotation
@map.selected_annotations.should == nil
@map.select_annotation @map.annotations.last
map_screen.selected_annotations.should == nil
map_screen.select_annotation map_screen.annotations.last
wait 0.75 do
@map.selected_annotations.count.should == 1
map_screen.selected_annotations.count.should == 1
end
end

it "should select an image annotation by index" do
add_image_annotation
@map.selected_annotations.should == nil
@map.select_annotation_at 5
map_screen.selected_annotations.should == nil
map_screen.select_annotation_at 5
wait 0.75 do
@map.selected_annotations.count.should == 1
@map.selected_annotations[0].should == @map.promotion_annotation_data[5]
map_screen.selected_annotations.count.should == 1
map_screen.selected_annotations[0].should == map_screen.promotion_annotation_data[5]
end
end

it "should select an image annotation and check that the title is correct" do
add_image_annotation
@map.selected_annotations.should == nil
@map.select_annotation @map.annotations[5]
map_screen.selected_annotations.should == nil
map_screen.select_annotation map_screen.annotations[5]
wait 0.75 do
@map.selected_annotations.count.should == 1
map_screen.selected_annotations.count.should == 1
end
@map.selected_annotations.first.title.should == "My Cool Image Pin"
@map.selected_annotations.first.subtitle.should == "Image pin subtitle"
map_screen.selected_annotations.first.title.should == "My Cool Image Pin"
map_screen.selected_annotations.first.subtitle.should == "Image pin subtitle"
end

it "should allow setting a leftCalloutAccessoryView" do
btn = UIButton.new
ann = {
longitude: -82.965972900392,
latitude: 35.090648651124,
title: "My Cool Image Pin",
subtitle: "Image pin subtitle",
left_accessory: btn
}
map_screen.add_annotation ann
annot = map_screen.annotations.last
annot.should.be.kind_of?(ProMotion::MapScreenAnnotation)
v = map_screen.annotation_view(map_screen.mapview, annot)
v.leftCalloutAccessoryView.should == btn
end

it "should allow setting a rightCalloutAccessoryView" do
btn = UIButton.new
ann = {
longitude: -82.965972900392,
latitude: 35.090648651124,
title: "My Cool Image Pin",
subtitle: "Image pin subtitle",
right_accessory: btn
}
map_screen.add_annotation ann
annot = map_screen.annotations.last
annot.should.be.kind_of?(ProMotion::MapScreenAnnotation)
v = map_screen.annotation_view(map_screen.mapview, annot)
v.rightCalloutAccessoryView.should == btn
end

end