Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ interface PinInterface {
fun getLocations(
@Path("userId") userId: Int,
): Call<PinResponse>

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.example.romanticyeojido.network.map

data class PinResponse(
val success: Boolean,
val locations: List<LocationData>
val result: List<LocationData>
)

data class LocationData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import retrofit2.http.Query
interface PopupInterface {
@GET("/api/v1/users/{userId}/map/memory") // 요청 경로
fun getPopupData(
@Header("Authorization") accessToken: String, // Authorization 헤더를 요청에 추가
@Header("Content-Type") contentType: String = "application/json",
@Path("userId") userId: Int,
@Query("latitude") latitude: String,
@Query("longitude") longitude: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.example.romanticyeojido.network.map

data class PopupResponse(
val success: Boolean,
val memory: MemoryData
val result: MemoryData
)

data class MemoryData(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.example.romanticyeojido.network.map.PopupResponse
import okhttp3.MultipartBody
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.Multipart
Expand Down Expand Up @@ -38,4 +39,12 @@ interface ImageService {
@Path("memoryId") memoryId : Int,
@Part images: List<MultipartBody.Part>
): Call<ResponseBody>
}

interface MemoryContentInterface {
@GET("/api/v1/users/{userId}/locations/{locationId}/memory-content")
suspend fun getMemoryContent(
@Path("userId") userId: Int,
@Path("locationId") locationId: Int
): Response<MemoryContentResponse>
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,27 @@ data class MemoryResult(
@SerializedName(value = "friends") val friends: String,
@SerializedName(value = "content") val content: String,
@SerializedName(value = "summary") val summary: String
)

data class MemoryContentResponse(
val result: MemoryContent
)

data class MemoryContent(
val id: Int,
val user_id: Int,
val location_id: Int,
val title: String,
val visit_date: String,
val friends: String,
val content: String,
val summary: String,
val is_deleted: Int,
val created_at: String,
val updated_at: String
)

data class ErrorResponse(
val success: Boolean,
val message: String
)
146 changes: 115 additions & 31 deletions app/src/main/java/com/example/romanticyeojido/ui/map/MapActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.example.romanticyeojido.ui.map

import android.content.Context.MODE_PRIVATE
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.example.romanticyeojido.R
import com.example.romanticyeojido.databinding.ActivityMapBinding
import com.example.romanticyeojido.network.map.PinInterface
Expand All @@ -14,6 +16,9 @@ import com.example.romanticyeojido.network.map.NewpinInterface
import com.example.romanticyeojido.network.map.NewpinRequest
import com.example.romanticyeojido.network.map.NewpinResponse
import com.example.romanticyeojido.network.RetrofitClient
import com.example.romanticyeojido.network.memoryPost.MemoryContentInterface
import com.example.romanticyeojido.network.memoryPost.MemoryInterface
import com.example.romanticyeojido.network.memoryPost.MemoryService
import com.example.romanticyeojido.ui.memoryPost.MemoryPostActivity
import com.kakao.vectormap.KakaoMap
import com.kakao.vectormap.KakaoMapReadyCallback
Expand All @@ -25,6 +30,7 @@ import com.kakao.vectormap.label.LabelLayer
import com.kakao.vectormap.label.LabelOptions
import com.kakao.vectormap.label.LabelStyle
import com.kakao.vectormap.label.LabelStyles
import kotlinx.coroutines.launch
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
Expand Down Expand Up @@ -60,7 +66,7 @@ class MapActivity: AppCompatActivity() {
sendNewPin(userId,latitude,longitude) // 서버에 핀 정보 전송

val intent = Intent(this, MemoryPostActivity::class.java).apply{}

onPause()
startActivity(intent)
clearUnsavedLabel() // 라벨 초기화
} ?: Toast.makeText(this, "핀을 선택해주세요!", Toast.LENGTH_SHORT).show()
Expand All @@ -71,20 +77,20 @@ class MapActivity: AppCompatActivity() {
}

// val locationId = intent.getIntExtra("locationId", 0)
// val title = intent.getStringExtra("title")
// val visit_date = intent.getStringExtra("visit_date")
// val content = intent.getStringExtra("content")
// val friends = intent.getStringExtra("friends")
// val summary = intent.getStringExtra("summary")

// Log.d("MapActivity", "받은 memory_data: $locationId, $title, $visit_date, $content, $friends, $summary")

initializeMap()
// val title = intent.getStringExtra("title")
// val visit_date = intent.getStringExtra("visit_date")
// val content = intent.getStringExtra("content")
// val friends = intent.getStringExtra("friends")
// val summary = intent.getStringExtra("summary")
//
// Log.d("MapActivity", "받은 추억 데이터: $title, $visit_date, $content, $friends, $summary")

initializeMap(userId)
fetchLocations(userId)
}

//API 호출 - 저장된 핀 호출
private fun fetchLocations(userId: Int) {
fun fetchLocations(userId: Int) {
val call = pinInterface.getLocations(userId)

call.enqueue(object : Callback<PinResponse> {
Expand All @@ -93,44 +99,35 @@ class MapActivity: AppCompatActivity() {
val pinResponse = response.body()
if (pinResponse != null && pinResponse.success) {
// locations가 null이 아닌지 확인
if (pinResponse.locations != null) {
pinResponse.locations.forEachIndexed { index, location ->
if (pinResponse.result != null) {
pinResponse.result.forEachIndexed { index, location ->
Log.d("Location", "위치 $index: 위도: ${location.latitude}, 경도: ${location.longitude}")

val styles = kakaoMap?.labelManager?.addLabelStyles(
LabelStyles.from(
LabelStyle.from(R.drawable.ic_pin_gray).setAnchorPoint(0.5f, 1.0f)
)
)
// 지도에 위치 추가
val options = LabelOptions.from(LatLng.from(location.latitude, location.longitude)).setStyles(styles)
val layer = kakaoMap?.labelManager?.layer

if (layer != null) {
val label = layer.addLabel(options)
label.show()
if (location.latitude == null || location.longitude == null) {
Log.e("pinResponse", "위도/경도 값이 null입니다.")
}
}
} else {
Log.d("API", "위치 정보가 없습니다.")
Log.d("pinResponse", "위치 정보가 없습니다.")
}
} else {
Log.d("API", "응답이 성공적이지 않음")
Log.d("pinResponse", "응답이 성공적이지 않음")
}
} else {
Log.e("API", "응답 실패: ${response.code()}")
Log.e("pinResponse", "응답 실패: ${response.code()}")
}
}

override fun onFailure(call: Call<PinResponse>, t: Throwable) {
Log.e("API", "API 호출 실패", t)
Log.e("pinResponse", "API 호출 실패", t)
}
})
}

private fun initializeMap() {
//지도 설정 함수
private fun initializeMap(userId: Int) {
binding.mapView.start(object : MapLifeCycleCallback() {
override fun onMapDestroy() {
Log.d("onMapDestroy", "onMapDestroy")
// 지도 API 종료 시 호출
}

Expand All @@ -141,6 +138,7 @@ class MapActivity: AppCompatActivity() {
override fun onMapReady(map: KakaoMap) {
kakaoMap = map
setupMap() // 지도 설정
fetchLocations(userId)
}

override fun getPosition(): LatLng {
Expand All @@ -154,9 +152,12 @@ class MapActivity: AppCompatActivity() {
override fun isVisible(): Boolean {
return true // 지도 표시 여부
}

})
}



// 지도 시작 설정
private fun setupMap() {
val position = LatLng.from(37.44983420181418,127.12727640486801)
Expand All @@ -173,6 +174,7 @@ class MapActivity: AppCompatActivity() {
private fun addLabel(position: LatLng) {
// 기존 임시 라벨 제거
unsavedLabel?.remove()

val styles = kakaoMap?.labelManager?.addLabelStyles(
LabelStyles.from(
LabelStyle.from(R.drawable.ic_pin).setAnchorPoint(0.5f, 1.0f)
Expand All @@ -183,8 +185,11 @@ class MapActivity: AppCompatActivity() {
val layer = kakaoMap?.labelManager?.layer

if (layer != null) {
// 새로운 라벨 추가
val label = layer.addLabel(options)
label.show()

// 라벨을 임시 라벨로 설정
unsavedLabel = label // 새로 생성된 라벨을 임시 라벨로 설정

latitude = label.position.latitude.toString()
Expand All @@ -194,10 +199,13 @@ class MapActivity: AppCompatActivity() {
binding.btnRegister.isEnabled = true
binding.btnRegister.setBackgroundColor(ContextCompat.getColor(this, R.color.P500))

// 핀 클릭 리스너
kakaoMap?.setOnLabelClickListener { map, labelLayer, clickedLabel ->
initLabelClickListener(map, labelLayer, clickedLabel)
true
}

// 위치 정보 저장
val spfLoc = getSharedPreferences("map_location", MODE_PRIVATE)
val editor = spfLoc.edit()
editor.putString("latitude", label.position.latitude.toString())
Expand All @@ -213,6 +221,11 @@ class MapActivity: AppCompatActivity() {
val spf = getSharedPreferences("UserPrefs", MODE_PRIVATE)
val userId = spf.getInt("USER_ID", 0)

val locationIdSpf = getSharedPreferences("MapPreferences", MODE_PRIVATE)
val locationId = locationIdSpf.getInt("locationId", 0)

fetchMemoryContent(userId, locationId)

val popupActivity = PopupActivity(this)
popupActivity.showPopup(binding.root, userId) {
}
Expand Down Expand Up @@ -264,10 +277,81 @@ class MapActivity: AppCompatActivity() {
override fun onResume() {
super.onResume()
binding.mapView.resume() // MapView resume
Log.d("MapLog", "Map_resume")

// 사용자 ID 가져오기
val spf = getSharedPreferences("UserPrefs", MODE_PRIVATE)
val userId = spf.getInt("USER_ID", 0)
Log.d("MapActivity", "USER_ID in MainActivity: $userId")

// 이전에 저장한 좌표
val spfLoc = getSharedPreferences("map_location", MODE_PRIVATE)
val latitude = spfLoc.getString("latitude", null)
val longitude = spfLoc.getString("longitude", null)

// val memoryspf = getSharedPreferences("MemoryPrefs", MODE_PRIVATE)
// val title = memoryspf.getString("title", "")
// val visitDate = memoryspf.getString("visit_date", "")
// val friends = memoryspf.getString("friends", "")
// val content = memoryspf.getString("content", "")
// val summary = memoryspf.getString("summary", "")
//
// // 추억 데이터 로그 확인
// Log.d("MapActivity", "추억 데이터: title=$title, visit_date=$visitDate, friends=$friends, content=$content, summary=$summary")

// 위치가 저장되어 있으면 그곳에 핀 고정
if (latitude != null && longitude != null) {
val position = LatLng.from(latitude.toDouble(), longitude.toDouble())
// 기존 핀 고정 (회색 핀으로)
val styles = kakaoMap?.labelManager?.addLabelStyles(
LabelStyles.from(
LabelStyle.from(R.drawable.ic_pin_gray).setAnchorPoint(0.5f, 1.0f)
)
)

val options = LabelOptions.from(position).setStyles(styles)
val layer = kakaoMap?.labelManager?.layer

if (layer != null) {
val fixedLabel = layer.addLabel(options)
fixedLabel.show()
}
}

// 위치 목록 가져오기
fetchLocations(userId)
}


override fun onPause() {
super.onPause()
binding.mapView.pause() // MapView pause
Log.d("MapLog", "Map_pause")
}

private fun fetchMemoryContent(userId: Int, locationId: Int) {
// Create a Retrofit instance and API service
val apiService = RetrofitClient.instance.create(MemoryContentInterface::class.java)

// Make the API call
lifecycleScope.launch {
try {
val response = apiService.getMemoryContent(userId, locationId)
if (response.isSuccessful) {
val memoryContent = response.body()?.result
if (memoryContent != null) {
// Memory data fetched successfully, handle it here
Log.d("MemoryData", "Memory Title: ${memoryContent.title}")
// You can display it in a Toast or another UI element
Toast.makeText(this@MapActivity, "Memory loaded: ${memoryContent.title}", Toast.LENGTH_SHORT).show()
}
} else {
Log.e("MemoryData", "Error fetching memory data: ${response.code()}")
}
} catch (e: Exception) {
Log.e("MemoryData", "API call failed", e)
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ class PopupActivity (private val context: Context) {
val apiService = RetrofitClient.instance.create(PopupInterface::class.java)
val accessToken = AccessTokenManager.getAccessToken() ?: ""

apiService.getPopupData(accessToken,"application/json",userId,latitude,longitude).enqueue(object : Callback<PopupResponse> {
apiService.getPopupData(userId,latitude,longitude).enqueue(object : Callback<PopupResponse> {
override fun onResponse(call: Call<PopupResponse>, response: Response<PopupResponse>) {
if (response.isSuccessful && response.body()?.success == true) {
response.body()?.memory?.let { memory ->
response.body()?.result?.let { result ->
// 성공 로그 추가
Log.d("PopupData", "데이터를 성공적으로 불러왔습니다: ${memory.title}, 방문일: ${memory.visit_date}, 친구: ${memory.friends}")
Log.d("PopupData", "데이터를 성공적으로 불러왔습니다: ${result.title}, 방문일: ${result.visit_date}, 친구: ${result.friends}")

binding.tvPopupTitle.text = memory.title
binding.tvPopupDate.text = "방문일: ${memory.visit_date} / 친구: ${memory.friends}"
binding.tvPopupDescription.text = memory.gpt_summary
binding.tvPopupTitle.text = result.title
binding.tvPopupDate.text = "방문일: ${result.visit_date} / 친구: ${result.friends}"
binding.tvPopupDescription.text = result.gpt_summary
Glide.with(binding.imgPopup.context)
.load(RetrofitClient.BASE_URL + memory.image_url)
.load(RetrofitClient.BASE_URL + result.image_url)
.into(binding.imgPopup)
}
} else {
Expand Down
Loading