
Android telefonunda GPS ile kişinin konumunu almak için Location Manager veya FusedLocationProviderApi kullanıyoruz.
Google, yeni servisi olan FusedLocationProviderClient’ı kullanmamızı öneriyor. Bu derste, FusedLocationProviderClient’in neden daha iyi olduğunu ve nasıl uygulamamız gerektiğini açıklayacağız .
FusedLocationProviderClient nedir? Neden kullanmalıyız?
FusedLocationProviderClient diğer konum servislerine göre daha hızlı ve gereksiz kodlardan arındırılmış google’ın bize sunmuş olduğu yeni konum servisidir. Denildiğine göre bu servis diğerlerine göre pil tüketimi ve doğru konum için en iyi performansı bize sağlıyor.
( NOT: Bu özelliği kullanmak için, GPS cihazınızda açık olmalıdır. Kullanıcıdan GPS’yi açmasını manuel olarak isteyin, Gps açmasını istemek için makaleye bakın )
1) İlk olarak kütüphaneyi build.gradle(Module) da dependencies içerisine ekleyelim.
1 |
implementation 'com.google.android.gms:play-services-location:16.0.0' |
2) Android Manifest içerisinde gerekli konum izinlerini alalım.
1 2 |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> |
3) Yeni bir layout dosyası oluştuurun ve aşağıdaki kodu içerisine ekleyin
activity_maps.xml
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:map="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/map" android:name="com.google.android.gms.maps.SupportMapFragment" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MapsActivity" /> |
4) MapsActivity.java adlı java dosyamızdaki kodlar da aşağıdaki gibidir.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
package com.maps.unalzafer; import android.Manifest; import android.annotation.SuppressLint; import android.content.pm.PackageManager; import android.location.Location; import android.os.Looper; import android.support.v4.app.FragmentActivity; import android.os.Bundle; import android.support.v4.content.ContextCompat; import android.util.Log; import com.google.android.gms.location.FusedLocationProviderClient; import com.google.android.gms.location.LocationCallback; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationResult; import com.google.android.gms.location.LocationServices; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { private GoogleMap mMap; private LocationCallback locationCallback; FusedLocationProviderClient fusedLocationClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); fusedLocationClient = LocationServices.getFusedLocationProviderClient(this); locationCallback = new LocationCallback() { @Override public void onLocationResult(LocationResult locationResult) { if (locationResult == null) { return; } for (Location location : locationResult.getLocations()) { Log.d("locationControl", location.getLatitude()+"-"+location.getLongitude()); LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude()); mMap.addMarker(new MarkerOptions().position(myLocation).title(location.getLatitude()+"-"+location.getLongitude())); mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation,15)); } }; }; } @Override public void onMapReady(GoogleMap googleMap) { mMap = googleMap; mMap.clear(); } @Override protected void onResume() { super.onResume(); if (fusedLocationClient != null) { startLocationUpdates(); } } @SuppressLint("MissingPermission") private void startLocationUpdates() { LocationRequest locationRequest = LocationRequest.create(); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); locationRequest.setInterval(5000); locationRequest.setFastestInterval(1000); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)== PackageManager.PERMISSION_GRANTED) { fusedLocationClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper()); } } @Override protected void onPause() { super.onPause(); stopLocationUpdates(); } private void stopLocationUpdates() { if(fusedLocationClient!=null) { fusedLocationClient.removeLocationUpdates(locationCallback); } } } |
LogCat de görebilmek için locationControl yazarsanız konum değişinimi görebilirsiniz.
startLocationUpdates() methodumuz ile sürekli konum almayı yapabilirsiniz.
stopLocationUpdates() methodu konum verisi almayı durdurur.
LocationRequest ile konum değerleri hangi saniye hangi mesafe değişiminde tekrarlanmasını istediğimizi belirtiyoruz.
Umarım faydalı olmuştur.
Herkese iyi çalışmalar.
Bir yanıt bırakın