๋ฐ์ํ
Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- ์น๋์๋ณด๋ ๊ธฐ์ด
- ํ๋ค์ค๊ณต๋ถ
- MySQL
- ์๋ฐํ๋ก๊ทธ๋๋ฐ
- Streamlit๊ธฐ์ด
- ์๋๋ก์ด๋ ์คํ๋์ค
- ์๋ฐ
- ์๋ฐ์ด๋ณด
- java
- ์๋ฐ๊ธฐ์ด
- ์คํธ๋ฆผ๋ฆฟ
- RESTful API
- Matplotlib ๊ธฐ์ด
- ์๋ฐ๊ณต๋ถ
- db
- Android
- ํ์ด์ฌ๊ณต๋ถ
- serverless
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ณต๋ถ
- ํ๋ค์ค
- ํ์ด์ฌ
- ์คํธ๋ฆผ๋ฆฟ ๊ธฐ๋ณธ
- Pandas
- ์๋๋ก์ด๋ ์ฑ ๊ฐ๋ฐ
- streamlit
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- JavaScript
- Streamlit๊ธฐ๋ณธ
- ์น๋์๋ณด๋ ์ ์
Archives
- Today
- Total
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
);
}
}
}
- ์๋/๊ฒฝ๋ ๋ก๊ทธ ํ์ธ.
๋ฐ์ํ