0

I wish to randomly position markers on a Google map so hoped to randomly generate the longitude and latitude of the markers using PHP and load them in via AJAX. The problem I have is not only are coordinates decimals but some are negative. For example I need the longitude to be between -2.07137437719725 and -1.92779606909178 and the latitude to be between 50.71603387939352 and 50.793906977546456. The only random functions I can find only work for positive integers so aren't feasible. I did try multiplying the numbers by a billion to remove the decimals and then would later divide the resulting random number by the same amount to return to using decimals but unfortunately PHP can't handle such large numbers.

I hope you can help.

Thanks

Paul

AdrenalineJunky
  • 891
  • 11
  • 15
  • See this answer: http://stackoverflow.com/a/1504655/782609 – kamituel May 22 '13 at 12:40
  • Sounds like you want to do something similar to a `Geo guessing` thing? – Funk Forty Niner May 22 '13 at 12:43
  • Fred - Nope just need random markers on the map. – AdrenalineJunky May 22 '13 at 12:47
  • Kamituel - Thanks but that is limited to 14 digits and doesn't do decimals or negatives. – AdrenalineJunky May 22 '13 at 12:47
  • @AdrenalineJunky SO tip: Add the `@` symbol in front of the person's name you are addressing, that way the person in question will be notified ;-) – Funk Forty Niner May 22 '13 at 12:49
  • @AdrenalineJunky Personally, I don't know how to do it. However with a bit of searching on SO or Google and a bit of time, am sure I'd come up with something, or a mix of different functions to achieve what you wish to perform. You'll need a few functions to do this, one that will randomly select from a positive or negative function, then the number itself with a pre-defined set of accepted numbers and decimals. – Funk Forty Niner May 22 '13 at 12:52
  • @Fred I should of realised that @ is used. Thanks. I tried various approaches from Google & SO but non seemed to work properly, the main issue to overcome is variable type conversion and limitations such as integers are limited so can't do big numbers. – AdrenalineJunky May 22 '13 at 13:27
  • @AdrenalineJunky `dougBTV's` answer below seems like what you need. Food for thought: You might want to try and omit regions such as Africa and others that have not been scanned by Google; those coordinates could very well come up.... eventually. ;-) – Funk Forty Niner May 22 '13 at 13:40

2 Answers2

1

Here's a function that I brewed up to do just this. It takes a number of decimal points as an argument and uses a check of a random number even/odd to make the positivity/negativity randomized.

    $latlon = randomLatLon(4);
    print_r($latlon);

    function randomLatLon($granularity) {

            // $granularity = Number of decimal spaces.
            $power = pow(10,$granularity); // Extended 10 to the power of $granularity.

            // Generate the lat & lon (as absolutes) according to desired granularity.
            $lat = rand(0,90 * $power) / $power;
            $lon = rand(0,180 * $power) / $power;

            // Check if a random number is even to randomly make the lat/lon negative.
            if (rand(0,100) % 2 == 0) {
                    $lat = $lat * -1;
            }

            // Same for lon...
            if (rand(0,100) % 2 == 0) {
                    $lon = $lon * -1;
            }

            return array(
                    "lat" => $lat,
                    "lon" => $lon,
            );


    }
dougBTV
  • 1,883
  • 1
  • 15
  • 18
0

You could use something like this:

float Latitudes = (float)((rand(0, 180000000) - 90000000)) / 1000000);
float Latitudes = (float)((rand(0, 360000000) - 180000000)) / 1000000);

Personally I think the accuracy up to 0.001 would be good enough for the locations. If you do not believe me, try it on google map (-32.833, 151.893) & (-32.832, 151.895), see how much further apart they are.

TelKitty
  • 3,146
  • 3
  • 18
  • 21
  • rand() is crap, use mt_rand() instead. – GordonM May 22 '13 at 13:11
  • @yiz Thanks but that's quite low level or accuracy and it doesn't allow specification of a maps range which is important in my current project as the map is split into regions so it must be within one of the regions – AdrenalineJunky May 22 '13 at 13:31
  • @GordonM Thanks but even mt_rand has issues as it is still uses only integers so no positive, negative or decimals. – AdrenalineJunky May 22 '13 at 13:32