You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Maps添加标记并存入数据库?基于Retrofit实现类JustPark功能

Absolutely! You can absolutely build this JustPark-style functionality on top of your existing Retrofit-powered membership system. Let’s walk through how to pull this off step by step, keeping everything aligned with your current setup.

1. Core Architecture Overview

Since you’re already using Retrofit for your membership system, we’ll extend that setup to handle location data:

  • Your backend will expose new APIs to store/retrieve user-specific location data (tied to their membership ID)
  • Retrofit will act as the bridge between your app and these new backend endpoints
  • The Google Maps SDK will handle displaying markers and rendering navigation routes
  • We’ll tie everything to your existing auth flow (e.g., JWT tokens) to ensure only authenticated users access their location data
2. Step 1: Extend Backend & Retrofit Service

First, update your backend to support location operations, then create a Retrofit interface to interact with these new endpoints.

Retrofit Location Service Interface

interface LocationService {
    // Save a user's location (tied to their membership ID)
    @POST("api/locations")
    suspend fun saveUserLocation(@Body locationRequest: LocationRequest): Response<LocationResponse>

    // Fetch all saved locations for the logged-in user
    @GET("api/locations/my")
    suspend fun getMyLocations(): Response<List<LocationDto>>
}

Data Classes for Requests/Responses

data class LocationRequest(
    val latitude: Double,
    val longitude: Double,
    val name: String // e.g., "Downtown Parking Spot"
)

data class LocationDto(
    val id: String,
    val latitude: Double,
    val longitude: Double,
    val name: String,
    val userId: String // Ties location to your membership system
)

data class LocationResponse(
    val success: Boolean,
    val location: LocationDto?
)

Secure Endpoints with Your Membership Auth

Since you already handle auth with Retrofit, ensure these new location endpoints use the same security (e.g., JWT tokens in request headers). Add an interceptor to your OkHttpClient to auto-include the token:

val okHttpClient = OkHttpClient.Builder()
    .addInterceptor { chain ->
        val originalRequest = chain.request()
        val authRequest = originalRequest.newBuilder()
            .header("Authorization", "Bearer ${currentUser.authToken}") // Pull token from your membership session
            .build()
        chain.proceed(authRequest)
    }
    .build()
3. Step 2: Fetch Locations & Display on Google Maps

On app launch, use Retrofit to fetch the user’s saved locations, then render markers on Google Maps.

Load & Render Locations

// In your MapFragment/Activity
private fun loadUserLocations() {
    viewModelScope.launch {
        val response = locationService.getMyLocations()
        if (response.isSuccessful) {
            response.body()?.forEach { location ->
                val markerPosition = LatLng(location.latitude, location.longitude)
                // Add marker to the map
                map.addMarker(
                    MarkerOptions()
                        .position(markerPosition)
                        .title(location.name)
                )?.tag = location // Store location data in marker tag for later use
            }
        } else {
            // Handle errors (e.g., show toast for failed fetch)
            Toast.makeText(context, "Failed to load locations", Toast.LENGTH_SHORT).show()
        }
    }
}

Note: Make sure you’ve already integrated the Google Maps SDK, added your API key to AndroidManifest.xml, and set up a SupportMapFragment in your layout.

4. Step 3: Add Navigation on Marker Click

When a user taps a marker, you have two options for navigation—either launch the Google Maps app for turn-by-turn directions, or draw the route directly in your app.

Option A: Launch Google Maps App (Simplest)

This leverages the native Google Maps app for navigation:

map.setOnMarkerClickListener { marker ->
    val targetLocation = marker.tag as LocationDto
    val currentLocation = getCurrentUserLocation() // Use FusedLocationProviderClient to get current lat/lng

    currentLocation?.let {
        // Build intent to open Google Maps navigation
        val navigationUri = Uri.parse("google.navigation:q=${targetLocation.latitude},${targetLocation.longitude}")
        val mapIntent = Intent(Intent.ACTION_VIEW, navigationUri)
        mapIntent.setPackage("com.google.android.apps.maps")

        if (mapIntent.resolveActivity(requireContext().packageManager) != null) {
            startActivity(mapIntent)
        } else {
            Toast.makeText(context, "Google Maps not installed", Toast.LENGTH_SHORT).show()
        }
    }
    true
}

Option B: Draw Route Inside Your App

If you want to keep users in your app, use the Google Maps Directions API with Retrofit to fetch route data and render a polyline:

Retrofit Directions Interface

interface DirectionsService {
    @GET("maps/api/directions/json")
    suspend fun getRoute(
        @Query("origin") origin: String, // e.g., "37.7749,-122.4194"
        @Query("destination") destination: String,
        @Query("key") apiKey: String // Your Google Maps API key
    ): Response<DirectionsResponse>
}

Fetch & Render Route

map.setOnMarkerClickListener { marker ->
    val targetLocation = marker.tag as LocationDto
    val currentLocation = getCurrentUserLocation()

    currentLocation?.let {
        viewModelScope.launch {
            val origin = "${it.latitude},${it.longitude}"
            val destination = "${targetLocation.latitude},${targetLocation.longitude}"
            val response = directionsService.getRoute(origin, destination, GOOGLE_MAPS_API_KEY)

            if (response.isSuccessful) {
                response.body()?.routes?.firstOrNull()?.let { route ->
                    // Decode polyline points to draw the route
                    val polylinePoints = PolyUtil.decode(route.overviewPolyline.points)
                    map.addPolyline(
                        PolylineOptions()
                            .addAll(polylinePoints)
                            .width(12f)
                            .color(Color.parseColor("#2196F3"))
                            .geodesic(true)
                    )
                }
            }
        }
    }
    true
}

Don’t forget to enable the Directions API in your Google Cloud Console and add the required permissions (ACCESS_FINE_LOCATION) to your app.

5. Additional Tips for Smooth Integration
  • Local Caching: Use Room Database to cache location data offline—fetch from local storage first, then sync with the backend via Retrofit.
  • Error Handling: Add retry logic for failed Retrofit requests, and handle cases where the user loses network connectivity.
  • Performance: If users have many saved locations, implement pagination in your backend API to avoid loading too much data at once.
  • User Experience: Add loading states while fetching locations, and allow users to edit/delete saved locations via additional Retrofit endpoints.

内容的提问来源于stack exchange,提问作者realsavior

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:28:44