Page 1 of 1

Address geocode then echo [SOLVED]

Posted: Tue Feb 09, 2010 9:46 pm
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

Re: Address geocode then echo

Posted: Wed Feb 10, 2010 1:42 am
by klevis miho
What is the address?

Re: Address geocode then echo

Posted: Wed Feb 10, 2010 2:52 am
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).

Re: Address geocode then echo

Posted: Wed Feb 10, 2010 3:28 pm
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];
 

Re: Address geocode then echo [SOLVED]

Posted: Fri Feb 12, 2010 11:54 am
by josh
Yeah simpleXML rocks.

Re: Address geocode then echo [SOLVED]

Posted: Fri Feb 12, 2010 12:15 pm
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.

Re: Address geocode then echo [SOLVED]

Posted: Fri Feb 12, 2010 12:16 pm
by josh
Because they need to return to you an object with data+state, not just state (otherwise an array would be used)

Re: Address geocode then echo [SOLVED]

Posted: Fri Feb 12, 2010 12:22 pm
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).

Re: Address geocode then echo [SOLVED]

Posted: Sat Feb 13, 2010 10:32 am
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.

Re: Address geocode then echo [SOLVED]

Posted: Tue Feb 16, 2010 9:14 am
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.

Re: Address geocode then echo [SOLVED]

Posted: Tue Feb 16, 2010 12:30 pm
by josh
$a->getAsString()
(string)$a->get()

they are both the same amount of typing ;-)

Re: Address geocode then echo [SOLVED]

Posted: Tue Feb 16, 2010 1:43 pm
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.

Re: Address geocode then echo [SOLVED]

Posted: Tue Feb 16, 2010 6:41 pm
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.