generate random locations nearby my location

I would like to generate random latitude/longitude pairs inside the circle surrounding my location.

Currently i have implemented a location listener and everytime i get a new location i would like to draw random locations nearby me.

I am able to compute distance between 2 already existing locations and know if its inside my range:

public List<Location> filter(Location current, List<Location> locations, int radius) {
    List<Location> results = new ArrayList<Location>();
    for (Location loc : locations) {
        if (current.distanceTo(loc) <= radius) {
            results.add(loc);
        }
    }
    return results;
}

But really, i cant find how to generate locations inside my range.

Any help here? Thank you very much.

Answers


How about randomly generating the coordinates yourself? Using the current location you could set limitations to these numbers. Using the current coordinates and the radius you get the span for the random numbers. Span for random new X = Old X +- radius

I'd suggest generating a random number between -1 and 1, then multiply it with the radius. This means that the max value for the generated coordinate is the radius (and min is -radius). Adding this value to the current location coordinate gives you a value between Old X +- radius. Summary:

(Random number between -1 and 1)*radius + (old x) = new x within radius of old x.

Example code for the random number (OBS! from 0 to 1):

import java.util.Random;

Random r = new Random();
double zeroToOne = r.nextInt(1001) / 1000

Here's the java documentation on random: Random


it seems like a good thing. if you want to take a look : http://android-er.blogspot.com/2011/02/get-list-of-address-from-location-using.html

i think, normally it have been working but i couldn't try to setup it on device


Need Your Help

How to copy/clone a DataTable

c# datatable copy clone

I have a DataTable that will be modified by the user. I want to identify what's been change an keep record of that in the database.

SQL - WHERE on new column

sql sql-server tsql

I've a table with 3 columns: Id, Price, Total.