0

I want to convert the value from shared preferences to mutable list of LatLng pairs to draw a polylines. I dont know how to convert it. Please help.Here I get the value in shared preferences polyl as [lat/lng:(10.184133,76.4005395),lat/lng(10.1840907,76.4004505)]etc. How do I convert it to mutable list of LatLng pairs so that I could draw a polyline.


            val sharedPr= activity?.getPreferences(Context.MODE_PRIVATE)
            val polyl= sharedPr?.getString("polylinep",null)

            Toast.makeText(requireContext() ,"$polyl", Toast.LENGTH_SHORT).show()
            try {
            val polyline10= polyl as MutableList<LatLng>

            Toast.makeText(requireContext() ,"ub$polyline10", Toast.LENGTH_SHORT).show()


            map.addPolyline(
                    PolylineOptions().apply {
                        width(10f)
                        color(Color.BLUE)
                        jointType(JointType.ROUND)
                        startCap(ButtCap())
                        endCap(ButtCap())
                        addAll(polyline10)
                    }

                )


            } catch (e:java.lang.ClassCastException){

            }

Vadik Sirekanyan
  • 3,332
  • 1
  • 22
  • 29
user16493824
  • 57
  • 1
  • 12

1 Answers1

1

Preamble

I do not know how you create the String "[lat/lng:(10.184133,76.4005395),lat/lng(10.1840907,76.4004505)]", but I think it would be a good idea to convert your coordinates to a JSON String before saving it to the preferences. If you are not able to change the way how these coordinates get saved in the first palce then my answer won't help you.

Solution

Saving to the shared preferences

Considering that you have the coordinates in some sort of List and want to save them to the Preferences you can for example use a library like GSON to convert that List into JOSN String that represents this list. To see how that is done give this tutorial a look: https://www.tutorialspoint.com/how-to-save-arraylist-to-sharedpreferences-on-android

Reading from the shared preferences

Now that the list of coordinates got saved as JSON String you can read it from the shared preferences, convert it back to a JSON object and read back your list from it. This is for example shown in the first answer of this thread: Is it ok to save a JSON array in SharedPreferences?

Hope I could help you :)

Erik Schmidt
  • 85
  • 1
  • 8