Backendless Support
 
Answered

Search in radius C#

Hello i want to ask how i can do this in c#.

JAVA CODE:

BackendlessGeoQuery geoQuery = new BackendlessGeoQuery();

geoQuery.setLatitude( 32.555 );

geoQuery.setLongitude( -97.667 );

geoQuery.addCategory( "geoservice_sample" );

geoQuery.setIncludeMeta( true );

geoQuery.setRadius( 270d );

geoQuery.setUnits( Units.MILES );

Backendless.Geo.getPoints( geoQuery, new AsyncCallback<BackendlessCollection<GeoPoint>>()

{

@Override

public void handleResponse( BackendlessCollection<GeoPoint> geoPointCollection )

{

Iterator<GeoPoint> iterator = geoPointCollection.getCurrentPage().iterator();

while( iterator.hasNext() )

{

GeoPoint geoPoint = iterator.next();

System.out.println( "GeoPoint - " + geoPoint );

}

}

@Override

public void handleFault( BackendlessFault backendlessFault )

{

System.out.println( "Server reported an error - " + backendlessFault.getMessage() );

}

} );

C# CODE:

AsyncCallback<BackendlessCollection<GeoPoint>> getPointscallback = new AsyncCallback<BackendlessCollection<GeoPoint>>

(

result => {

//Dispatcher.BeginInvoke(() => ProblemBlock.Text = " result.Data.Count")

}

,

fault =>

{

System.Console.WriteLine("Error - " + fault);

});

BackendlessGeoQuery geoQuery = new BackendlessGeoQuery();

geoQuery.Latitude = 37.9811;

geoQuery.Longitude = 23.6363;

geoQuery.Radius = 1;

geoQuery.Units = BackendlessAPI.Geo.Units.KILOMETERS;

Backendless.Geo.GetPoints(geoQuery, getPointscallback);

Furthermore, i connect each geopoint with metadata being the name of the person in this location. How can i take

metadata of each geopoint and show it on screen?

(what i am trying to do in my application is to search in radius for nearby users within 1km and get their name on my screen)

Leave a Comment

Comments (13)

photo
1

Hi,

please pay attention to this page in the documentation. If you have any further questions or problems, I will be glad to answer here.

Regards,

Stanislaw

photo
1

AsyncCallback<BackendlessCollection<GeoPoint>> getPointscallback = new AsyncCallback<BackendlessCollection<GeoPoint>>

(

result => {

System.Diagnostics.Debug.WriteLine(String.Format("\nSearchByDateInCategory GETPOINTS: {0}", String.Join("\n", result.GetCurrentPage())));

} //Dispatcher.BeginInvoke(() => ProblemBlock.Text = " result.Data.Count")

,

fault =>

{

System.Console.WriteLine("Error - " + fault);

});

BackendlessGeoQuery geoQuery = new BackendlessGeoQuery();

geoQuery.Latitude = 37.9811;

geoQuery.Longitude = 23.6363;

geoQuery.Radius = 1;

geoQuery.Units = BackendlessAPI.Geo.Units.KILOMETERS;

geoQuery.IncludeMeta = true;

Backendless.Geo.GetPoints(geoQuery, getPointscallback);

Ok here is my code i managed to get the points in console with GetCurrentPage.

Looks like this.

SearchByDateInCategory GETPOINTS: GeoPoint{ objectId='186BD678-933F-B8C6-FFC3-F4F24A7DF200', latitude=37,9814873076923, longitude=23,6353465384615, categories=Default, metadata=nikolaos=System.Collections.Generic.Dictionary`2[System.Object,System.Object], distance=0,094 }

Now how can i only get the metadata?For example nikolaos.

photo
1

There is no way to get only metadata, you can extract it from full response after retrieving the point.

photo
1

Ok i get every Geopoint whithin 1km in radius. Then how can i extract the metada from each Geopoint?Which method to use?

photo
1

I think that extracting the metadata is the wrong way. In my app the users log in and each user save its GeoPoint when clicking the button (with this method BackendlessAPI.Backendless.Geo.SavePoint(geoPoint, callback); ). Then i want for every user to search for nearby users within 1km radius and see their names. How can i associate a geopoint with a user?? I use older sdk beacuse there is a problem referencing the newest ( i have made a topic for this) , so i cannot user SetRelation method to do it.

photo
1

You can establish user-to-geo relation. Navigate to users schema and add a new column > select a datatype "geopoint relationship". Here is the manual for managing data-to-geo relations in Backendless 3. This one for Backendless 4

Best Regards

photo
1

I have a column location type of geopoint relationship but i cannot set tha property with this line

currentUser.SetProperty("location", geoPoint);

AddRelation method does not exist in backendles sdk 1.0

Furthermore,

BackendlessUser currentUser = Backendless.UserService.CurrentUser;

//get current logged in user

GeoPoint geoPoint = new GeoPoint(latitude, longitude);

var name = currentUser.GetProperty("firstname").ToString();

geoPoint.Metadata.Add(name, currentUser);

Backendless.Geo.AddPoint(geoPoint);

AddPoint method stucks my application.

photo
photo
1

Hi Nikos

If you're including metadata in geoQuery - you can use getMetadata() method for every geoPoint object in returning collection.

Regards Anton

photo
1

Hi Anton! I do include metadata using geoQuery.IncludeMeta = true; . But i cannot find getMetada() method even though i tried some paths. I am using Backendless sdk version 1.0 . I cannot add 4.0 reference and i have made a topic for this. Where is method getMetadata() located in sdk i cannot find that either, i searched here https://github.com/Backendless/.NET-SDK/tree/master/Backendless

photo
1

Ok, in .Net SDK this method is called 'Metadata'. It's over here - https://github.com/Backendless/.NET-SDK/blob/master/Backendless/Geo/GeoPoint.cs

Best Regards

photo
1

Thanks for your help. I have one more question. Each user geopoint has this metada

geoPoint.Metadata.Add("name", currentUser.GetProperty("firstname").ToString());

geoPoint.Metadata is a Dictionary<string, object> where object value is a string.

I cannot get the string value probably because it's an object. Here is my code:

AsyncCallback<BackendlessCollection<GeoPoint>> getPointscallback = new AsyncCallback<BackendlessCollection<GeoPoint>>

(

result =>

{

foreach (var item in result.GetCurrentPage())

{

//Dispatcher.BeginInvoke(() => ProblemBlock.Text = item.Metadata.ToString());

System.Diagnostics.Debug.WriteLine(item.Metadata.Values.ToString());

}

}

I get this System.Collections.Generic.Dictionary`2+ValueCollection[System.String,System.Object] and not the actual value which is a string name for example george. How can i solve this??

metadata=george=System.Collections.Generic.Dictionary`2[System.String,System.Object]

photo
1

Probably that's because you're calling .ToString() on a Dictionary Values object itself, not on its member.

Try this instead:

  1. System.Diagnostics.Debug.WriteLine(item.Metadata["name"].ToString())

photo
1

Oh yes that worked!! Thank you very much!!

photo