๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ๋‚ด ์œ„์น˜ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ, GPS ๊ถŒํ•œ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ณธ๋ฌธ

๐Ÿค–Android/Java

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - ๋‚ด ์œ„์น˜ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ, GPS ๊ถŒํ•œ ์„ค์ •ํ•˜๋Š” ๋ฐฉ๋ฒ•

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 1. 11. 09:23
๋ฐ˜์‘ํ˜•

์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ๊ฐœ๋ฐœ ์‹œ GPS ์„ค์ •์„ ํ•˜์—ฌ ์œ„๋„, ๊ฒฝ๋„๋กœ ๋””๋ฐ”์ด์Šค์˜ ์œ„์น˜์˜ ๊ฐ’์„ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ๋‹ค.

 

1. AndroidManifest.xml

์ด ๊ถŒํ•œ์€ ๊ณ ์ •๋œ ์œ„์น˜์— ์•ก์„ธ์Šคํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•œ๋‹ค.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

 

2. MainActivity.java

2.1. LocationManager์™€ LocationListener์˜ ๋ฉค๋ฒ„๋ณ€์ˆ˜ ์„ ์–ธ

public class MainActivity extends AppCompatActivity {

    LocationManager locationManager;
    LocationListener locationListener;

}

 

2.2. onCreate()

- ์œ„์น˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š”, ์‹œ์Šคํ…œ์„œ๋น„์Šค๋กœ๋ถ€ํ„ฐ locationManager๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.

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

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
}

 

 

- ๋ฆฌ์Šค๋„ˆ ์ƒ์„ฑํ•˜๊ณ  ์œ„์น˜๊ฐ€ ๋ณ€ํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜ onLocaionChanged๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(@NonNull Location location) {

        // ์œ„๋„, ๊ฒฝ๋„๊ฐ’์„ ์ถ”์ถœ, ์—ฌ๊ธฐ์—์„œ ํ™œ์šฉํ•  ์ฝ”๋“œ ์ž‘์„ฑ
        // ๋กœ๊ทธ๋กœ ์œ„์น˜ ํ™•์ธ.

        double lat = location.getLatitude(); // ์œ„๋„

        double lng = location.getLongitude(); // ๊ฒฝ๋„

        Log.i("AAA","์œ„๋„ : "+ lat);
        Log.i("AAA","๊ฒฝ๋„ : "+ lng);
        Log.i("AAA","  ");

    }
};

 

- ๊ถŒํ•œ์„ ํ—ˆ์šฉํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

if(ActivityCompat.checkSelfPermission(MainActivity.this,
        Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
        ActivityCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){

    ActivityCompat.requestPermissions(MainActivity.this,
            new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                    Manifest.permission.ACCESS_COARSE_LOCATION},
            100);
    return;
}

 

- ๋งค๋‹ˆ์ €์—๊ฒŒ ์—…๋ฐ์ดํŠธ ๋  ๋•Œ๋งˆ๋‹ค ์œ„์— ๋งŒ๋“  ์ฝ”๋“œ๊ฐ€ ๋™์ž‘ํ•œ๋‹ค.

3000 -> 3์ดˆ
10 -> 10m

locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER,
        3000, 10,
        locationListener
);

์•ˆ์›€์ง์ผ ๋•Œ๋Š” -1๋กœ ๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.

locationManager.requestLocationUpdates(
        LocationManager.GPS_PROVIDER,
        3000, -1,
        locationListener
);

 

2.3. ๊ถŒํ•œ ๊ด€๋ จ, GPS ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ( ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ)

์ด ์ฝ”๋“œ๋Š” onCreate() ํ•จ์ˆ˜ ๋ฐ–์— ์œ„์น˜ํ•œ๋‹ค.

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

    if(requestCode == 100){

        // ํ—ˆ์šฉํ•˜์ง€ ์•Š์•˜์œผ๋ฉด, ๋‹ค์‹œ ํ—ˆ์šฉํ•˜๋ผ๋Š” ์•Œ๋ŸฌํŠธ ๋„์šด๋‹ค.
        if(ActivityCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.checkSelfPermission(MainActivity.this,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){

            finish();
            return;
        }

        // ํ—ˆ์šฉํ–ˆ์œผ๋ฉด, GPS ์ •๋ณด ๊ฐ€์ ธ์˜ค๋Š” ์ฝ”๋“œ ๋„ฃ๋Š”๋‹ค.
        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                3000, -1,
                locationListener
        );

    }


}

 

 

3. ์ „์ฒด ์ฝ”๋“œ ๋ฐ ๋กœ๊ทธ ํ™•์ธ

 

- ์ „์ฒด์ฝ”๋“œ

public class MainActivity extends AppCompatActivity {

    LocationManager locationManager;
    LocationListener locationListener;

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

        // ์œ„์น˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š”, ์‹œ์Šคํ…œ์„œ๋น„์Šค๋กœ๋ถ€ํ„ฐ
        // locationManager๋ฅผ ๋ฐ›์•„์˜จ๋‹ค.

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);


        // ๋ฆฌ์Šค๋„ˆ๋ฅผ ๋งŒ๋“ ๋‹ค.
        // ์œ„์น˜๊ฐ€ ๋ณ€ํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑ!

        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {

                // ์œ„๋„, ๊ฒฝ๋„๊ฐ’์„ ๋ฝ‘์•„์„œ, ์šฐ๋ฆฌ์— ๋งž๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

                double lat = location.getLatitude(); // ์œ„๋„

                double lng = location.getLongitude(); // ๊ฒฝ๋„

                Log.i("AAA","์œ„๋„ : "+ lat);
                Log.i("AAA","๊ฒฝ๋„ : "+ lng);
                Log.i("AAA","  ");

            }
        };

        //๊ถŒํ•œ ํ—ˆ์šฉํ•˜๋Š” ์ฝ”๋“œ

        if(ActivityCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
                ActivityCompat.checkSelfPermission(MainActivity.this,
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){

            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
                            Manifest.permission.ACCESS_COARSE_LOCATION},
                    100);
            return;
        }

        // ๋งค๋‹ˆ์ €์—๊ฒŒ ์—…๋ฐ์ดํŠธ ๋  ๋•Œ๋งˆ๋‹ค ์•„๊นŒ ๋งŒ๋“  ์ฝ”๋“œ ๋™์ž‘ํ•˜๋„๋ก ํ•ด.
        // 3์ดˆ(3000) , 10m / ์•ˆ์›€์ง์ผ ๋•Œ๋Š” -1

        locationManager.requestLocationUpdates(
                LocationManager.GPS_PROVIDER,
                3000, -1,
                locationListener
        );

    }

    // ๊ถŒํ•œ ๊ด€๋ จ. GPS ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ(์žฌ์‚ฌ์šฉ ์ฝ”๋“œ)

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

        if(requestCode == 100){

            // ํ—ˆ์šฉํ•˜์ง€ ์•Š์•˜์œผ๋ฉด, ๋‹ค์‹œ ํ—ˆ์šฉํ•˜๋ผ๋Š” ์•Œ๋ŸฌํŠธ ๋„์šด๋‹ค.
            if(ActivityCompat.checkSelfPermission(MainActivity.this,
                    Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED ||
                    ActivityCompat.checkSelfPermission(MainActivity.this,
                            Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){

                finish();
                return;
            }

            // ํ—ˆ์šฉํ–ˆ์œผ๋ฉด, GPS ์ •๋ณด ๊ฐ€์ ธ์˜ค๋Š” ์ฝ”๋“œ ๋„ฃ๋Š”๋‹ค.
            locationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    3000, -1,
                    locationListener
            );

        }


    }
}

 

- ์œ„๋„/๊ฒฝ๋„ ๋กœ๊ทธ ํ™•์ธ.

๋ฐ˜์‘ํ˜•