Hi, i’m trying to setup a map of member locations by geocoding just the member’s postcodes (which i have as a custom field).
My code below is to output an XML file with the markers for each member. I’ve almost got it sorted out but being an sql/php dummy i can’t figure out how to pass the postcode variable into the http request for geocoding. I want each member’s postcode to show up in the url where i currently have “XXXXXX” written in the code below.
I thought using $postcode where XXXXXX is should work, but the XML file gives me “not well-formed” parsing error. If i substitute a valid postal/zip code in for XXXXXX the geocoding and lat/lng output work, so the rest of the code seems to be working fine. Any help would be greatly appreciated!
<markers>
<?php
// Google Maps API key
$key = "YOUR API KEY";
global $DB;
$query = "SELECT m.member_id, m.screen_name, m.photo_filename, c.m_field_id_3 AS postcode FROM exp_members m LEFT JOIN exp_member_data c ON m.member_id=c.member_id";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
echo "<marker ";
$postcode = $row['postcode'];
$postcode = urlencode("$postcode");
$postcode = substr($postcode,0,(strlen($postcode)-1));
// Desired address
$url = "http://maps.google.com/maps/geo?q=XXXXXX&output=xml&key;=$key";
// Retrieve the URL contents
$page = file_get_contents〈$url〉;
// Parse the returned XML file
$xml = new SimpleXMLElement($page);
// Parse the coordinate string
list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);
// Output the coordinates
echo "lat=\"$latitude\" lng=\"$longitude\" name=\"";
echo $row['screen_name'];
echo "\" photo=\"";
echo $row['photo_filename'];
echo "\" memid=\"";
echo $row['member_id'];
echo "\" >";
}
?>
</markers>