I need to get value of latitude and longitude from google map api by the address. I used the url "http://maps.googleapis.com/maps/api/geocode/json?address=".$doctorAddress."&sensor=true" like to get value from google map api. I am trying to get list of values using for loop. But my problem is it return only the null value. I can't able to get the value from API. Is there anyway to get it. Can anyone help me to do this.
1 Answer
Below is the code to get the lat and long from google map api using curl in php
$address = "India+Panchkula";
$url = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=India";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response);
echo $lat = $response_a->results[0]->geometry->location->lat;
echo "<br />";
echo $long = $response_a->results[0]->geometry->location->lng;
You can achieve the same with the below method also
$address = str_replace(" ", "+", $address);
$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region");
$json = json_decode($json);
$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
7 Comments
sankar
Yes i know that. I did the same code but can't able to get value. It should return only the null value.
Veerendra
@sankar You should have posted the code in question can you post it now, as the above code is working for me
Veerendra
@sankar After your comment i have tested it once more and it give me output 30.6942091 76.860565. Just to confirm is curl enabled?
sankar
okay fine. i am getting the following error Fatal error: Maximum execution time of 300 seconds exceeded
sankar
I used file_get_contents... Now am getting Fatal error:Maximum execution time of 300 seconds exceeded in php
|