Appcelerator Titanium ACS Order place by nearest the user - iphone

I am trying to order at list by nearest place. This is working fine with this code:
Cloud.Places.query({
page: 1,
per_page: 20,
where: {
lnglat: { '$nearSphere': [latitudefast,longitudefast], }
},
order: {
lnglat: { '$nearSphere': [latitudefast,longitudefast], }
},
latitudefast and longitudefast is representing the actual position on the user. It has be defined before the query.
But it is "upside down", which means that the nearest place is in the bottom of the list, and the one farthest away is at the top of the list! How come? How do I order in reverse? Am i ordering wrong?
Thanks!

Have you tried not specifying the order? $nearSphere by default returns results sorted by distance.
From MongoDB's documentation (which ACS uses as its data store) describing $near:
The above query finds the closest points to (50,50) and returns them
sorted by distance (there is no need for an additional sort
parameter).
This applies to $nearSphere as well.
http://www.mongodb.org/display/DOCS/Geospatial+Indexing

Related

Random item from Algolia

How do I get random algolia item from it's index?
All of my items have:
objectID "POST#news#44.7704046#17.1900285"
name "News"
categories [ "cafe", "food", "establishment", "food" ]
_geoloc { lat: "44.7704046", lng: "17.1900285" }
I would like to optionally search by name, match 1 or all categories, geo location filtering with distance, and most importantly, I only want 1 RANDOM returned from Algolia.
I can't do client side random, because sometimes without filters I would get too many results back ( 10000 ), so I can't transfer that over the wire.
Please help
Hi #Djordje there are no real way to get a random result with Algolia though you you could use an attribute to randomise the results and only use the first item. See documentation [here][1]

Mongoid geonear query with max distance not working properly

I am confused about the geonear query with mongoid. It seems that I am missing something and when I m trying to query for points near the given coordinates with max distance 200 meters form my controller with the following:
Coord.geo_near([params[:lon].to_i, params[:lat].to_i]).max_distance(200)
I get all the points that are available in the collection in the same order for every set of coords that i provide.
My model:
class Coord
include Mongoid::Document
include Mongoid::Geospatial
field :Message, :type => String
field :location, type: Point, spatial: true
end
I have created the 2dsphere index from mongo console:
db.coords.createIndex( { location : "2dsphere" } )
I have also tried instead of 200 meters to provide 0.2/111.2 in the case that the max_distance works with degrees but in this case I m not getting any results.
Use Earth radius
To search in 10km radius:
Coord.geo_near([params[:lon].to_f, params[:lat].to_f]).max_distance(10.fdiv(6_371))

How to display polygons that are within a specific range (circle) using Leaflet

I am trying find a solution on how to display polygons that are only within a specific range, a circle with radius using leaflet.
Polygons screenshots
Before, I have ask for help regarding on the display of points within a specific range but this time, since a polygon have many nodes/coordinates, i don't have any idea of how it can be done for polygons; a foreach statement?
Any solution? Thanks for the help!
Similar problem solved for displaying points within a specific range
Since you're using MongoDB, the best solution here is (if that's possible), to handle this in the database. Put 2dsphere indexes on your document's loc field and use a $geoWithin query in combination with $centerSphere:
The following example queries grid coordinates and returns all documents within a 10 mile radius of longitude 88 W and latitude 30 N. The query converts the distance to radians by dividing by the approximate radius of the earth, 3959 miles:
db.places.find( {
loc: { $geoWithin: { $centerSphere: [ [ -88, 30 ], 10/3959 ] } }
} )
2dsphere reference: http://docs.mongodb.org/manual/core/2dsphere/
$geoWithin reference: http://docs.mongodb.org/manual/reference/operator/query/geoWithin/
$centerSphere reference: http://docs.mongodb.org/manual/reference/operator/query/centerSphere/
If you really want to do this clientside (which i absolutely wouldn't recommend) and you don't want to build your on solution (which is possible) you could take a look at GeoScript.
GeoScript's geom.Geometry() class has a contains method:
Tests if this geometry contains the other geometry (without boundaries touching).
Geom.geometry reference: http://geoscript.org/js/api/geom/geometry.html
EDIT: Here's the pure JS/Leaflet solution as requested in the comments, this is quick-n-dirty, but it should work. Here the containsPolygon method returns true when all of the polygon's points are within the circle:
L.Circle.include({
'containsPoint': function (latLng) {
return this.getLatLng().distanceTo(latLng) < this.getRadius();
},
'containsPolygon': function (polygon) {
var results = [];
polygon.getLatLngs().forEach(function (latLng) {
results.push(this.containsPoint(latLng));
}, this);
return (results.indexOf(false) === -1);
}
});
Here's a working example: http://plnkr.co/edit/JlFToy?p=preview
If you want to return true if one or more of the polygon's points are within the circle than you must change the return statement to this:
return (results.indexOf(true) !== -1);

Search with flexible ranking

Can any one suggest search engine that has flexible ranking calculation?
What is flexible ranking calculation?
for example I have two documents:
obj1 = {
title: "new record"
tags: [
{value:"tag1", weight:1},
{value:"tag2", weight:0.8},
{value:"tag3", weight:2},
]
}
obj2 = {
title: "new record with tag1 in title"
tags: [
{value:"tag1", weight:0.5},
{value:"tag2", weight:1},
{value:"tag3", weight:0.01},
]
}
let's assume weight for "title" property is 0.25
When I do search for "tag1" in all properties
I want search to return ranking = 1 for obj1 and ranking = 0.75 for obj2
I know Solr can do it but do you have any other suggestions?
You mention weight for title but then the values you described for scores mapped directly to tag values. Not sure if you missed a detail on how these two connect.
Assuming you want the score of the title match to play a role in the overall document score in addition to boosting documents that match a particular tag or value range, you can do this with Azure Search using scoring profiles (if you want a search-as-a-service solution), and can do it with Solr or Elasticsearch by including boosts as part of the query if you prefer to deploy and management your own infrastructure; in Elasticsearch for example there are function boosts that will allow you to use the value of a field as input to boost computation.

Mongodb geolocation boundaries search/query

I have a documents contains list of location "boxes" (square area). Each box is represented by 2 points (bottom-left or south-west, top-right or north-east).
Document, for example:
{
locations: [
[[bottom,left],[top,right]],
[[bottom,left],[top,right]],
[[bottom,left],[top,right]]
]
}
I'm using 2d index for those boundaries points.
My input is a specific location point [x,y] and I want to fetch all documents that have at list one box that this point is located in it.
Is there any geospatial operator I can use to do that?
How do I write this query?
You can use the box operator, see:
http://docs.mongodb.org/manual/reference/operator/query/box/#op._S_box with the following example taken directly from that page:
db.places.find( { loc : { $geoWithin : { $box :
[ [ 0 , 0 ] ,
[ 100 , 100 ] ] } } } )
It is worth noting that the 2d index is considered legacy. If you can convert to using GeoJSON and a 2dsphere index, then you can use the $geoWithin operator: see
http://docs.mongodb.org/manual/reference/operator/query/geoWithin/#op._S_geoWithin
GeoJSON has a number of other benefits, not least of which, is that it is easily transmitted and digested by web mapping apps such as OpenLayers or Leaflet.