Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 9 of 9
  1. #1
    Junior Member
    Join Date
    Mar 2007
    Posts
    8
    Member #
    15000
    Hello,

    I have a client that wants site visitors to enter their US ZIP Code on a "Store Locator" page. For now they don't need to locate a store location, because they only have one store, but they are thinking about expanding and would like to know where the demand is for a new store location. I have the form on the page, and it dumps the data into a mySQL database, but I have no ideas as to what options might be out there to pull the ZIP Code data from the database and dispaly it on a map so the client can easily view it. Does anyone have any ideas? What about the Google maps API?

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    Google API is great.

    You could do your own MySQL thing, or spend a few bucks to purchase a ready-made script ...
    like this: http://www.got-zip.com/zblog/2005/10...api-zip-codes/

    Since you already have the MySQL database, you're almost there.

    Sign up for a key and start playing around:
    http://www.google.com/apis/maps/documentation/

    Look at the PHP/MySQL at the bottom of this page:
    http://www.map-server.com/googlemaps/overview.html


  4. #3
    Junior Member
    Join Date
    Mar 2007
    Posts
    8
    Member #
    15000
    Thanks so much for the links. I think I'll try to write a script myself to save some cash, but the Google API doesn't look too bad. I really appreciate the help!

  5. #4
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    When a user enters their zipcode, you save that zipcode ... but do you
    also save the Earth coordinates for that zipcode?

    http://geocoder.us/


  6. #5
    Junior Member
    Join Date
    Mar 2007
    Posts
    8
    Member #
    15000
    I've decided to drop the ZIP code idea and go with the full address to hone in on the actual location better. Well, I've gotten the locations to show up on my map. I'm using a mySQL database to store the latitude and longitude data. Currently, I'm populating those tables manually; I can't figure out how to get the geocodes for the locations from Google into the database automatically. I did find a PHP script that sends the HTTP request to Google and pulls back the KML file with the coordinates for each location, but the trouble is I can't figure ot how to parse the file to pick out the lat and lon data and then dump it into the database. Here's how I'm getting the KML file from Google:

    PHP Code:
    <?php

       
    // My Google Maps API key
       
    $key "[my key]";

       
    // Connect to the MySQL database
       
    $conn mysql_connect("localhost:/tmp/mysql5.sock""[username]""[password]");

       
    // Select the database
       
    $db mysql_select_db("[my database]");

       
    // Query the table
       
    $query "SELECT id, street, city, state, zip FROM map";
       
    $result mysql_query($query) or die(mysql_error());

       
    // Loop through each row, submit HTTP request, output KML file
       
    while (list($id$street$city$state$zip) = mysql_fetch_row($result))
       {
          
    $mapaddress urlencode("$street $city $state $zip");

          
    // Desired address
          
    $url "http://maps.google.com/maps/geo?q=$mapaddress&output=xml&key=$key";

    // Display KML Data
    echo $page;
          echo 
    "<br>";
       }
    ?>
    Does anyone know if there is an easy way to parse the file and upload the coordinates to my database tables? Thanks!

  7. #6
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    Cut and paste a copy of the output here ...
    The "Display KLM Data" part so we can see what you need to parse.


  8. #7
    Junior Member
    Join Date
    Mar 2007
    Posts
    8
    Member #
    15000
    Sorry, I suppose that might help. Here's the first entry of the KML file:

    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://earth.google.com/kml/2.0">
       <Response>
          <name>807 Rolling Green Drive Green Bay WI 54313</name>
             <Status>
                <code>200</code>
                <request>geocode</request>
             </Status>
             <Placemark>
                <address>807 Rolling Green Dr, Green Bay, WI 54313, USA</address>
                <AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
                   <Country>
                      <CountryNameCode>US</CountryNameCode>
                      <AdministrativeArea>
                         <AdministrativeAreaName>WI</AdministrativeAreaName>
                         <SubAdministrativeArea>
                            <SubAdministrativeAreaName>Brown<SubAdministrativeAreaName>
                         <Locality>
                            <LocalityName>Green Bay</LocalityName>
                            <Thoroughfare>
                               <ThoroughfareName>807 Rolling Green Dr</ThoroughfareName>
                            </Thoroughfare>
                            <PostalCode>
                               <PostalCodeNumber>54313</PostalCodeNumber>
                            </PostalCode>
                         </Locality>
                      </SubAdministrativeArea>
                   </AdministrativeArea>
                </Country>
             </AddressDetails>
             <Point><coordinates>-88.126624,44.528189,0</coordinates></Point>
          </Placemark>
       </Response>
    </kml>
    Here's what the live output looks like:

    http://www.glime.us/CashHaus/php/geocode.php

    Thanks again. This is probably something easy but I just can't figure it out.

  9. #8
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 times
    I saved your output as a file ("klm.htm") and then ran this test script:

    PHP Code:
    <?php

    // Parse Coordinates out of KLM 

    // Get page
    $url "klm.htm";
    $page implode(""file($url)); 

    // Get contents of text between <Response> and </Response>
    preg_match_all ("/<Response>([^`]*?)<\/Response>/"$page$matches);

    // Loop through each item
    foreach ($matches[0] as $match) {

    // Get contents between <coordinates> and </coordinates>
    preg_match ("/<coordinates>([^`]*?)<\/coordinates>/"$match$temp);
    $coord $temp['1'];
    $coord strip_tags($coord);
    $coord trim($coord);

    }

    //split apart the variable $coord if you need to.
    list ($c1,$c2,$c3) = split (','$coord);

    echo 
    "Coordinates: $coord <br> \n";
    echo 
    "The 3 split values .... <br> \n";
    echo 
    "Coordinate 1: $c1 <br> \n";
    echo 
    "Coordinate 2: $c2 <br> \n";
    echo 
    "Coordinate 3: $c3 <br> \n";
    ?>

    Here is the output:
    Coordinates: -88.126624,44.528189,0
    The 3 split values ....
    Coordinate 1: -88.126624
    Coordinate 2: 44.528189
    Coordinate 3: 0



    So you can do the same thing with the results of your output.

    PS, that output link you gave fails.
    But I think you'll get enough from the example above to make it work.




    .


  10. #9
    Junior Member
    Join Date
    Mar 2007
    Posts
    8
    Member #
    15000
    Thanks so much, mlseim. That worked like a charm. I added:

    mysql_query("UPDATE map SET lon = $c1, lat = $c2 WHERE id = $id");

    to the geocode.php file and success! The points are showing up correctly on the map page now. I really appreciate your help. I never would have figured that parsing part out on my own.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 12:28 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com