Class: Parse::Constraint::NearSphereQueryConstraint
- Inherits:
- Parse::Constraint
- Object
- Parse::Constraint
- Parse::Constraint::NearSphereQueryConstraint
- Defined in:
- lib/parse/query/constraints.rb
Overview
Add support $maxDistanceInKilometers (for kms) and $maxDistanceInRadians (for radian angle).
Equivalent to the `$nearSphere` Parse query operation. This is only applicable if the field is of type `GeoPoint`. This will query Parse and return a list of results ordered by distance with the nearest object being first.
q.where :field.near => geopoint
geopoint = Parse::GeoPoint.new(30.0, -20.0)
PlaceObject.all :location.near => geopoint
If you wish to constrain the geospatial query to a maximum number of miles, you can utilize the `max_miles` method on a `Parse::GeoPoint` object. This is equivalent to the `$maxDistanceInMiles` constraint used with `$nearSphere`.
q.where :field.near => geopoint.max_miles(distance)
# or provide a triplet includes max miles constraint
q.where :field.near => [lat, lng, miles]
geopoint = Parse::GeoPoint.new(30.0, -20.0)
PlaceObject.all :location.near => geopoint.max_miles(10)
Instance Attribute Summary
Attributes inherited from Parse::Constraint
#operand, #operation, #operator, #value
Instance Method Summary collapse
- #build ⇒ Hash
The compiled constraint.
- #near ⇒ NearSphereQueryConstraint
A registered method on a symbol to create the constraint.
Methods inherited from Parse::Constraint
#as_json, contraint_keyword, create, #formatted_value, formatted_value, #initialize, #key, #precedence, register, #to_s
Constructor Details
This class inherits a constructor from Parse::Constraint
Instance Method Details
#build ⇒ Hash
Returns the compiled constraint.
617 618 619 620 621 622 623 624 625 626 627 628 | # File 'lib/parse/query/constraints.rb', line 617 def build point = formatted_value max_miles = nil if point.is_a?(Array) && point.count > 1 max_miles = point[2] if point.count == 3 point = { __type: "GeoPoint", latitude: point[0], longitude: point[1] } end if max_miles.present? && max_miles > 0 return { @operation.operand => { key => point, :$maxDistanceInMiles => max_miles.to_f } } end { @operation.operand => { key => point } } end |
#near ⇒ NearSphereQueryConstraint
A registered method on a symbol to create the constraint. Maps to Parse operator “$nearSphere”.
613 | # File 'lib/parse/query/constraints.rb', line 613 contraint_keyword :$nearSphere |