GeoFire ile Google Maps’de Realtime Lokasyon Sorguları

GeoFire kütüphanesi, açık kaynaklı olan Firebase ile birlikte lokasyon işlemlerini yapmamıza ve veritabanındaki var olan lokasyonları sorgulayıp bununla birlikte işlemler yapmamızı olanak sağlar.

GeoFire kütüphanesine buradan ulaşabilirsiniz.

Github Projesine Ulaşmak İçin Buradan

GeoFire çalışma prensibi ilk olarak, kullanıcının lokasyonunu alıp daha sonradan programda belirtilen aralıklar içerisinde var olan ve veritabanında var olan lokasyonları, kullanıcının lokasyonuna yakın olanları listeyecek şekilde çalışır. Bu kütüphane ile lokasyona bağlı uygulamalar ve sorgular yapabiliriz.

Örnek uygulama için ilk olarak Firebase’den bir proje oluşturup daha sonra bu projeyi android studio’da açtığımız proje ile tanımlıyoruz.

Gereklilik olarak ise Google Maps Activity açıp daha sonra aldığımız Google’dan aldığımız keyi strings klasörü altına ekliyoruz.

  1. GeoFire Kütüphane Ekleme

build.gradle Klasörü içine implements ediyoruz.

implementation 'com.firebase:geofire-android:2.1.1'

2. Gerekli Firebase Servisleri

implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.firebase:firebase-firestore:20.1.0'

implementation 'com.google.firebase:firebase-database:18.0.0'
implementation 'com.google.firebase:firebase-storage:18.1.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'


3. MapsActivity ile Lokasyon Alma
Burada ise kullanıcının lokasyonunu alma işlemini yapmamız lazım. Bunun için öncelikle android manifest dosyasına bu izinleri alıyoruz.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

4. Kullanıcıdan İzin Almak


@Override
public void onRequestPermissionsResult(int requestCode, @Nonnull String[] permissions, @Nonnull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mMap.setMyLocationEnabled(true);
}
} else {

Toast.makeText(getApplicationContext(), "Please grant permission!", Toast.LENGTH_SHORT).show();
}
}
}
private void checkLocationPermission() {

if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){

if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)){
new android.app.AlertDialog.Builder(this)
.setTitle("Permission")
.setMessage("Location Allow Use!")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MapsActivity.this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 1);

}
})
.create()
.show();
}
else {
ActivityCompat.requestPermissions(MapsActivity.this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, 1);

}
}
}


public void mlocationRequest(){
mLocationRequest = new LocationRequest();
//ınterval for active location updates, in milliseconds
mLocationRequest.setInterval(5000);
//This controls the fastest rate at which your application will receive location updates,
mLocationRequest.setFastestInterval(5000);
//the most acccurate locations available
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

}

5. OnMapReady ile Konumu Haritada Göstermek


@Override
public void onMapReady(GoogleMap googleMap) {

mMap = googleMap;

mlocationRequest();

if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){

mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
mMap.setMyLocationEnabled(true);


}else{
checkLocationPermission();
}

}

}
Firebase Realtime Kaydedilen Lokasyonlar
Realtime Kurallar’ı Düzenlemek. read ve erite true olmalı

6. LocationCallBack Metodu

LocationCallBack ile kullanıcının lokasyonunu dinler ve size enlem ve boylam olmak üzere koordinatlarını döndürür. LocationCallBack metodu içinde lokasyonları bir değişkene “latitude, longitude” atayıp daha sonradan GeoFire ile lokasyonu set edebiliyoruz. GeoFire.SetLocation ile kullanıcı konumu haritada Marker olarak gösteriyor.

LocationCallback mLocationCallback = new LocationCallback(){
    @Override
    public void onLocationResult(LocationResult locationResult) {
        for(final Location location : locationResult.getLocations()){

            mLastLocation = location;

            DatabaseReference ref = FirebaseDatabase.getInstance().getReference("offerAvailable");
            GeoFire geoFire = new GeoFire(ref);

            if(mLastLocation !=null){

                final double latitude = mLastLocation.getLatitude();
                final double longitude = mLastLocation.getLongitude();

                geoFire.setLocation("You",new GeoLocation(latitude, longitude),
                         new GeoFire.CompletionListener() {
                             @Override
                             public void onComplete(String key, DatabaseError error) {

                                 //ad marker
                                 if(myCurrent !=null){
                                     myCurrent.remove();
                                     myCurrent = mMap.addMarker
(new MarkerOptions()
                                        .position(new LatLng(latitude,longitude))
                                            .title("You'are"));

                                     //move camera
                                     mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude), 15.0f));
                                 }

                             }
                         });
            }
            setCircle(location);
            geoQuery.addGeoQueryEventListener(MapsActivity.this);
        }
    }
};

7. Circle Oluşturup Lokasyonları Sorgulamak

public void setCircle(Location location){

    mLastLocation = location;

    final double latitude = mLastLocation.getLatitude();
    final double longitude = mLastLocation.getLongitude();

    // Lists the last location in firebase.
    Log.d("mlastLocation", "location: " + location.getLatitude() + " " + location.getLongitude());

    //current location
    LatLng currentLocation = new LatLng(latitude,longitude);

    //old circle is deleted as the location refreshed.
    if(mapCircle!=null)
    {
        mapCircle.remove();
    }
    //Create a new circle.
    mapCircle=mMap.addCircle(new CircleOptions()
            .center(currentLocation)
            .radius(radius)  //500 metres
            .strokeColor(Color.BLUE)
            .fillColor(0x220000FF)
            .strokeWidth(5.0f));

    //0.5f = 0.5 km = 500m
    //In this code, it sends the location that will search within the radius range.
    geoQuery = geoFire.queryAtLocation(new GeoLocation(currentLocation.latitude, currentLocation.longitude), 0.5);

}

8. GeoQueryEventListener metodlarını implements Etmek

GeoQueryEventListener' implements ettikten sonra metodlarını override etmemiz gerekiyor.

onKeyExited: Lokasyonlar, alanın dışındaysa burası çalışır.
onKeyEntered: Lokasyonlar, alanın içindeyse bu metod çalışır.
onKeyMoved: Lokasyonlar değişirse, bir bildirim atar.
@Override
public void onKeyEntered(String key, GeoLocation location) {

    //Users in the circle.

    myCurrent= mMap.addMarker(new MarkerOptions().position(new LatLng(location.latitude, location.longitude))
                                .title("Users"));
    //list the all users location in firebase database
    Log.d("DbLocation", key +" :"  + location.latitude + " " +location.longitude);

}

@Override
public void onKeyExited(String key) {

    //users outside the circle.

}

@Override
public void onKeyMoved(String key, GeoLocation location) {

}

@Override
public void onGeoQueryReady() {

}

@Override
public void onGeoQueryError(DatabaseError error) {

}


Çıktı

Github Projesine Ulaşmak İçin Buradan

İnternet sitesi https://www.merttoptas.com
Yazı oluşturuldu 18

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Benzer yazılar

Aramak istediğinizi üstte yazmaya başlayın ve aramak için enter tuşuna basın. İptal için ESC tuşuna basın.

Üste dön
%d blogcu bunu beğendi: