diff --git a/lib/ProMotion/map/map_screen_module.rb b/lib/ProMotion/map/map_screen_module.rb index 89d6552..17f990f 100644 --- a/lib/ProMotion/map/map_screen_module.rb +++ b/lib/ProMotion/map/map_screen_module.rb @@ -53,6 +53,31 @@ def center=(params={}) ) end + def show_user_location + set_show_user_location true + end + + def hide_user_location + set_show_user_location false + end + + def set_show_user_location(show) + self.mapview.showsUserLocation = show + end + + def showing_user_location? + self.mapview.showsUserLocation + end + + def user_location + self.mapview.userLocation.location.nil? ? nil : self.mapview.userLocation.location.coordinate + end + + 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 + def annotations @promotion_annotation_data end @@ -95,6 +120,8 @@ def clear_annotations end def mapView(mapView, viewForAnnotation:annotation) + return if annotation.is_a? MKUserLocation + identifier = annotation.annotation_params[:identifier] if view = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) view.annotation = annotation @@ -122,7 +149,7 @@ def set_start_position(params={}) initialLocation = CLLocationCoordinate2D.new(params[:latitude], params[:longitude]) region = MKCoordinateRegionMakeWithDistance(initialLocation, params[:radius] * meters_per_mile, params[:radius] * meters_per_mile) - self.mapview.setRegion(region, animated:false) + set_region(region, animated:false) end def set_up_start_position @@ -163,7 +190,7 @@ def zoom_to_fit_annotations(animated=true) region = MKCoordinateRegionMake(coord, span) fits = self.mapview.regionThatFits(region); - self.mapview.setRegion(fits, animated:animated) + set_region(fits, animated:animated) end def set_region(region, animated=true) @@ -190,6 +217,15 @@ def look_up_address(args={}, &callback) return geocoder.geocodeAddressString(args[:address].to_s, inRegion:args[:region].to_s, completionHandler: callback) if args[:region] end + ########## Cocoa touch methods ################# + def mapView(mapView, didUpdateUserLocation:userLocation) + if self.respond_to?(:on_user_location) + on_user_location(userLocation) + else + PM.logger.info "You're tracking the user's location but have not implemented the #on_user_location(location) method in MapScreen #{self.class.to_s}." + end + end + module MapClassMethods def start_position(params={}) @start_position_params = params diff --git a/spec/map_spec.rb b/spec/map_spec.rb index a3c5767..f0d5af0 100644 --- a/spec/map_spec.rb +++ b/spec/map_spec.rb @@ -57,4 +57,12 @@ @map.annotations.count.should == 0 end + it "should return false when user location is not being shown" do + @map.showing_user_location?.should == false + end + + it "should return nil for user location when not being shown" do + @map.user_location.should == nil + end + end