Requesting a Firebase Object Next to the Object

In mysql, I used the haversine formula to query the closest object.

Using this formula Formula

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20; 

What kind

  • 3959: Earth radius in miles
  • 37, -122: given lat lng
  • 25: within 25 miles

In Firebase, Can I store lat lng users like what I did in mysql? Create a marker table. id, lat, lng and then use the formula to query

Update

I have to ask what is the query method next to using this formula in firebase.

+4
source share
2 answers

The short answer, not how you want it.

Firebase has two ways of requesting data: by path and by priority. This is more limited than SQL, and the reason for this is very good there - our API is carefully designed so that we can guarantee fast execution. Firebase is a real-time and scalable backend, and we want you to be able to create great applications that can serve millions of users without compromising responsiveness.

See what firebase and deNormalizing data are. Also, this SO question is similar.

Reply to comment : Firebase will not calculate sin( radians(X) ) for you. This is a "slow" operation. Thus, you will need to save this information in the data when you save it. I'm not 100% sure, but you can store markers as well as store longitude / latitude in a separate parent.

 Root -> Markers -> longitude (Use the value as priority) -> MarkerId -> latitude (Use the value as priority) -> MarkerId 

You can then use the restriction to find the Max and Min longitude and latitude. Use this to request longitude and latitude paths by priority. If MarkerId exists in both cases, you use it.

A quick study found that this article is about Longitude Latitude Coordinates

+8
source

Hey, I just finished creating a Google map in real time using firebase and GeoFire. GeoFire is really cool and easy to use. It allows you to query using lon lat and radius. It returns a key that you can use to query your firebase database. You set the key while you create the geoFire object to be what you want. This is usually a link that you can use to get the object associated with this distance.

Here is the link to geoFire: https://github.com/firebase/geofire-js

Here is a usage example:

You have lon lat, which you use with the navigator:

 var lon = '123.1232'; var lat = '-123.756'; var user = { name: 'test user', longitude: lon, latitude: lat } usersRef.push(user).then(function(response) { var key = response.key; var coords = [lon, lat]; geoFire.set(key, coords, function(success){ console.log('User and geofire object has been created'); }); }) 

Now you can query the user using:

 // Set your current lon lat and radius var geoQuery = geoFire.query({ center: [latitude, longitude], radius: radiusKm }); geoQuery.on('key_entered', function(key, location, distance) { // You can now get the user using the key var user = firebaseRefUrl + '/' + key; // Here you can create an array of users that you can bind to a scope in the controller }); 

If you use google maps. I recommend you use angular -google-maps. This is a really cool Google Maps directive that uses an array of markers and circles. Therefore, when the variables $ scope.markers or $ scope.circles change in the controller, it will automatically be applied to the map without any dirty code. They have very good documentation.

Here is the link: http://angular-ui.imtqy.com/angular-google-maps/

+3
source

Source: https://habr.com/ru/post/1484134/


All Articles