1
<h1>hello this is phtml file</h1>
<?php $address=$_GET['location'];//manhattan


$address = str_replace(" ", "+", $address);
 $url="https://maps.google.com/maps/api/geocode/json?address=$address&key=".$block->getApiKey();
echo $url;

from above code i am getting this url

if hit that url in browsers other tab it will display json data i.e https://maps.google.com/maps/api/geocode/json?address=Manhattan&key=AIzaSyBr3mn7y42_iHMTcE_5zEJk_uSGCFXIFGk

i have to fetch that data in php variables mainly longitude and latitude for setting in my map any suggestion

any help will be appreciated thanks

5
  • you want to get map URL or want to get latitude and longitude from variable? Oct 25, 2017 at 10:35
  • if you hit the url you will find lat and long in geometry of that api file .I only need those variables value after that i will manage Oct 25, 2017 at 10:38
  • are you get your data from JSON or other api you have called ? Oct 25, 2017 at 10:43
  • google map API i haven't created any json file on localhost that file is generated by google maps Oct 25, 2017 at 10:46
  • you can use CURL to get this json in PHP Oct 25, 2017 at 10:58

1 Answer 1

3

If you have URL and just want to get Data use CURL as below.

$url = "https://maps.google.com/maps/api/geocode/json?address=Manhattan&key=AIzaSyBr3mn7y42_iHMTcE_5zEJk_uSGCFXIFGk";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$response = curl_exec($ch); 
curl_close($ch);
echo "<pre>";
print_r($response);
$jsonToArray = json_decode($response,1);
print_r($jsonToArray);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.