0

I have a javascript to take my current position and draw it in Google Maps, but I would like to have,also, the coordinates in variable usable in PHP . I have :

<script src="http://maps.google.com/maps/api/js?sensor=true"></script>

<script type="text/javascript">

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(mostra_mappa);
}else{
alert('La geo-localizzazione NON è possibile');
}

function mostra_mappa(position) {
var punto = new google.maps.LatLng(position.coords.lat, position.coords.long),

opzioni = {
zoom: 15,
center: punto,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
contenitore = document.getElementById("mia_mappa"),
mappa = new google.maps.Map(contenitore, opzioni),
marker = new google.maps.Marker({
position: punto,
map: mappa,
title: "Tu sei qui!"
});
}
</script>

Now, I would like to take the coordinates x and y (previous computed) of my position and put its into the request(as follow) :

$x =?????;
$y=??????;
$String = file_get_contents('http://api.yelp.com/business_review_searchterm=restaurant&lat=' . $x.'&long='. $y . '&radius=20&limit=500&ywsid=uHhsYI82_aEk8Q9NjSsIzg');

Could you help me?

1 Answer 1

1

Just send your coordinates to the PHP file. Javascript...

var data = { 
    'x': position.coords.lat,
    'y': position.coords.lon
};

$.post('script.php', { coordinates: JSON.stringify(data) }, function () {
    // deal with your returned Yelp review...
});

PHP...

if (isset($_POST['coordinates'])) {
    $geo = json_decode($_POST['coordinates']);

    $x = $geo.x;
    $y= $geo.y;

    // get Yelp review here
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm trying to implement you suggestion, but I failed. The thing is that is possible to pass the javascript variables to Php.
@user3043636, see the edited answer and try it that way, with JSON.stringify(). This should make sure that your Javascript objects are correctly parsed as a JSON string.
Maybe I don't understand where i must put your suggest javascript code and Php code. I'm new in this kind of languages, could you,please, explain better. thanks
Put the Javascript at the end of your existing code. The PHP code would go in the beginning of your script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.