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.

3
  • If you're simply wanting to print out the coordinates into your page, (as your example of <?php echo()?> 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? Commented Sep 11, 2014 at 8: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. Commented Sep 11, 2014 at 13:32
  • @duncan I edited the question, hope it's better now. Commented Sep 11, 2014 at 15:52

2 Answers 2

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"];
      ...
   }
Sign up to request clarification or add additional context in comments.

3 Comments

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!
By tye way, is there a way to use the current url in the $.ajax()?
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 :)
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']

1 Comment

Thank you user2804362. How should I connect this with the variables latitude and longitde created inside the javascript?

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.