Address geocode then echo [SOLVED]
Moderator: General Moderators
Address geocode then echo [SOLVED]
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
What is the address?
Re: Address geocode then echo
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
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]
Yeah simpleXML rocks.
Re: Address geocode then echo [SOLVED]
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.
Re: Address geocode then echo [SOLVED]
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]
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]
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.)
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.
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
}
}
Re: Address geocode then echo [SOLVED]
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]
$a->getAsString()
(string)$a->get()
they are both the same amount of typing
(string)$a->get()
they are both the same amount of typing
Re: Address geocode then echo [SOLVED]
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]
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.
With type coerceion it doesnt matter if you pass an object or string anyways.