0

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.

sankar
  • 83
  • 4
  • 16

1 Answers1

1

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&region=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&region=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
Veerendra
  • 2,562
  • 2
  • 22
  • 39
  • Yes i know that. I did the same code but can't able to get value. It should return only the null value. – sankar Feb 04 '15 at 11:12
  • @sankar You should have posted the code in question can you post it now, as the above code is working for me – Veerendra Feb 04 '15 at 11:13
  • @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? – Veerendra Feb 04 '15 at 11:15
  • okay fine. i am getting the following error Fatal error: Maximum execution time of 300 seconds exceeded – sankar Feb 04 '15 at 11:23
  • I used file_get_contents... Now am getting Fatal error:Maximum execution time of 300 seconds exceeded in php – sankar Feb 04 '15 at 11:27
  • @sankar First thing where is your curl code in question i asked you to please update it and then is curl enabled or not. And if you are getting execution time error you need to increase execution time on your server – Veerendra Feb 04 '15 at 11:34
  • { "error_message" : "You have exceeded your daily request quota for this API.", "results" : [], "status" : "OVER_QUERY_LIMIT" } am getting the above message. is there any other way to resolve this. – sankar Feb 04 '15 at 11:51