Link to home
Start Free TrialLog in
Avatar of designersx
designersx

asked on

PHP script to geocode an address with Google API is not working

the code of PHP script to geocode an address with Google API is not working , please help me.
my key is :-

ABQIAAAAST4sux1VdyzTps5CXY6vkRSUi5n9UPmza1-UGBYjjmCGJKKnhxSCkKnQbKKobNPpxt5Cyo5Dt_X-yA


echo $result; does not show the latitude and longitude when i fill any particular address.
<?php
/*
To access the Maps API geocoder directly using server-side scripting, send a request to http://maps.google.com/maps/geo? with the following parameters in the URI:
 
    * q -- The address that you want to geocode.
    * key -- Your API key.
    * output -- The format in which the output should be generated. The options are xml, kml, csv, or json.
E.g.  http://maps.google.com/maps/geo?q=1600+Amphitheatre+Parkway,+Mountain+View,+CA&output=xml&key=abcdefg
myKey = 
*/
$result = "";
$strAddress = "";
if (isset($_POST["completeAddress"])) {
        
        $strAddress = $_POST['completeAddress'];
        $url = "http://maps.google.com/maps/geo?"; // base URL
        $url .= "q=" . urlencode($strAddress);
        $url .= "&output=json";
        $url .= "&key=YOURKEY";
        
        $content = null;
        $ch = curl_init();
        
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $content = curl_exec($ch);
        curl_close($ch);
        
        if ($content === null) die ("Could not get the page.");
        
        $result = htmlspecialchars($content);
}
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link href="/style.css" rel="stylesheet" type="text/css" />
<title>Geocode an address</title>
<script type="text/javascript">
function init() {
        document.form1.completeAddress.onfocus = function () {
                if (this.value != '') {
                        this.value = '';
                        document.getElementById('display').innerHTML = '';
                }
        }
}
</script>
</head>
 
<body onload="init();">
<h3>Geocode an address</h3>
 
<br /><br />
<form name="form1" action="" method="post">
Enter adddress to be encoded: <input type="text" name="completeAddress" value="<?php echo $strAddress; ?>" size="60" />
   <a href="#" onclick="document.form1.completeAddress.focus(); return false">edit address</a>
<br />
E.g. 123 Main St, Downtown, CA, 91111
<br /><br />
<input type="submit" value="Geocode" />
</form>
 
<br /><br /><br />
Result:<br />
<div id="display">
<?php echo $result; ?>
</div>
 
 
</body>
</html>
 
<?php
/*
returns a JSON object consisting of the following information:
 
    * Status
    *
          o request -- The request type. In this case, it is always geocode.
          o code -- A response code (similar to HTTP status codes) indicating whether the geocode request was successful or not. See the full list of status codes.
    * Placemark -- Multiple placemarks may be returned if the geocoder finds multiple matches.
    *
          o address -- A nicely formatted and properly capitalized version of the address.
          o AddressDetails -- The address formatted as xAL, or eXtensible Address Language, an international standard for address formatting.
          o
                + Accuracy -- An attribute indicating how accurately we were able to geocode the given address. See a list of possible values.
          o Point -- A point in 3D space.
          o
                + coordinates -- The longitude, latitude, and altitude of the address. In this case, the altitude is always set to 0.
*/
?>

Open in new window

Avatar of uncleramsay
uncleramsay

I don't know if you did this intentionally, but in your script you have the line

$url .= "&key=YOURKEY";

I believe it should be:

$url .= "&key=ABQIAAAAST4sux1VdyzTps5CXY6vkRSUi5n9UPmza1-UGBYjjmCGJKKnhxSCkKnQbKKobNPpxt5Cyo5Dt_X-yA";
Avatar of designersx

ASKER

buddy i have not done this intentionally, i have actually put my key while executing . this was the code provided by one of ur members and at that i did not try and now when i am trying, it is giving the problem.

i want that it should tell me the latitude and longitude according to the address. if u try the code putting my key, u will see no output. please try it.
To be honest, I don't really understand what you are trying to do!

I can't try your code, because each key only works on the domain that it was signed up for, so this particular key will only work when coming from the web address that you told Google it was coming from...
Are you trying to do something like this...

http://pagesperso-orange.fr/universimmedia/geo/loc.htm

Have a look at the source code for that, it is quite helpful.
yes exactly this i want. could u give me the script for this?

i will be very thankful to you.

kind regards
yogesh
this code actually contains the 2 parts.

1)  to know the latitude and longitude from address.
2) to show the google map from latitude and longitude.

i was facing the problem in the 1st step.

yogesh.
I don't own that web page, so the script isn't mine to give.

However, it is freely available - just right click on the page, and click 'View Source'.
Then take a look for yourself!

Glad to have helped :)
Avatar of b0lsc0tt
designersx,
As mentioned above there is no way for someone else to test your key, except on the domain you set it up with.  Do you have a page on that domain that uses the key?  If so provide the URL.
What is the problem exactly?  The lat & lng should be in the results but won't be the only thing.  Before you were getting an error because you couldn't use cUrl.  Has that been fixed?  If not then you need to fix it.  If you can't use it then we might be able to try another method but there won't be too many other options if you have to stick to PHP.
If there is some other error then provide details.  We can't fix something when there is no way to know what is broken.  Did you try entering a test address like ...
1600 Amphitheatre Parkway, Mountain View, CA
Let me know if you have a question about any of this.
bol
By the way ... if you can't use cUrl and have no other options then will you just need to get a limited number of addresses geocoded?  How many roughly?  Just this one time or will you need more done regularly and often?
The reason I ask is there are public website that could probably offer this too.  That would be an option if this were a one time thing and you just had a few to geocode.
bol
my curls are enabled now. the problem is that code should echo the latitude and longitude when i fill the address and click on submit button but it does not, so i request u to please check with ur own key because i have not changed anything in the code u provided me.

kind regards
yogesh
Had another quick look at your code and noticed this line:

<form name="form1" action="" method="post">

At the moment this form isn't doing anything! I expect when you click submit, nothing happens.

Try this:

<form name="form1" action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
no buddy, i had done that previously, not working yet.
>> so i request u to please check with ur own key because i have not changed anything in the code u provided me. <<
Provide the address you entered (or one you want to test since I don't want you to post personal info) and I will see what I get.  I need something to test since I know the ones I try, including the one I gave you, work.
Now ... did you try mine???  I asked a few questions in http:#a24029181 and it seems you ignored them all.  I will not continue to bug for responses and answers.  I ask them because they are important so I can provide help.  If you don't wish to answer them then that is your choice but I will be very limited in how I can help, if I can help at all.  If they weren't clear then let me know.  I won't bother or repeat myself again.
Thanks for confirming cUrl was enabled.
bol
p.s.  There are addresses and places in the world Google can't resolve or geocode.
sir,

http://www.designers-x.com       is the url for which i was trying. After that i entered my google username and password. the exact problem was the page gets posted and nothing happens, means value of $result is not echoed.
ASKER CERTIFIED SOLUTION
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
atlast working , i tested thanks for ur cooperation to bear me so long  and for ur patience.

finally got it.

i am increasing the 200 points for ur help. leave all the rest things

http://www.designers-x.com, Google username and password?  now

kind regards
yogesh
fantastic
I'm glad you have it working now.  Thanks for the grade, the points and the fun question.
bol
** I will have an admin remove the URL from my last comment.  The PHP code for that page is available above. **