SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict buildakicker's Avatar
    Join Date
    Jun 2005
    Location
    NorCal
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile myFunction($var) how to get to write to file (CODE)

    Hello all,

    I am working on getting some addresses Geocoded then added to a text file, that will sooner than later be a .kml file that will then be loaded into a google map to display my users location. I can get the info to display, but I cannot get it to write to a file... here's the code...

    I have been trying to get this function:

    PHP Code:
    <?php
    //###### GEOCODER #######-->
    //Code from http://www.coastrentals.com/tech/2006/09/20/php-geocoding-tutorial-with-the-google-maps-api-part-one/
    function geocoder($location){
        
    //Set up our variables
        
    $longitude '';
        
    $latitude '';
        
    $precision '';
        
    //Three parts to the querystring: q is address, output is the format (
        
    $key 'ABQIAAAAJLBChhC8rzvRheEI30N73RT4hU-87Z5apuJV-ynhcKXXndNqkxTmLED3HqGEQZDrKtFMbbVhsHj_Lw';
        
    $address urlencode($location);
        
    $url 'http://maps.google.com/maps/geo?q='.$address.'&output=csv&key='.$key;
        
        
    $ch curl_init();
        
        
    curl_setopt($chCURLOPT_URL$url);
        
    curl_setopt($chCURLOPT_HEADER,0);
        
    curl_setopt($chCURLOPT_cityAGENT$_SERVER['HTTP_city_AGENT']);
        
    curl_setopt($chCURLOPT_FOLLOWLOCATION1);
        
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
        
        
    $data curl_exec($ch);
        
    curl_close($ch);
        
        
    /*echo 'Data: '. $data.'';*/
        
        
    if (strstr($data,'200'))
        {
        
    $data explode(',',$data);
        
        
    $precision $data[1];
        
    $latitude $data[2];
        
    $longitude $data[3];
        
    echo 
    '<p>Latutide: '.$latitude'<br />';
    echo 
    'Longitude: '.$longitude.'</p>';
        
        } else {
        echo 
    '<p>Error in geocoding!</p>';
        }
    }
    To write to a file like this:
    PHP Code:
    while($row mysql_fetch_array($results))
    {
        
    $location $row['city'];
        echo 
    $location '<br />';
        
    fwrite($hFile$location);
        
    fwrite($hFile"\n");
        
    geocoder($location);
        
    fwrite($hFile"\n");

    However, I am not a very good programmer. I cannot figure out how to get the $lat / $lon out of the function and write it to the file. The Cities write out fine...

    Any clues?

    Thanks!
    SKILEASES.COM - FREE rental listings!
    WILDFIREBLOG.COM - Wildland Fire microblog!

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    get geocoder() to return a string instead of echoing it.

    return '<p>Latutide: '.$latitude. '<br />' . 'Longitude: '.$longitude.'</p>';

    then call it here:
    PHP Code:
    while($row mysql_fetch_array($results))
    {
        
    $location $row['city'];
        
    $location .= geocoder$location ) ;
     
    //   echo $location . '<br />'; // check this first, uncomment it
        
    fwrite($hFile$location);
        
    fwrite($hFile"\n");
       } 

  3. #3
    SitePoint Addict buildakicker's Avatar
    Join Date
    Jun 2005
    Location
    NorCal
    Posts
    378
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks that worked great! I wasn't getting the vars out of Geocoder...
    SKILEASES.COM - FREE rental listings!
    WILDFIREBLOG.COM - Wildland Fire microblog!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •