0

I need to get latitude and longitude from an address, using google maps, and manipulate it with php.

I'm using a code like this one https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple, that has this part:

function codeAddress() {
  var address = document.getElementById('address').value;
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var latitude = results[0].geometry.location.lat();
      var longitude = results[0].geometry.location.lng();
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

where I already added the variables latitude and longitude (following the instructions found in this question. Now, I need to use the variables I'd created inside the java script in a php script.

I'm newbie to javascript, so, I'm sorry if this is a simple question.

Community
  • 1
  • 1
Leandro Guedes
  • 111
  • 1
  • 2
  • 14
  • If you're simply wanting to print out the coordinates into your page, (as your example of `` indicates) you can do that using javascript, no need to use PHP. Can you clarify what you need to pass the coords into PHP for? – duncan Sep 11 '14 at 08:28
  • I don't simply want to print out the coordinates, that was an example to keep the question simple. I need to send the coordinates to setcookie() and to an external program that will process it and give an output. It is easy and I have no problems to do all that with PHP thatś why I would like to pass the variables. – Leandro Guedes Sep 11 '14 at 13:32
  • @duncan I edited the question, hope it's better now. – Leandro Guedes Sep 11 '14 at 15:52

2 Answers2

2

A simple way is to use a jquery based ajax. Don't forget to download/include the library.

$.ajax({
   type:"POST",
   url:"yourscript.php",
   data: {type:getlatlong,latitude:latitude,longitude:longitude},
   success: function(){
      alert('sent');
   }
});

On your PHP, you access the information as:

if ($_SERVER["REQUEST_METHOD"]=="POST") {
   if ($_POST["type"]=="getlatlong") {
      $latitude = $_POST["latitude"];
      $longitude = $_POST["longitude"];
      ...
   }
  • Thankyou Vizvi, I'm trying to make this work. My websit is a Wordpress one, and always had problems with post method, but I guess your solution will work perfect. Thankyou! – Leandro Guedes Sep 11 '14 at 12:10
  • By tye way, is there a way to use the current url in the $.ajax()? – Leandro Guedes Sep 11 '14 at 12:18
  • The current url, meaning the PHP file along the path? You could use `dirname(__FILE__)` but this will _only work_ for plain PHP scripts and not inside javascript files. You could use as well `get_bloginfo('wpurl')` WP function to get wordpress base directory and just modify it to include your PHP script. I'm not really fond of wordpress, sorry :) –  Sep 11 '14 at 18:56
0

I think you need javascript variable in php script.Php is serverside language for accessing data you can use either get or post method.You can simply get this way, first assign the javascript variables to hidden text field and access in php using get or post method.You can assign javascript variable like this

<form action="yourphp.php" method="post"> <input type="hidden" value="latitude" name="latitude"> <input type="hidden" value="longitude " name="longitude"> </form>

in your php file access data as:

$latitude=$_POST['latitude'] $longitude=$_POST['longitude']

shamon shamsudeen
  • 5,466
  • 17
  • 64
  • 129