user1892989
user1892989

Reputation:

Geolocation with PHP

Is there a way to get lat/long into PHP in a single file solution? Normally using JS I would use this in a pure JS sulution, but now I need the values in PHP variables:

<script>
$(document).ready(function(){
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showLocation);
    } else {
        $('#location').html('Geolocation is not supported by this browser.');
    }
});
function showLocation(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    return latitude;
}
</script>

I need to get the lat/long vars into PHP without using an AJAX post into a url: xxx.php type of sulution. Single file only. Any ideas on a clean way to execute this anyone?

BTW- If I write them into cookies this only works if I reload the doc. :-(

Thanks!

Upvotes: 2

Views: 25569

Answers (2)

Andrew Leonard
Andrew Leonard

Reputation: 11

I did this In the JS (g1.php):

function showPosition(position) { var latlon = position.coords.latitude + "," + position.coords.longitude; var prog = "g2.php?latlon=" + latlon; location.href = prog;}

and then did this in g2.php

$field = explode(',', $_GET['latlon']);

I don't know if it is good programming but it works

Upvotes: 1

bhaghyasandar
bhaghyasandar

Reputation: 516

You can use geoplugin api Link

<?php
  echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp? 
  ip='.$_SERVER['REMOTE_ADDR'])));
?>

Upvotes: 1

Related Questions