ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

php/sql Query + Geocoding [RESOLVED]

October 27, 2007 11:24pm

Subscribe [1]
  • #1 / Oct 27, 2007 11:24pm

    David J

    17 posts

    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>
  • #2 / Oct 28, 2007 1:18pm

    David J

    17 posts

    Alright, i figured everything out. Putting $postcode was correct in the url. Turns out the reason it was failing was because a couple of the postcodes didn’t actually exist. So now i’ve added an if statement to only output the member’s marker if the status returned from google was 200. Here is my final working code:

    <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))
    
        {
    
        $postcode = $row['postcode'];
        
        $postcode = urlencode("$postcode");
    
        $postcode = substr($postcode,0,(strlen($postcode)-1));
        
        // Desired address
        $url = "http://maps.google.com/maps/geo?q=$postcode&output=xml&key;=$key";
    
        // Retrieve the URL contents
        $page = file_get_contents($url);
    
        // Parse the returned XML file
        $xml = new SimpleXMLElement($page);
        
        // Check for error
        list($code) = explode(",", $xml->Response->Status->code);
        
        if ($code == "200")
            {
            // Parse the coordinate string
            list($longitude, $latitude, $altitude) = explode(",", $xml->Response->Placemark->Point->coordinates);
            
    
            // Output the coordinates
            echo "<marker ";
            echo "lat=\"$latitude\" lng=\"$longitude\" name=\"";
            echo $row['screen_name'];
            echo "\" photo=\"";
            echo $row['photo_filename'];
            echo "\" memid=\"";
            echo $row['member_id'];
            echo "\" >";
            }
    
        }
    
    ?>
    </markers>
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases