Android ile Kişinin Konumunu Bulmak

AndroidManifest.xml dosyasına ekleyiniz;
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

KonumServisi.java oluşturup aşağıdaki kodları yazınız;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;

public class KonumServisi extends Service implements LocationListener {

    private final Context mContext;
    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;
    Location konum;
    double enlem; // latitude
    double boylam; // longitude

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
    protected LocationManager locationManager;

    public KonumServisi(Context context) {
        this.mContext = context;
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        konum = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                        if (konum != null) {
                            enlem = konum.getLatitude();
                            boylam = konum.getLongitude();
                        }
                    }
                }

                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (konum == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            konum = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);

                            if (konum != null) {
                                enlem = konum.getLatitude();
                                boylam = konum.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return konum;
    }

    public void stopUsingGPS(){
        if(locationManager != null){
            locationManager.removeUpdates(KonumServisi.this);
        }
    }

    public double getEnlem(){
        if(konum != null){
            enlem = konum.getLatitude();
        }
        return enlem;
    }

    public double getBoylam(){
        if(konum != null){
            boylam = konum.getLongitude();
        }
        return boylam;
    }


    public boolean getKonumaErisim() {
        return this.canGetLocation;
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
}

MainActivity.java için;

public class MainActivity extends AppCompatActivity {

    KonumServisi konumServisi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        konumServisi = new KonumServisi(getApplicationContext());
        if(konumServisi.getKonumaErisim()){
            double enlem=konumServisi.getEnlem();
            double boylam=konumServisi.getBoylam();
            Log.d("KONUM_SERVISI","ENLEM: "+enlem);
            Log.d("KONUM_SERVISI","BOYLAM: "+boylam);
        }else{

            AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
            adb.setTitle("GPS ayarları");
            adb.setMessage("Konum servisi kapalıdır. Açmak için Düzenle'ye tıklayınız.");
            adb.setPositiveButton("Düzenle", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {
                    Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                    startActivity(intent);
                }
            });
            adb.setNegativeButton("Vazgeç", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            adb.show();
        }
    }
}
Bu makaleyi paylaşın:

3 Comments

Bir yorum bırakın