Takis
Posts: 0
Joined: Wed Oct 22, 2014 5:10 pm

Find if a map cords are withing a cycle

Hi

I have a DB where I store the lat and lng in one field with format "23.0000,89.0000"
I want to return records from my DB service only if are inside a map cycle, with center my current location.
I want the radius of the cycle to be configurable (100 meters, 1 km, etc).
I am able to return in a page the filed that contains the "lat,lng" and other info that I want.
So I want to add this criteria in my quere where clause.

Thanks!

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Find if a map cords are withing a cycle

Just an opinion, the best way to store map coordinates in the db is in geo format ( which uses long, lat ) and then you can use a where clause supported by mongodb which will return results within a radius.

I am using this successfully in a db where I expect over 100,000 records, and since this will be a high volume query db... It geo field can be indexed and will speed your query performance as well.

You can use the geo spatial query in your where clause. Code example on reuqest. You can also use the tutorial ... But it's going to be a slower way to return the result set since it requires reading through the entire db each time you need a result set.

Happy Sunday,
Bruce

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Find if a map cords are withing a cycle

Here's a sample query against a DB with a geopoint on it (the cwhereclause is a localStorage item used in a mapping when the query is initiated on my UI , and the geo_latlong field used below is the name of my column which is in geo format - on the DB. slong and slat are my variables which are being used in the query to compare against (these would be the long and lat from your 'currentlocation' in your example above).

Code: Select all

     cwhere =  '{ "geo_latlong" : { "$nearSphere" : [' + slong + ',' + slat  + '], "$maxDistanceInMiles" : ' + Appery('slider_radius').val() +'}}'; 
         console.log('executed and set where to default:' + cwhere); 

best,

Bruce

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

Find if a map cords are withing a cycle

Hi Panagiotis and Bruce,

Good suggestion.

Regards.

Takis
Posts: 0
Joined: Wed Oct 22, 2014 5:10 pm

Find if a map cords are withing a cycle

Many thanks!!!

I will try it!

Takis
Posts: 0
Joined: Wed Oct 22, 2014 5:10 pm

Find if a map cords are withing a cycle

Is it because in my DB CustomerGeoPoint is a "string" type and not "Geopoint"?
If this is the case, can I work with "sting" type?
How should I save values in Geopoint ? copy/paste cords as it appears in Google maps?

Takis
Posts: 0
Joined: Wed Oct 22, 2014 5:10 pm

Find if a map cords are withing a cycle

Are we using the correct syntax for the nearSphere ?

http://docs.mongodb.org/manual/refere...

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Find if a map cords are withing a cycle

your CustomerGeoPoint needs to be saved as type "Geo" in the database....

How many records in your customerGeoPoint DB ? the approach to getting your string data into the Geopoint format - will depend upon volume....

I had 25000 so I needed to use automation and wrote some Javescript in Appery.... so I can help you with that if you need to ....

Add a column to your CustomerGeopint collection - of type Geo - and you'll see that that element (once you bring the collection into appery) is an object of type array - and the elements are two numerics - the first is long and the second is lat.

Let me know and I can help with ideas to get the data in the format you need

Takis
Posts: 0
Joined: Wed Oct 22, 2014 5:10 pm

Find if a map cords are withing a cycle

Bruce many thanks for replying!

It seems that I am missing some basics....

I have very few records, as I am currently in implementation phase.
I just created the CustomerGeopoint column and added by typing the values.
This is not correct?
If not what is the correct way to insert values in the CustomerGeopoint ?

I tried again put nearSphere does not seem to work.
Is the where clause, in previous screen shot correct?

Thanks. Image

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Find if a map cords are withing a cycle

First question - in the query - did you change the column name you are querying against - to the CustomerGeopoint1 ? instead of the customerGeopoint (shown in the sscreenprint above)?

Also - are you testing your query in the service itself - prior to putting it in your code ? Go to the 'test' tab for your service and paste your query above-in there Then = let me and the folks here know the error message....

Thanks!
Bruce

Return to “Issues”