Address geocode then echo [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amh1010
Forum Newbie
Posts: 19
Joined: Sat Feb 06, 2010 1:49 pm

Address geocode then echo [SOLVED]

Post by amh1010 »

Could someone help me write a PHP script that looks up the lat/long values of an address, then echos (displays) the latitude and longitude? Thanks for any help
Last edited by amh1010 on Wed Feb 10, 2010 3:30 pm, edited 1 time in total.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Address geocode then echo

Post by klevis miho »

What is the address?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Address geocode then echo

Post by josh »

You should use something off the shelf like Google's geo coding service. The way they work internally is by doing lockups against street address block level data ( 100 block, 200 block ,etc.. so they can pinpoint to you +/- the distance of "one city block" usually).
amh1010
Forum Newbie
Posts: 19
Joined: Sat Feb 06, 2010 1:49 pm

Re: Address geocode then echo

Post by amh1010 »

Got it working using the XML output file. Here's the code:

Code: Select all

 
$location=urlencode($address.", ".$city.", ".$state.", ".$zip);
 
$url = "http://maps.google.com/maps/geo?q=".$location."&output=xml&key=$key";
// Retrieve the URL contents
$page = file_get_contents($url);
// Parse the returned XML file
$xml = new SimpleXMLElement($page);
// Retrieve the desired XML node
$output= $xml->Response->Placemark->Point->coordinates;
$array=array();
$array=explode(",",$output);
 
echo $array[0];
echo $array[1];
 
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Address geocode then echo [SOLVED]

Post by josh »

Yeah simpleXML rocks.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Address geocode then echo [SOLVED]

Post by JNettles »

While on the subject, why does SimpleXML return nodes as "SimpleXMLElement"s???? While I appreciate SimpleXMLs ability to easily parse out an XML document, the (to me) inexplicable SimpleXMLElement is nothing but a pain to deal with. There's no problem echoing it or anything but the moment you try to do anything fancy like iterate by reference it becomes a pain.
Last edited by JNettles on Fri Feb 12, 2010 12:20 pm, edited 2 times in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Address geocode then echo [SOLVED]

Post by josh »

Because they need to return to you an object with data+state, not just state (otherwise an array would be used)
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Address geocode then echo [SOLVED]

Post by JNettles »

IMO, SimpleXML would be truly great if it just parse them out to their respective primitive data types. Or just give me a string to work with. As it is now I only find SimpleXML useful if I'm going straight from XML to screen output, rather than filling other data structures (which is far more often the case).
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Address geocode then echo [SOLVED]

Post by josh »

If it returned strings then I would be upset because I want an object. With the object they give you can get the string. You could also wrap the simpleXML class and change the way it works for your own purposes, no need to over simplify for everyone else (thats the beauty of software, you can just change it for yourself by wrapping the class.)

Code: Select all

 
class Core_Class
{
 function foo() { return '1'; }
}
 

Code: Select all

 
class My_Extended_Class extends Core_Class
{
 function foo() {
  $val =  parent::foo();
  $val = intval( $val ) ;; /do your casting
  return $val
 }
 
}
 
See, just "wrap" the class so your type casting is encapsulated instead of scatted thru your app. SimpleXML isnt the one making you put (string) everywhere, thats was a choice you choose to make.
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Address geocode then echo [SOLVED]

Post by JNettles »

I realize that the element has value in different settings, but the lack of any built-in toString method in SimpleXMLElement is just a sin. It needs functionality to automatically return the value as its respective primitive type. Yes, I can write a class extending SimpleXML's functionality but I shouldn't have to - I'm pretty sure that the first thing you're taught to do when creating a class is to build a toString method. It needs to be part of the core - this is just one more file that I have to include, giving SimpleXML the functionality it should already have.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Address geocode then echo [SOLVED]

Post by josh »

$a->getAsString()
(string)$a->get()

they are both the same amount of typing ;-)
User avatar
JNettles
Forum Contributor
Posts: 228
Joined: Mon Oct 05, 2009 4:09 pm

Re: Address geocode then echo [SOLVED]

Post by JNettles »

Regardless - its just good class design to have a toString method that is visible, documentable, and clearly overridable through a class hierarchy. I realize I'm splitting hairs here but its just little stuff like this that irks me about PHP's language design.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Address geocode then echo [SOLVED]

Post by josh »

It does use __toString, how do you think (string)$object returns a string? It can be override, look at my example. The only reason it couldn't be overriden is inexperience on your part. ;-)

With type coerceion it doesnt matter if you pass an object or string anyways.
Post Reply