get random Location - Android Map

Android examples for Map:Location

Description

get random Location

Demo Code


//package com.java2s;
import android.location.Location;
import android.location.LocationManager;
import java.util.Random;

public class Main {
    public static Location randomLocation() {
        Location location = new Location(LocationManager.GPS_PROVIDER);
        Random rand = new Random();
        location.setLatitude(randomLat(rand));
        location.setLongitude(randomLong(rand));
        return location;
    }//from   ww w  .ja v  a  2  s .  co  m

    public static Double randomLat(Random rand) {
        return doubleRandomInclusive(-90.0, 90.0, rand);
    }

    public static Double randomLong(Random rand) {
        return doubleRandomInclusive(-180.0, 180.0, rand);
    }

    public static Double doubleRandomInclusive(double min, double max,
            Random rand) {
        double num = rand.nextDouble();
        if (num < 0.5) {
            return ((1 - rand.nextDouble()) * (max - min) + min);
        }
        return (rand.nextDouble() * (max - min) + min);
    }
}

Related Tutorials