I am using the function getAddress() to get the address of the coordinates provided. How do I make sure that the returned value of the function could be echoed in php?
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places&key=yourAPIkey"></script>
<script type="text/javascript">
function GetAddress(lat,lng) {
var lat = parseFloat(lat);
var lng = parseFloat(lng);
var latlng = new google.maps.LatLng(lat, lng);
var geocoder = geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
var addr = results[0].formatted_address;
return addr;
}
}
});
}
</script>
<?php
$lng = 103.72;
$lat = 1.34628;
echo "<script type='text/javascript'>",
"GetAddress(",$lat,",",$lng,");",
"</script>";
?>
</html>
-
3I think you're fundamentally misunderstanding the relationship between PHP and JavascriptPatrick Q– Patrick Q2017年10月31日 17:27:01 +00:00Commented Oct 31, 2017 at 17:27
-
@PatrickQ What am in misunderstanding? Issit the link?Tyra– Tyra2017年10月31日 17:29:11 +00:00Commented Oct 31, 2017 at 17:29
-
2PHP cannot "call" Javascript. Therefore, your Javascript function cannot "return" a value to PHP the way you are asking for. The PHP has already finished execution by the time any of the HTML/Javascript is processed.Patrick Q– Patrick Q2017年10月31日 17:31:47 +00:00Commented Oct 31, 2017 at 17:31
-
@PatrickQ So there's no way I could get the value from php?Tyra– Tyra2017年10月31日 17:36:01 +00:00Commented Oct 31, 2017 at 17:36
-
1You need to do a better job explaining what it is you're actually trying to accomplish. Why does PHP need the result of this function? Unless you have more that you're not showing, you don't have any PHP code that would make use of this value. Either you actually want to use this value in Javascript and just don't know it, or you want to generate the value client-side and then use ajax to send the value to the server.Patrick Q– Patrick Q2017年10月31日 17:41:11 +00:00Commented Oct 31, 2017 at 17:41
2 Answers 2
The echo that you've will echo the JS script to your page and not the result so I suggest to you to add a result container then put the data inside it like :
<?php
$lng = 103.72;
$lat = 1.34628;
echo "<p id='result'> </p>";
echo "<script type='text/javascript'>".
"document.getElementById('result').innerText = GetAddress(".$lat.",".$lng.");".
"</script>";
?>
Hope this helps.
5 Comments
echo sandbox.onlinephpfunctions.com/code/… result as a variable, could I use it as $result? @ZakariaAcharkiasync. On JavaScript I am doing async call which does not wait to return the return value in async call into result variable that causes to return undefined output @ZakariaAcharkiIf all that you are trying to do is write the output from the function call (as per your recent comment reply), you can just do this:
$lng = 103.72;
$lat = 1.34628;
echo "<script type='text/javascript'>document.write(GetAddress(",$lat,",",$lng,"));</script>";
So, where ever the GetAddress is called, you'll see the output.
Alternatively, if you need to target a specific area where you want the result to appear, see Zakaria Acharki updated answer.
I personally would avoid mixing php and javascript and just do everything in php.
For example;
<?php
// Config
define('API_KEY', 'yourAPIkey');
// Helper function to geodecode lat/lng
function getFormattedAddress($lat, $lng) {
$result = json_decode(file_get_contents("https://maps.google.com/maps/api/geocode/json?key=". API_KEY ."&latlng=$lat,$lng"));
if ($result->status == 'OK') {
return $result->results[0]->formatted_address;
}
return 'Invalid lat/lng coordinates';
}
// Usage
$lat = 40.714224;
$lng = -73.961452;
echo getFormattedAddress($lat, $lng);
?>
Outputs: 277 Bedford Ave, Brooklyn, NY 11211, USA
So, your php script has the lat, lng coords, so you can just call the php function and just render the result in one go.
P.S. you should avoid posting your API keys in your question; people might misuse it ;)