๊ธฐ๋ณธ์ ์ผ๋ก ์๋/๊ฒฝ๋๋ ๋/๋ถ/์ด๋ก ํ์๋๋๋ฐ ์ผ๋ฐ์ ์ธ ์์คํ
์์๋ ์ค์ํ ํ์
์ผ๋ก ์ ๊ณต๋๋ค.
ex) 37๋ 30๋ถ 30์ด โ 37.5๋
์ ์ ์์น ์ ๋ณด๋ฅผ ํ๋ํ๋ ๋ํ์ ์ธ ๋ฐฉ๋ฒ์ผ๋ก๋ LocationManager๊ณผ Fused API๊ฐ ์๋ค.
val manager = getSystemService(LOCATION_SERVICE) as LocationManager
val location: Location? = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
package com.tutorial.c81
import android.Manifest
import android.content.pm.PackageManager
import android.location.LocationManager
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
class MainActivity : AppCompatActivity() {
lateinit var resultView: TextView
lateinit var manager: LocationManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
resultView = findViewById(R.id.resultView)
manager = getSystemService(LOCATION_SERVICE) as LocationManager
val launcher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if(isGranted){
getLocation()
}else{
Toast.makeText(this, "denied...", Toast.LENGTH_SHORT).show()
}
}
val status = ContextCompat.checkSelfPermission(this,
"android.permission.ACCESS_FINE_LOCATION")
if(status == PackageManager.PERMISSION_GRANTED){
getLocation()
}else{
launcher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
}
}
private fun getLocation() {
val location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
location?.let {
val latitude = location.latitude
val longitude = location.longitude
val accuracy = location.accuracy
val time = location.time
resultView.text = "$latitude\n $longitude\n $accuracy\n $time"
}
}
}
implementation 'com.google.android.gms:play-services:12.0.1'
Fused Location Provider์ ํต์ฌ ํด๋์ค 2๊ฐ์ง
package com.tutorial.c82
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.api.GoogleApiClient
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
class MainActivity : AppCompatActivity() {
lateinit var resultView: TextView
lateinit var providerClient: FusedLocationProviderClient
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
resultView = findViewById(R.id.resultView)
val apiClient = GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(connectionCallback)
.addOnConnectionFailedListener(connectionFailedCallback)
.build()
providerClient = LocationServices.getFusedLocationProviderClient(this)
val launcher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
){
if(it){
apiClient.connect()
}else{
Toast.makeText(this, "denied...", Toast.LENGTH_SHORT).show()
}
}
val status = ContextCompat.checkSelfPermission(this,
"android.permission.ACCESS_FINE_LOCATION")
if(status == PackageManager.PERMISSION_GRANTED){
apiClient.connect()
}else{
launcher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
}
}
private val connectionCallback = object: GoogleApiClient.ConnectionCallbacks {
// ์์น ์ ๋ณด ์ ๊ณต์๊ฐ ์ฌ์ฉ ๊ฐ๋ฅํ ์ํ๊ฐ ๋์์ ๋
override fun onConnected(p0: Bundle?) {
providerClient.lastLocation.addOnSuccessListener {
val latitude = it?.latitude
val longitude = it?.longitude
resultView.text = "$latitude, $longitude"
}
}
// ์์น ์ ๋ณด ์ ๊ณต์๊ฐ ์ฌ์ฉ ๋ถ๊ฐ๋ฅํ ์ํ๊ฐ ๋์์ ๋
override fun onConnectionSuspended(p0: Int) {
}
}
private val connectionFailedCallback = object: GoogleApiClient.OnConnectionFailedListener {
override fun onConnectionFailed(p0: ConnectionResult) {
}
}
}
implementation 'com.google.android.gms:play-services:12.0.1'
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tutorial.c83">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidLab">
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value=""/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.tutorial.c83
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
class MainActivity : AppCompatActivity(), OnMapReadyCallback {
var googleMap: GoogleMap? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
(supportFragmentManager.findFragmentById(R.id.mapView) as SupportMapFragment)
.getMapAsync(this)
}
override fun onMapReady(p0: GoogleMap?) {
googleMap = p0
val latLng = LatLng(37.566610, 126.978403) // ์์ธ ์์ฒญ์ ์์น
val position = CameraPosition.Builder() // ๋ณด์ฌ์ค ์ง๋ ์์น ์ค์
.target(latLng)
.zoom(16f)
.build()
googleMap?.moveCamera(CameraUpdateFactory.newCameraPosition(position))
}
}
Settings > Experimental > Do not build Gradle task list during Gradle sync ์ฒดํฌ ํด์
์๋๋ก์ด๋ ์คํ๋์ค ์ค๋ฅธ์ชฝ์ Gradle ํญ > signingReport
cf) ํ ์คํธ์ฉ์ด ์๋๋ผ, ์ ์ ์ฑ์ ์ํ SHA-1 ํค๋ฅผ ์ป์ผ๋ ค๋ฉด ์ดํ๋ฆฌ์ผ์ด์ ์ ๋น๋ํ๋ฉด์ ์ฌ์ธํ๋ ํค๋ฅผ ์ด์ฉํด์ผ ํ๋ค.
https://developers.google.com/maps/documentation/android-sdk/get-api-key
์๋ value์ API ํค๋ฅผ ๋ฑ๋กํด์ค๋ค.
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="~~~~~"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
ํน์ ์๋๋ก์ด๋ 29๋ฒ์ ๋ฐ์ด๋ฉด ๋ฐฑ๊ทธ๋ผ์ด๋ ๊ถํ๋ง๋ฐ์ผ๋ฉด ์ฑ๋ด๋ ค๊ฐ๋ watchPostion ๊ฒ์ ๋ฐ์ดํฐ ๋ฐ๊ณ ์๋์ ?