Android

์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค GPS ๊ธฐ๋Šฅ์œผ๋กœ ์ง€๋„์— ๋งˆ์ปค ์ถ”๊ฐ€ํ•˜๊ธฐ

567Rabbit 2024. 6. 18. 15:15

 

์ขŒํ‘œ ํ‘œ์‹œํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ

 

ํ™˜๊ฒฝ๋ณ€์ˆ˜ ์„ค์ •ํ•˜๊ธฐ

 

 

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

 

 

 

 

MainActivity.java

 

package com.~.gps;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import java.util.List;


public class MainActivity extends AppCompatActivity {

    LocationListener locationListener;
    LocationManager locationManager;

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

        // 1. ์œ„์น˜๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ์‹œ์Šคํ…œ ์„œ๋น„์Šค๋กœ๋ถ€ํ„ฐ ๋กœ์ผ€์ด์…˜ ๋งค๋‹ˆ์ €๋ฅผ ๋ฐ›์•„์™€์•ผ ํ•œ๋‹ค.
        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // 2. ์œ„์น˜๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์šฐ๋ฆฌ๊ฐ€ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•  ํ•จ์ˆ˜ ์ž‘์„ฑ
        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {
                Log.i("GPS MAIN", "์œ„๋„ : " + location.getLatitude() + " , ๊ฒฝ๋„ : " + location.getLongitude());
            }
        };

        // 3. ๋กœ์ผ€์ด์…˜ ๋งค๋‹ˆ์ €์—, ์šฐ๋ฆฌ๊ฐ€ ์ž‘์„ฑํ•œ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•œ๋‹ค.
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(this, android.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;
        }

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                3000,
                -1,
                locationListener);  //minTimeMs์ดˆ๋งˆ๋‹ค ํ•œ๋ฒˆ์”ฉ ๋˜๋Š” minDistanceM๋ฏธํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ํ•œ๋ฒˆ์”ฉ ์‹คํ–‰ ์‹œํ‚จ๋‹ค.
    }


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

            if (requestCode == 100) { // ์š”์ฒญ ์ฝ”๋“œ๊ฐ€ 100์ผ ๋•Œ๋งŒ ์ฒ˜๋ฆฌ
                // grantResults ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ํ™•์ธํ•˜์—ฌ IndexOutOfBoundsException ๋ฐฉ์ง€
                if (grantResults.length > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
                    // ์œ„์น˜ ๊ถŒํ•œ์ด ์Šน์ธ๋˜์—ˆ์„ ๋•Œ
                    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                            && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

                        if (locationManager != null) { // locationManager๊ฐ€ null์ด ์•„๋‹Œ์ง€ ํ™•์ธ
                            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                    3000,
                                    -1,
                                    locationListener); // minTimeMs์ดˆ๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ๋˜๋Š” minDistanceM๋ฏธํ„ฐ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ํ•œ ๋ฒˆ์”ฉ ์‹คํ–‰
                        }
                    }
                }
            }
        }

}

 

 

 

์‹คํ–‰์‹œํ‚ค๋ฉด, ๋กœ๊ทธ์บฃ์—์„œ ํ˜„์žฌ ๊ธฐ๊ธฐ์— ๋“ฑ๋ก๋œ ๋‚˜์˜ ์œ„์น˜์˜ ์œ„๋„ ๊ฒฝ๋„๋ฅผ ์•Œ๋ ค์ค€๋‹ค.

 

 

 

 

 

์‹คํ–‰์‹œํ‚จ ์ƒํƒœ์—์„œ, ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ƒˆ ์ฐฝ์œผ๋กœ GPS๊ฐ€ ๋œฌ๋‹ค.

 

 

 

 

 

์ง€์ •ํ•˜๊ณ  ์‹ถ์€ ์œ„์น˜๋ฅผ ํด๋ฆญํ•˜๊ณ  SavePoint๋ฅผ ํ•œ ๋’ค, import GPX/KML ๋ˆŒ๋Ÿฌ์ค€๋‹ค

๊ทธ๋ฆฌ๊ณ  Set Location ํ•˜๋ฉด ๋œ๋‹ค.