문제

I'm building my first iOS app that helps people place down annotations on a map for other people to see. I'm using Parse as my backend service.

What I have:

A class named "Pins" on Parse, which has a "location" column (type: GeoPoint) and an occurrences column (type: number).

How it works:

Users can click on the map to create an annotation that gets sent to the server with its GeoPoint. Other users can then see it when they open the app.

What I want to achieve:

When a user places an annotation near another annotation that already exists, I want to increment the "occurrences" of the object nearby rather than creating a new object.

I was half way through creating beforeSave function when I discovered Parse has a method to do it already: {Parse.Query} near(key, point)

Unfortunately I'm having a lot of trouble implementing it and the documentation is lacking some examples. so my question is:

How would I query the "Pins" class for annotations that are within 100 meters of the one the user is creating?

도움이 되었습니까?

해결책

It's really just like a regular query:

If you know the position of the newly dropped annotation:

// say your annotation answers a location with lat and long
PFGeoPoint *point = [PFGeoPoint geoPointWithLatitude:lat longitude:long];

Query your custom class for nearby instances like this:

PFQuery *query = [PFQuery queryWithClassName:@"Pins"];
[query whereKey:@"location" nearGeoPoint:point];

// do your regular find stuff with query.

In case the question is about js (it's unclear from the tags), its the same concept, using:

var Pins = Parse.Object.extend("Pins");
var query = new Parse.Query(Pins);
query.near("location", point);

Or to do it around a given radius, then:

query.withinKilometers("location", point, 0.1);  // 100 meters
query.find().then( /* return to client */ )
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top