Solved

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

Posted on 2009-03-31
18
511 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:designersx
  • 8
  • 5
  • 5
18 Comments
 
LVL 3

Expert Comment

by:uncleramsay
ID: 24027366
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";
0
 

Author Comment

by:designersx
ID: 24027406
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.
0
 
LVL 3

Expert Comment

by:uncleramsay
ID: 24027426
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...
0
 
LVL 3

Expert Comment

by:uncleramsay
ID: 24027449
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.
0
 

Author Comment

by:designersx
ID: 24027512
yes exactly this i want. could u give me the script for this?

i will be very thankful to you.

kind regards
yogesh
0
 

Author Comment

by:designersx
ID: 24027528
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.
0
 
LVL 3

Expert Comment

by:uncleramsay
ID: 24027541
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 :)
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24029181
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
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24029205
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
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:designersx
ID: 24036031
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
0
 
LVL 3

Expert Comment

by:uncleramsay
ID: 24036651
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">
0
 

Author Comment

by:designersx
ID: 24036703
no buddy, i had done that previously, not working yet.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24040852
>> 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.
0
 

Author Comment

by:designersx
ID: 24042942
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.
0
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 300 total points
ID: 24043554
What is the exact page that has the PHP geocode? The page above is a landing page where the visitor seems to need to choose to Flash or HTML. I don't see anything like the html the php above would create.
What is an address you want to test?
No login is needed for the page I provided. What are you referring to when you mentioned entering a Google username and password? What page are you on?
I have the PHP code above on a page at ** URL REMOVED by b0lsc0tt - ZA ** . That isn't a URL I want to keep public so I will have it removed when this is closed. The page uses the PHP above and provides a way to enter an address. It should be basically the same page you get when you use the php code with the php above. I have only made small changes to the php on the site above and the php you have. What do you get trying your address at this page? What about my test address?
bol
0
 

Author Comment

by:designersx
ID: 24046260
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
0
 

Author Closing Comment

by:designersx
ID: 31564720
fantastic
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 24052027
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. **
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now