Search code examples
javaandroid-studioonclicklistenerpolylinegoogle-maps-android-api-3

How to change Random location onClickListener android google maps api


hey i'm new with this and I'm trying to implement a button to generate a location polyline. i got the code from here https://github.com/Moneemsaadaoui/Gradientpoly. and the code to generate a polyline like this.

 generate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            double randomValuex = 36.046851 + ((36.203712 - 36.046851) * r.nextDouble());
            double randomValuex2 = 36.046851 + ((36.203712 - 36.046851) * r.nextDouble());
            double randomValuey = 8.269289 + ((10.486982 - 8.269289) * r.nextDouble());
            double randomValuey2 = 8.269289 + ((10.486982 - 8.269289) * r.nextDouble());
            from = new LatLng(randomValuex, randomValuey);
            to = new LatLng(randomValuex2, randomValuey2);

            //Setting up our awesome gradient 🌈🌈

            gradientPoly.setApiKey("API_KEY")
                    .setStartPoint(from).setEndPoint(to)
                    .setStartColor(Color.parseColor("#1eb5ab"))
                    .setWidth(11).setEndColor(Color.parseColor("#ff0098"))
                    .DrawPolyline();
        }
    });

my question is how to change Random Generate location To some location that I have specified (fixed location)? sorry for my bad english.


Solution

  • Just replace the random variables to contain your own values,
    change these:

    double randomValuex = 36.046851 + ((36.203712 - 36.046851) * r.nextDouble());
    double randomValuex2 = 36.046851 + ((36.203712 - 36.046851) * r.nextDouble());
    

    To this:

       const val MY_X_LOCATION = 36.0 (or whatever you want)
       const val MY_Y_LOCATION = 36.1 (or whatever you want)
       const val MY_X2_LOCATION = 36.2 (or whatever you want)
       const val MY_Y2_LOCATION = 36.3 (or whatever you want)
    
       double myValueX = MY_X_LOCATION
       double myValueX2 = My_X2_LOCATION
       double myValueY = MY_Y_LOCATION
       double myValueY2 = My_Y2_LOCATION
    

    And then pass them here:

    from = new LatLng(myValueX, myValueY);
    to = new LatLng(myValueX2, myValueY2);
    

    (You can store the constants in your class or some other place.
    You can also pass them as parameters of a method, and put
    this onClickListener inside the method)