Question : Google Maps , ahhhh help pls!

Hi All,

I have a new project coming up and it invloves integrating google maps, now my problem is that I have never done any thing like this and from the outset it looks like a painfull coding excersie, but i like a challenge.

I have started looking at the google map API, and downloaded the HTML example, but What I need to do is to able to select a city, from a drop down and then send that of as request to generate a map to point to that location.

Questons
1) Can I control the size of the map window?
2) How do I translate a city name or address/postcode details into map cords or can google do this for me if so how please?
3) If I get a map up, can I plot different locations on it if so how.

PHP/javascripy/HTML and CSS is what I develop webpages in normaly.

Many thanks in advance for your help
Bob

Answer : Google Maps , ahhhh help pls!

Answers... After you see this, please read this page carefully:
http://code.google.com/apis/maps/documentation/staticmaps/

(1) You can control the size of the window up to 640x640 pixels
(2) Google can do this for you - the static map API can accept address fragments or even names of landmarks.  Or you can use a geocoder.  Both Google and Yahoo offer geocoders.  Please see the code snippet for an example of this.
(3) You can plot a lot of different locations on a map.  I will post a follow-up response with an example showing some of the ways you can use the Google Maps API to post multiple pushpins or icons on a map.

Best regards, ~Ray
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
<?php
// RAY_class_SimpleGeoCoder.php
error_reporting(E_ALL);


// API KEYS, ETC., IF REQUIRED
// require_once('local_data.php');


// PUT UP A FORM TO RECEIVE THE ADDRESS INFORMATION
if (empty($_GET))
{
    ?>
    <html>
    <head>
    <title>Yahoo/Google SimpleGeoCoder Demo</title>
    </head>
    <body>
    <form method="get">
    Addr: <input type="text" name="a" autocomplete="off" />
    City: <input type="text" name="c" autocomplete="off" />
    ST:   <input type="text" name="s" autocomplete="off" size="2" />
    Zip:  <input type="text" name="z" autocomplete="off" size="8" />
    <input type="submit" value="go" />
    </form>
    </body>
    </html>
    <?php die();
}


// A FREEFORM LOCATION STATEMENT IS OPTIONAL - GET ADDRESS, CITY, STATE, ZIP FROM THE URL
$location = '';
if ($_GET["a"] != '') { $location .= $_GET["a"] . ' '; }
if ($_GET["c"] != '') { $location .= $_GET["c"] . ' '; }
if ($_GET["s"] != '') { $location .= $_GET["s"] . ' '; }
if ($_GET["z"] != '') { $location .= $_GET["a"] . ' '; }
$location = trim($location);


// PREPARE THE GEOCODER
$demo          = new SimpleGeoCoder;
$demo->address = $_GET["a"];
$demo->city    = $_GET["c"];
$demo->state   = $_GET["s"];
$demo->zip     = $_GET["z"];


// TEST THE YAHOO! GEOCODER
$demo->geocodeYahoo();
echo PHP_EOL . "YAHOO! ";
print_rr($demo);


// TEST THE GOOGLE GEOCODER
$demo->geocodeGoogle();
echo PHP_EOL . "GOOGLE ";
print_rr($demo);

// ALL DONE
die();


// SIMPLE GEOCODER CLASS
class SimpleGeoCoder
{
    // DECLARE THE INPUT DATA
    public $location;

    // USE THIS FOR A FREEFORM QUERY, OR USE THE PARTS
    public $address;
    public $city;
    public $state;
    public $zip;

    // DECLARE THE WORKING DATA
    private $precis;

    // DECLARE THE OUTPUT DATA
    public $latitude;
    public $longitude;
    public $precision;
    public $warning;
    public $geocoder;

    // DECLARE THE CONSTRUCTOR
    public function __construct()
    {
        $this->latitude  = 0.0;
        $this->longitude = 0.0;
        $this->precision = FALSE;  // WANT A VALUE OF 5 OR HIGHER, HIGHER IS BETTER
        $this->warning   = '';
        $this->geocoder  = '';
        unset($this->precis);
    }

    // DECLARE THE DATA-CLEANUP
    private function _cleanup($str)
    {
        $str = preg_replace('/[^\' a-zA-Z0-9&!#$%()"+:?\/@,_\.\-]/', '', $str);
        return trim(preg_replace('/\s\s+/', ' ', $str));
    }

    // DECLARE THE YAHOO! VERSION OF THE WORKHORSE
    public function geocodeYahoo()
    {
        if (!defined('YAHOO_API')) define('YAHOO_API', 'YAHOO_API');
        $this->geocoder = 'Yahoo!';
        $yahooUrl       = "http://local.yahooapis.com/MapsService/V1/geocode?&appid=" . YAHOO_API;

        // YOU CAN ASK FOR A FREEFORM QUERY
        if ($this->location != '')
        {
            $yahooUrl .= "&location=" . urlencode($this->_cleanup($this->location));
        }

        // YOU CAN ASK FOR INDIVIDUAL PIECES OF AN ADDRESS
        else
        {   $yahooUrl .= "&street=" . urlencode($this->_cleanup($this->address));
            $yahooUrl .= "&city="   . urlencode($this->_cleanup($this->city));
            $yahooUrl .= "&state="  . urlencode($this->_cleanup($this->state));
            $yahooUrl .= "&zip="    . urlencode($this->_cleanup($this->zip));
        }

        // EXECUTE YAHOO GEOCODER QUERY
        // NOTE - USE ERROR SUPPRESSION OR IT WILL BARK OUT THE YAHOO API KEY - ON FAILURE RETURNS HTTP 400 BAD REQUEST
        if ($yfp = @fopen($yahooUrl, 'r'))
        {
            $yahooResponse = '';
            while (!feof($yfp))
            {
                $yahooResponse .= fgets($yfp);
            }
            fclose($yfp);
        }
        else
        {
            return FALSE;
        }

        // EXAMINE THE RESULT
        if ($yahooResponse != '') // NOT EMPTY, WE GOT DATA
        {
            $ydata    = new SimpleXMLElement($yahooResponse);

            // CHECK FOR ANY ERROR MESSAGE, IF NONE, EXTRACT THE DATA POINTS
            $yerror    = $ydata->Message;
            if ($yerror == '')
            {
                $this->precis    = (string)$ydata->Result["precision"];
                $this->warning   = (string)$ydata->Result["warning"];
                $this->latitude  = (string)$ydata->Result->Latitude;
                $this->longitude = (string)$ydata->Result->Longitude;

                // THESE STATEMENTS CAN BE USED TO RETURN NORMALIZED ADDRESS
                $this->address   = (string)$ydata->Result->Address;
                $this->city      = (string)$ydata->Result->City;
                $this->state     = (string)$ydata->Result->State;
                $this->zip       = (string)$ydata->Result->Zip;

                // SET PRECISION TO A NUMBER VALUE
                if ($this->precis == 'zip')     { $this->precision = "5"; }
                if ($this->precis == 'street')  { $this->precision = "6"; }
                if ($this->precis == 'address') { $this->precision = "8"; }
            }
            else
            {
                return FALSE;
            }
        }

        // NO RESULT - SOMETHING IS SICK AT YAHOO
        else
        {
            return FALSE;
        }

        return TRUE;
    } // END function geocodeYahoo


    // DECLARE THE GOOGLE VERSION OF THE WORKHORSE
    public function geocodeGoogle()
    {
        if (!defined('GOOGLE_API')) define('GOOGLE_API', 'GOOGLE_API');
        $this->geocoder = 'Google';
        $googleUrl      = "http://maps.google.com/maps/geo?key=" . GOOGLE_API . "&output=csv";
        if ($this->location != '') // YOU CAN ASK FOR A FREEFORM QUERY
        {
            $googleUrl .= "&q=" . urlencode($this->_cleanup($this->location));
        }
        else // YOU CAN ASK FOR INDIVIDUAL PIECES OF AN ADDRESS
        {
            $googleUrl .= "&q=" . urlencode(trim(
                    $this->_cleanup($this->address)
            . ' ' . $this->_cleanup($this->city)
            . ' ' . $this->_cleanup($this->state)
            . ' ' . $this->_cleanup($this->zip)
            ));
        }

        // EXECUTE GOOGLE GEOCODER QUERY
        if ($gfp = @fopen($googleUrl, 'r'))
        {
            $googleResponse = '';
            while (!feof($gfp))
            {
                $googleResponse .= fgets($gfp);
            }
            fclose($gfp);
        }
        else
        {
            return FALSE;
        }

        // EXTRACT THE DATA FROM THE CSV STRING
        $gdata    = explode(',',$googleResponse);
        if ($gdata[0] != '200') // RESPONSE CODE SHOULD BE '200' -- IF 602 - BAD ZIP CODE OR UNUSABLE ADDRESS
        {
            return FALSE;
        }
        $this->precision = $gdata[1]; // GEOCODE ACCURACY - ZIP CODE = 5, HIGHER NUMBERS ARE BETTER
        $this->latitude  = $gdata[2];
        $this->longitude = $gdata[3];
        return TRUE;
    } // END function geocodeGoogle

} // END class SimpleGeocoder



// UNRELATED FUNCTION TO MAKE THE OUTPUT SHOW THE PUBLIC INFORMATION ONLY
function print_rr($thing)
{
    $str = print_r($thing, TRUE);
    $arr = explode(PHP_EOL, $str);
    $num = count($arr) - 1;
    foreach ($arr as $ptr => $txt)
    {
        if (preg_match('/:private]/', $txt))
        {
            unset($arr[$ptr]);
        }
    }
    echo implode(PHP_EOL, $arr);
}
Random Solutions  
 
programming4us programming4us