Viktor Vajzer
Viktor Vajzer

Reputation: 9

Generating random position near me with markers

I want to integrate this code into mine, but don't know how to get it to work.

I got problem with the return statement and then how to create a mark with randomly generated position?
Can you please tell me how to get the getRandomLocation() method working in creating markers?

public Location getRandomLocation() {
        Location location = new Location("");
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    int radius = 10;


                   double x0 = latLng.latitude;
            double y0 = latLng.longitude;

            Random random = new Random();

            // Convert radius from meters to degrees
            double radiusInDegrees = radius / 111000f;

            double u = random.nextDouble();
            double v = random.nextDouble();
            double w = radiusInDegrees * Math.sqrt(u);
            double t = 2 * Math.PI * v;
            double x = w * Math.cos(t);
            double y = w * Math.sin(t);

            // Adjust the x-coordinate for the shrinking of the east-west distances
            double new_x = x / Math.cos(y0);

            double foundLatitude = new_x + x0;
            double foundLongitude = y + y0;
            LatLng randomLatLng = new LatLng(foundLatitude, foundLongitude);

            Location loc = new Location("");
            loc.setLatitude(randomLatLng.latitude);
            loc.setLongitude(randomLatLng.longitude);

//dont know what to return
        return ;
    }

    public final void addMarker(GoogleMap mMap) {
//dont know how to get working the getRandomLocation())
        mMap.addMarker( new MarkerOptions()
              //  .position(new LatLng(48.349723, 18.052405))
                .position(getRandomLocation())
                .title("krokodíl")
                .icon(BitmapDescriptorFactory.fromBitmap(resizeMapIcons("krokodil", 100, 100))));

        mMap.addMarker(new MarkerOptions()
               // .position(new LatLng(48.310025, 18.038878))
                .position(getRandomLocation())
                .icon(BitmapDescriptorFactory.fromBitmap(resizeMapIcons("fretka", 100, 100)))
                .title("fretk"));
        mMap.addMarker (new MarkerOptions()
                .title("hroch")
                .icon(BitmapDescriptorFactory.fromBitmap(resizeMapIcons("hroch", 100, 100)))
                //  .title()
                //.snippet(mObj.getNumber(Configs.MONSTERS_MONSTER_POINTS) + " points")
               // .position(new LatLng(48.318569, 18.055767)));
                 .position(getRandomLocation()));
    }

Upvotes: 0

Views: 437

Answers (1)

Mithilesh Gupta
Mithilesh Gupta

Reputation: 2930

According to the logic of your code, you need to return randomLatLng.

Change the line:

public Location getRandomLocation() {

to:

public LatLng getRandomLocation() {

and make the return statement something like:

return randomLatLng;

The method MarkerOptions.position() needs an object of type LatLng, that is what your IDE showing you as an error.

Upvotes: 1

Related Questions