A simple demos of how to connect firebase and google maps
GeoFirebase is a library to help you query Firebase Real-Time Database based on Geolocations on Android
There is no reason at all why you shouldn't use GeoFire, is a great library, and the people contributing to it are amazing developers. I made this library as an alternative to GeoFire because:
That is what you get! Easy Firebase Database queries + Geolocations, while keeping control of your Data Structure
If you are not convinced yet on using this library, let me offer you one more perk: Cool gifs in the documentation, so you have fun reading it :)
tools\Firebase
compile 'cl.cutiko:geofirebase:0.1.0'
You can look at this gist, for a quick example, but please remember your project have to add the google-service.json
Is very simple:
GeoPod
GeoEvent
Since the library is ment to be used with Geolocations, then the basic model provided makes sure the object being query have: longitude and latitude, also a key.
So the model you are gonna use for your place, cofee shop, or whatever is about your app have to extend GeoPod
public class GeoPlace extends GeoPod {
//Every variable you need, name, category, rating, etc.
//You need the empty constructor
//Getters and setters, always, getters and setters, say it slow, say it loud, but don't forget it
}
The code below is the anonymous instantiation mentioned before. Between the angle brackets, you have to pass your model, the same you use in the previous step.
Then in the constructor, the first 3 arguments are for the query. The longitude and latitude will be used as a center, and the third param will be the radius to query the data.
The fourth param is your model ass a class, pass it ass in the example YourModel.class
, include the dot class.
The last 2 arguments in the constructor are for doing the query in the database. Since the promise of this library is to allow you control over your data structure, then you have to tell where to query.
The first String is the route in the database where the data is stored, and the second is a funnel node to filter the data better (more information further).
new GeoEvent<GeoPlace>(
latitude,
longitude,
GeoDistances.TWO_KM,
GeoPlace.class,
"locations",
"cl"
) {
@Override
protected void results(List<GeoPlace> geoPods) {
for (GeoPlace geoPlace : geoPods) {
LatLng latLng = new LatLng(geoPlace.getLatitude(), geoPlace.getLongitude());
Marker marker = googleMap.addMarker(new MarkerOptions().position(latLng));
marker.setTag(geoPlace);
}
}
};
In this example when the data is return every object is placed on a marker in a Google Map
If your are not familiar with doing this kind of implementations, don't worry about it. Take a deep breath and do this: write new GeoEven<>
pass inside the angle brackets <>
your model, and then pass every argument to the constructor, one last thing that should fix everything: press alt+enter and select implement methods.
If you want to know more about the arguments in the constructor, you can read more about the radius and the funnel node in the wiki.
You can do whatever you want to upload your data, you can simply setValue(yourObject)
or do something more as updadateChilder()
. The library will take care of the data query in any node you ask. To keep this readme short if you want to know more about this, please see the wiki page about uploading data
Since you are gonna work with indexing, then you should use indexing, please read the Firebase Docs. This is what is being used in the example app:
{
"rules": {
".read": true,
".write": true,
"locations": {
"cl": {
".indexOn": ["latitude"]
}
}
}
}
true
regardless login or not, please mind this security, this is only for demostration purpouse. Set this rules in your database rules.Google Maps Activity
that will create an .xml
file inside the values
folder, look at it and follow the instructions there. When you are finish you can copy paste that key or the whole file to this project.FirebaseRecyclerAdapter
and the FirebaseIndexRecyclerAdapter
FirebaseRecyclerAdapter
for Google Maps would be really coolSuggestions and PRs are welcome