Class: Parse::Constraint::NearSphereQueryConstraint

Inherits:
Parse::Constraint show all
Defined in:
lib/parse/query/constraints.rb

Overview

TODO:

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

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

#buildHash

Returns the compiled constraint.

Returns:

  • (Hash)

    the compiled constraint.


622
623
624
625
626
627
628
629
630
631
632
633
# File 'lib/parse/query/constraints.rb', line 622

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, :max_miles.to_f } }
  end
  { @operation.operand => { key => point } }
end

#nearNearSphereQueryConstraint

A registered method on a symbol to create the constraint. Maps to Parse operator “$nearSphere”.

Examples:

q.where :field.near => geopoint
q.where :field.near => geopoint.max_miles(distance)

Returns:


618
# File 'lib/parse/query/constraints.rb', line 618

contraint_keyword