* GoogleMaps랑 기본적으로 비슷
build.gradle
implementation 'com.google.android.gms:play-services-location:17.0.0'
implementation 'com.google.android.gms:play-services-maps:17.0.0'
google_maps_api
google map key 입력
새로만들기 > Fragment > Google Maps Fragment 생성
activity_main.xml
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="622dp"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="256dp">
</FrameLayout>
<TextView
android:text="주소"
android:layout_width="61dp"
android:layout_height="31dp" android:id="@+id/textView"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="36dp" android:layout_marginStart="44dp"/>
<EditText
android:layout_width="425dp"
android:layout_height="47dp"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editAddr"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintVertical_bias="0.028"
app:layout_constraintStart_toEndOf="@+id/textView" android:layout_marginStart="16dp"/>
<EditText
android:layout_width="224dp"
android:layout_height="44dp"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editLat"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="48dp" app:layout_constraintTop_toBottomOf="@+id/button"
android:layout_marginTop="20dp"/>
<EditText
android:layout_width="226dp"
android:layout_height="41dp"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editLon"
app:layout_constraintStart_toEndOf="@+id/editLat"
android:layout_marginStart="44dp" app:layout_constraintTop_toTopOf="@+id/editLat"/>
<Button
android:text="위도, 경도 변환후 지도 보기"
android:layout_width="230dp"
android:layout_height="50dp"
android:id="@+id/button"
app:layout_constraintTop_toBottomOf="@+id/editAddr"
android:layout_marginTop="36dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="184dp"/>
MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val fm = supportFragmentManager
val fragmentTransaction = fm.beginTransaction()
val mapsFragment = MapsFragment(this)
fragmentTransaction.add(R.id.content, mapsFragment) // <- 수정됨
fragmentTransaction.commit()
var button1: Button = findViewById<Button>(R.id.button) // 위도, 경로로 변환
val editText: EditText = findViewById<EditText>(R.id.editAddr) // 주소입력
val editText1:EditText = findViewById<EditText>(R.id.editLat) // 위도입력
val editText2:EditText = findViewById<EditText>(R.id.editLon) // 경도입력
val geocoder: Geocoder = Geocoder(this)
var latitude:Double = -34.0
var longitude:Double = 151.0
// 변환1 주소 -> 위도, 경도
button1.setOnClickListener{
var list: List<Address>? = null
val str: String = editText.text.toString()
try {
list = geocoder.getFromLocationName(
str, // 지역 이름
10
) // 읽을 개수
} catch (e: IOException) {
e.printStackTrace()
Log.e("test", "입출력 오류 - 서버에서 주소변환시 에러발생")
}
if (list != null) {
if (list!!.isEmpty()) {
Toast.makeText(this, "해당되는 주소 정보는 없습니다", Toast.LENGTH_LONG).show()
} else {
// editText1.text 는 안된다
editText1.setText(list!![0].latitude.toString())
editText2.setText(list!![0].longitude.toString())
latitude = list!![0].latitude
longitude = list!![0].longitude
// 위도,경도 입력 후 지도
mapsFragment.setLocation(latitude, longitude)
}
}
}
}
}
MapsFragment
class MapsFragment(val activity: Activity) : Fragment(), OnMapReadyCallback{
// 위치 권한
lateinit var locationPermission: ActivityResultLauncher<Array<String>>
private lateinit var mMap: GoogleMap
// 위치 서비스가 GPS를 사용해서 위치를 확인
lateinit var fusedLocationClient: FusedLocationProviderClient
// 위치 값 요청에 대한 갱신 정보를 받는 변수
lateinit var locationCallback: LocationCallback
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? {
// 권한 승인
locationPermission = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions() ){ results->
if(!results.all { it.value }){
Toast.makeText(activity, "권한 승인이 필요합니다", Toast.LENGTH_LONG).show()
}
}
// 권한 요청
locationPermission.launch(
arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)
)
return inflater.inflate(R.layout.fragment_maps, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val mapFragment = childFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
}
override fun onMapReady(googleMap: GoogleMap) {
mMap = googleMap
fusedLocationClient = LocationServices.getFusedLocationProviderClient(activity)
// updateLocation() 이건 현재 필요 없음
}
fun setLocation(latitude:Double, longitude:Double){
val LATLNG = LatLng(latitude, longitude)
val markerOptions = MarkerOptions()
.position(LATLNG)
.title("Here!")
val cameraPosition = CameraPosition.Builder()
.target(LATLNG)
.zoom(15.0f)
.build()
mMap.clear()
mMap.addMarker(markerOptions)
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition))
}
}
'안드로이드' 카테고리의 다른 글
[Android] 숫자 야구 (0) | 2022.04.06 |
---|---|
[Android] 숫자 +/- (0) | 2022.04.05 |
[Android] Geocoding (0) | 2022.04.03 |
[안드로이드] Camera (0) | 2022.04.03 |
[Android] 동적버튼 (0) | 2022.04.03 |
댓글