์ขํ ํ์ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ก ์ฝ๋ ์์ฑํ๊ธฐ
ํ๊ฒฝ๋ณ์ ์ค์ ํ๊ธฐ
<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 ํ๋ฉด ๋๋ค.