generate random locations nearby my location

五迷三道 提交于 2019-12-24 15:38:18

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/10530031/generate-random-locations-nearby-my-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!