Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
La respuesta de la API de dirección de Google Maps no se asigna con la carretera

Estoy tratando de llamar a la API de indicaciones y mostrar la ruta en el mapa, pero la ruta no coincide perfectamente con la carretera.

Captura de pantalla de mapa con polilínea

Así es como obtengo las indicaciones:

 suspend fun getGoogleDirections( origin: LatLng, destination: LatLng, mapsApiKey: String ): Flow<DataState<List<LatLng>>> = flow { try { val apiResponse = apiInterface.getDirectionFromGoogle( "${origin.latitude},${origin.longitude}", "${destination.latitude},${destination.longitude}", mapsApiKey ) if (apiResponse.isSuccessful) { val responseBody = apiResponse.body()!! logD(TAG, "response = ${responseBody}") val responseObject = JSONObject(responseBody) val routes = responseObject.getJSONArray("routes") val firstRoute = routes.getJSONObject(0) val legs = firstRoute.getJSONArray("legs") val firstLeg = legs.getJSONObject(0) val steps = firstLeg.getJSONArray("steps") val ovPolyline = firstRoute.getJSONObject("overview_polyline") val polyline = ovPolyline.getString("points") emit(DataState.Success(PolyUtil.decode(polyline))) } else { val error = apiResponse.errorBody()?.string()!! logE(TAG,"", error) emit(DataState.Error("error in fetching directions")) } } catch (e: Exception) { logE(TAG,"",e.message ?: "Exception") emit(DataState.Error("An unexpected error occurred")) } }

Mi interfaz API:

 @GET("directions/json") suspend fun getDirectionFromGoogle( @Query("origin") origin: String, @Query("destination") destination: String, @Query("key") apiKey: String, @Query("mode") mode: String = "driving" ): Response<String>

Agregar polilínea en el mapa

 val directionPolyline = mMap.addPolyline( PolylineOptions() .color(ContextCompat.getColor(requireContext(), R.color.colorPrimaryDark)) //.width(3f) .geodesic(false) .endCap(RoundCap()) .startCap(RoundCap()) .addAll(listOfPoints) )

Enfrento este problema especialmente si la distancia es grande. Para distancias cercanas los resultados son aún mejores.

Nota: anteriormente he usado MapBox y hay una clave "descripción general = completa" que permite esto, pero no puedo encontrar nada en Google Maps.

about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!