Geolocation: Sorting

Hey,

I’m creating an application that allows customers to search for users who can help them with a specific problem. I want it to be location-based, so it only shows the best suited users nearby.

I will ask the customer for an address, and therefore I can use geocoding to turn that info into lang and longitude. But from here I’m stuck, I have no clue as to what’s the best solution for calculating distance, and showing the nearest results.

I’ve looked at Mongo’s feature $near - but I feel like i need to do some work on the data before I can use it?

Thanks in advance!

I’m not sure what you’re looking for, but this blog post should help.

1 Like

Thank you very much - I’ve now found a solution to my sorting problem.
But until now I’ve only worked with static lat and lng - values. I’m implementing the aldeed:geocoder, and it works perfectly, but I’m having problems withdrawing just the lat and lng value from the “JSON array”.

var result = geo.geocode(address);

This is the variable I store the geocoded information in - how do i extract just the lat and lng?

Okay - so I might need to be a bit more specific! :smile:
I’ve console logged the result variable to the terminal: and this is what I get:

I20150713-17:49:58.140(2)? [ { latitude: 55.6808969,
I20150713-17:49:58.143(2)?     longitude: 12.5882364,
I20150713-17:49:58.144(2)?     country: 'Denmark',
I20150713-17:49:58.145(2)?     city: 'København',
I20150713-17:49:58.145(2)?     state: null,
I20150713-17:49:58.145(2)?     stateCode: null,
I20150713-17:49:58.146(2)?     zipcode: '1255',
I20150713-17:49:58.146(2)?     streetName: 'Store Strandstræde',
I20150713-17:49:58.146(2)?     streetNumber: '4',
I20150713-17:49:58.146(2)?     countryCode: 'DK' } ]

I’ve tried using JSON.parse and so on (from various tutorials - but that isn’t working).

From your example I would use something like:

var lat = result[0].latitude;
var long = result[0].longitude;
1 Like

Thank you so much - It was just the part with [0] I didn’t figure out.