store variables and retrieve them in another class

Viewed 25

I'm trying to make my own navigator and I placed the map in the main_Activity layout, then I created two classes, one to manage the singleTap and the longPress on the map (MapEventsReceiverImpl), one that contains the functions that create the paths on the map ( NavFun), and fragments. One of the fragments I created is used to manually enter the coordinates of the arrival and departure points and, using a button, create a path on the map between the two points. I have now added another button in the fragment, and set the longPress on the map to print the coordinates of the point pressed on the console.

I would like to be able to click the new button in the fragment and automatically set the coordinates of the points I touched via longPress in the editText fields but I don't know how to get the coordinates in the fragment after the longPress, can someone help me?

this is the MapEventsReceiverImpl fun

override fun longPressHelper(p0: GeoPoint?): Boolean {
        Log.d("longPressHelper", "${p0?.latitude} - ${p0?.longitude}")
        when (count) {
            1 -> {
                val puntoPartenza: GeoPoint = GeoPoint(p0?.latitude!!, p0?.longitude!!)
                println("First point coordinates saved: latitudine: ${puntoPartenza.latitude}, longitudine: ${puntoPartenza.longitude}")
                // navFun.setPoints(p0?.latitude!!, p0?.longitude!!)
                count += 1
                Toast.makeText(ctx, "Start Point", Toast.LENGTH_SHORT).show()
            }

            2 -> {
                val puntoArrivo: GeoPoint = GeoPoint(p0?.latitude!!, p0?.longitude!!)
                println("Second point coordinates saved: latitudine: ${puntoArrivo.latitude}, longitudine: ${puntoArrivo.longitude}")
                count -= 1
                Toast.makeText(ctx, "Arrival Point", Toast.LENGTH_SHORT).show()
            }

            else -> {
                Toast.makeText(ctx, "Error, counter variable out of range!!", Toast.LENGTH_SHORT).show()
            }
        }
        return true
    }

this is the function in NavFun which should receive the coordinates from the longPress and the function which I then thought of calling when I press the button

fun setPoints(p1Latit: Double, p1Laong: Double,){
        when (count) {
            1 -> {
                pto1 = GeoPoint(p1Latit, p1Laong)
                count += 1
            }
            2 -> {
                pto2 = GeoPoint(p1Latit, p1Laong)
                count -= 1
            }
            else -> {
                Toast.makeText(context, "Errore counter out of range" Toast.LENGTH_SHORT).show()
            }
        }
    }

    private fun routePathWithTap(pto1: Double, pto2: Double, pto3: Double, pto4: Double) {
        routePath(pto1, pto2, pto3, pto4)
    }

how can i memorize the coordinates of the pressed point and retrieve them when i need them in the fragment?

0 Answers
Related