question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the recommended way of updating geolocation while preserving associated data?

See original GitHub issue

I have the following structure:

screen shot 2014-08-27 at 00 02 03

I would like to update the location, while preserving the data

There is GeoFire.set(key, location) https://github.com/firebase/geofire-js#geofiresetkey-location

But when I call it the data would be lost.

I think I’ve found a workaround but it doesn’t seem very intuitive to me:

        firebaseRef.child('data').once('value', function(snap) {
          var data = snap.val();
          geofire.set(name, [lat, long]).then(function() {
           firebaseRef.child('data').set(data);
          }, function(error) {
            console.log('Error: ' + error);
          });
        });
  1. Saving data locally
  2. Updating geofire
  3. Storing data back to Firebase

Seem like a lot of work. Is there a better way?

Video explanation: https://www.youtube.com/watch?v=i5u77q7l8dU

You data sets are updating niceely: https://publicdata-transit.firebaseio.com/sf-muni 😃

Thanks!

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
jwngrcommented, Aug 27, 2014

Hey @stefek99 - The issue here is that GeoFire doesn’t support storing data inside of its index. Instead, you should store that information elsewhere, for multiple reasons. First, let me talk about what the solution here is. You should structure your data like this:

{
  geofire: {
    "-J9aZZl94Sx6h": { "g": <geohash>, "l": {} }
    "-Jhacf97x4S3h": { "g": <geohash>, "l": {} },
    ...
  },
  "locations": {
    "-J9aZZl94Sx6h": { <location-data> },
    "-Jhacf97x4S3h": { <location-data> }
  }
}

Note that the keys used for GeoFire are just the push IDs from the /locations/ node. This is important because when you receive a key from a key_entered or key_exited event, you can easily use it to look up the data by doing a rootRef.child("locations").child(key).once("value").

Now, what is the benefit of doing it this way? For one, this is the only way that really works with GeoFire. Doing what you are trying to do and hack your own solution into the GeoFire index itself is probably not going to work. You don’t have enough control over that.

Also, it’s important to realize that there is often a difference in security between your GeoFire index and your data. If you store them all together, you can’t separate their security rules. And since the GeoFire needs to be pretty open to work properly, that would mean your secure location data would also need to be open. Separating them allows you to give them separate security rules.

Finally, not including your data in the GeoFire index reduces the amount of data going across the wire. If you stored all that data in the index, every time a location moved would result in a lot of wasted bytes. If you do what I suggested, you would only load data that you actually need, when you need it.

You will notice our transit open data set is structured in exactly this way. The GeoFire index for it can be found here. If you look at that, you’ll see that the GeoFire keys (e.g. lametro:7869) are a combination of the transit provider and bus ID. This allows us to easily look up bus info by doing a ref.child(provider).child("vehicles").child(busId).once("value") call. This is exactly what we do in our SF MUNI demo code.

I’m going to close this issue but please feel free to respond if you still have questions.

3reactions
jwngrcommented, Jun 12, 2015

Yes, you should look them up individually by ID. This does require 100 calls, but since we use a websocket which has already established an HTTPS connection, it should be fast. It’s not like you are making 100 HTTPS requests which are all opening connections to our server.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Comply with Geolocation Data Privacy Regulations
It's true that there are many different laws governing how geolocation data can be collected, processed, and shared. And though these ...
Read more >
How to Navigate Geolocation and Data Protection Laws
Update your privacy policies to include geolocation collection practices. ... Under most privacy laws and regulations, consumers have the right to know how...
Read more >
The Power of Place: Geolocation Tracking and Privacy
[5] Aggregated location data (i.e., data that is identifiable by distinct data location points but not by individual) can help urban planners alleviate...
Read more >
Getting the current location of a device - Apple Developer
The Significant-change location service offers a low-power way to get location updates. This service uses the cellular and Wi-Fi radios (not GPS) to...
Read more >
Request location updates - Android Developers
Appropriate use of location information can be beneficial to users of your app. For example, if your app helps the user find their...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found