No-gyuho / trashcan

0 stars 1 forks source link

2021-08-03 수정본 #5

Open yedoong opened 3 years ago

yedoong commented 3 years ago

package com.example.googlemaps

import android.annotation.SuppressLint import android.content.pm.PackageManager.PERMISSION_GRANTED import android.graphics.Bitmap import android.graphics.drawable.BitmapDrawable import android.location.Location import android.os.Build import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.Looper import android.util.Log import android.widget.Toast import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat

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.example.googlemaps.databinding.ActivityMapsBinding import com.google.android.gms.location. import com.google.android.gms.maps.model. import com.google.android.gms.maps.model.LatLng import com.google.android.gms.maps.model.MarkerOptions

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

val permissions = arrayOf(
    android.Manifest.permission.ACCESS_FINE_LOCATION,
    android.Manifest.permission.ACCESS_COARSE_LOCATION
)
val PERM_FLAG = 99
private lateinit var mMap: GoogleMap

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_maps) //맵.xml
    //xml_parse();
    if (isPermitted()) {
        startProcess()
    } else {
        ActivityCompat.requestPermissions(this, permissions, PERM_FLAG)
    }

}

//권한 체크 함수
fun isPermitted(): Boolean {
    for (perm in permissions) {
        if (ContextCompat.checkSelfPermission(this, perm) != PERMISSION_GRANTED) {
            return false
        }
    }
    return true
}

fun startProcess() {
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    val mapFragment = supportFragmentManager
        .findFragmentById(R.id.map) as SupportMapFragment
    mapFragment.getMapAsync(this) //onMapReady 함수 호출
}

override fun onMapReady(googleMap: GoogleMap) {
    mMap = googleMap

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
    setUpdateLocationListener()

}

// -- 내 위치를 가져오는 코드
lateinit var fusedLocationClient: FusedLocationProviderClient //GPS+Network 좌표가져올때 배터리소모,정확도를 자동으로 조절해줌
lateinit var locationCallback: LocationCallback //fused의 응답코드

@SuppressLint("MissingPermission") //문법 검사기
fun setUpdateLocationListener() { //좌표값 갱신을 등록하는 함수
    val locationRequest = LocationRequest.create()
    locationRequest.run {
        priority = LocationRequest.PRIORITY_HIGH_ACCURACY //정확도를 높혀주지만 배터리소모는 빠름
        interval = 1000 //1초에 한번씩 좌표값 요청
    }

    locationCallback = object : LocationCallback() {
        override fun onLocationResult(locationResult: LocationResult?) {
            locationResult?.let {
                for ((i, location) in it.locations.withIndex()) {
                    Log.d("로케이션", "$i ${location.latitude}, ${location.longitude}")
                    setLastLocation(location)
                }
            }
        }
    }

    //location 요청 함수 호출 (locationRequest, locationCallback)
    fusedLocationClient.requestLocationUpdates(
        locationRequest,
        locationCallback,
        Looper.myLooper()
    )
}

//maker option and camera move
fun setLastLocation(location: Location) {
    val descriptor = getDescriptorFromDrawable(R.drawable.marker)   //marker icon

val myLocation = LatLng(37.55599978016992, 126.9723149532232) //mylocation 좌표값 서울역으로 변경해놨음 val marker = MarkerOptions() .position(myLocation) .title("I am here!") .icon(descriptor)

    // 1초씩 자기 위치 받아오는 거 + 카메라 이동이 고정이라 아예 다른 곳을 못 봄 일단 꺼놨음

// val cameraOption = CameraPosition.Builder() // .target(myLocation) // .zoom(15.0f) // .build() // var camera = CameraUpdateFactory.newCameraPosition(cameraOption)

    mMap.addMarker(marker)

// mMap.moveCamera(camera) }

//maker icon funtion
fun getDescriptorFromDrawable(drawableId: Int): BitmapDescriptor {
    var bitmapDrawable: BitmapDrawable
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        bitmapDrawable = getDrawable(drawableId) as BitmapDrawable
    } else {
        bitmapDrawable = resources.getDrawable(drawableId) as BitmapDrawable
    }

    //marker size
    val scaledBitmap = Bitmap.createScaledBitmap(bitmapDrawable.bitmap, 300, 400, false)
    return BitmapDescriptorFactory.fromBitmap(scaledBitmap)
}

}


//maker option and camera move 주석 확인 부탁합니당!!

No-gyuho commented 3 years ago
No-gyuho commented 3 years ago

val marker = MarkerOptions() .position(myLocation) .title("I am here!") .icon(descriptor) .anchor(0.5f, 0.5f)//화면 이동시에 커스텀 마커 위치가 잘 안 찍히는 문제 해결

yedoong commented 3 years ago

원래 대현이 코드는 GPS 버튼 눌러서 현재 위치로 돌아오게 할 때 쓰기