<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>designing4u.de &#187; Google Maps</title>
	<atom:link href="http://www.designing4u.de/tag/google-maps/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designing4u.de</link>
	<description>Yet Another Coding Blog</description>
	<lastBuildDate>Fri, 29 Jul 2011 08:11:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Zip code search using jQuery and Google maps &#8211; displaying results in a defined radius</title>
		<link>http://www.designing4u.de/2009/05/zip-code-search-using-jquery-and-google-maps-displaying-results-in-a-defined-radius/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=zip-code-search-using-jquery-and-google-maps-displaying-results-in-a-defined-radius</link>
		<comments>http://www.designing4u.de/2009/05/zip-code-search-using-jquery-and-google-maps-displaying-results-in-a-defined-radius/#comments</comments>
		<pubDate>Sun, 10 May 2009 15:18:36 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[distance]]></category>
		<category><![CDATA[distance from zip code]]></category>
		<category><![CDATA[GMap2]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[zip code]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=32</guid>
		<description><![CDATA[Last time I wrote about handling a Google map event, which is triggered, when a user moves around the map. I only mentioned that I had to use this solution for one of my projects, which goal was to display companies branches in a certain distance radius. I decided that it probably will be really [...]]]></description>
			<content:encoded><![CDATA[<p>Last time I wrote about handling a Google map event, which is triggered, when a user moves around the map. I only mentioned that I had to use this solution for one of my projects, which goal was to display companies branches in a certain distance radius. I decided that it probably will be really interesting for all of you, how to display all company branches according to user input, therefore this time I will show you, how to retrieve the addresses of the places from a MySQL database, based on the zip code and radius provided by a user. I will also split this example in two parts. Todays part will only handle retrieving the results, displaying a simple navigation and markers on the map. In the second part we will push our project a little bit farther and we will allow our user to display the directions from the zip code he or she provided to the branch of his or hers choice.<br />
<span id="more-32"></span><br />
To see a working example of this tutorial you can visit <a href="http://www.designing4u.de/examples/calculating-distance/index.html" target="_blank">this</a> site.</p>
<p>As I already mentioned in my previous post, to measure the distance between two points on a sphere one can use Haversine formula. For a detailed description I suggest visiting the wikipedia site. In our example we will use a simplified version of Haversine formula, which is caused by a really simple reason. Executing a web query with the actual Haversine formula might lead to a really slow response from the database and our simplified version will retrieve results which do not differentiate that much with the original version.</p>
<p>For retrieving the company branches in a certain radius we will first build a php class, which will be responsible for translating the zip code into geographical lengths and then use this values to retrieve the results and return them as json_encoded() data. Because our class will be relatively long, I will split it into parts and explain it line by line.</p>
<p>Since we are using an AJAX request to retrieve our data and the location of our php file is easy to guess just by looking at our jQuery code (will come after php class), we add a simple check of the apache environmental variable to check, if it actually is an XMLHttpRequest. A lot of you will probably say that this security measure is not enough, but for this example, it will at least stop all the robots trying access this file and since looking up addresses might consume a lot of resources of your server, this will save you some speed lost on servers with high traffic.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_X_REQUESTED_WITH&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">'XMLHttpRequest'</span><span style="color: #009900;">&#41;</span>
  <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'why are you doing this??'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In line 3 we create our class. Since the subject of this example is finding company branches, lets call it Branch. In lines 5 to 13, we define some variables, which we will use in our class. I think they are pretty much self explanatory, and you will understand, what is their usage just by looking at the rest of the class.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Branches <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'YOUR GOOGLE MAPS KEY'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$_host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$_db_user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'root'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$_db_pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'pass'</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000088;">$_db_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'db'</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$zip</span><span style="color: #339933;">,</span> <span style="color: #000088;">$radius</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data_set</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In our constructor we connect to the database and set the radius and zip properties. You could notice that I convert the zip property to integer as well and I additionally add leading zero for strings, which length equals 4. Well, this class was written for German zip codes. You might want to change this parts so it suits your needs. After all it will be important in the part where we will convert the zip code to geographical length, so Google can find this zip code in their database.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_host</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_db_user</span><span style="color: #339933;">,</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_db_pass</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_db_name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">radius</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'radius'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'zip'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">// add leading zeros in case of 0... zip codes</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%05s</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In line 26 we define a public method translateZipToLatLng() and in line 39 a private method getGeolocation(), which both will be responsible for translating the zip code into the geographical length. To get a detailed description, what this two methods actually do, refer to my other post:  <a href="http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/">Geolocation class - retrieving longitude and latitude using google maps</a>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> translateZipToLatLng<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://maps.google.com/maps/geo?q='</span> <span style="color: #339933;">.</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">zip</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">', Germany'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&amp;output=csv&amp;key='</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_key</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getGeolocation</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #0000ff;">'200'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$geo</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$geo</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$geo</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getGeolocation<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$init</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #339933;">,</span> CURLOPT_USERAGENT<span style="color: #339933;">,</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;HTTP_USER_AGENT&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$response</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$init</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$response</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In line 51 we define a private method getResults, which, as you can see, will be responsible for retrieving all the information from the “branches" table and additionally measure the approximate distance from the zip code provided by the user to our actual company branches. In line 56 we define private method populate results, which will be responsible for creating an array of the results retrieved from the database. As you can see, if our query returns zero rows we define an error, if our query returns at least one row, we create an array with all the information about the company. The last public method in line 75, findBranches() will be the one, which we will call after instantiating our class and translating zip code into geographical length.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getResults<span style="color: #009900;">&#40;</span><span style="color: #000088;">$latlng</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT *, (POW((69.1*(longitude-<span style="color: #006699; font-weight: bold;">{$latlng[1]}</span>)*COS(<span style="color: #006699; font-weight: bold;">{$latlng[0]}</span>/57.3)), '2') + POW((69.1*(latitude-<span style="color: #006699; font-weight: bold;">{$latlng[0]}</span>)), '2')) AS distance FROM branches HAVING distance &lt; <span style="color: #006699; font-weight: bold;">{$this-&gt;radius}</span> ORDER BY distance ASC;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> populateResults<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">mysql_num_rows</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data_set</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'none'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data_set</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'name'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'zip'</span>       <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'zip'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'city'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'city'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'latitude'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'latitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'longitude'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'longitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'distance'</span>  <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'distance'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> findBranches<span style="color: #009900;">&#40;</span><span style="color: #000088;">$latlng</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data_set</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getResults</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$latlng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">populateResults</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Our class for retrieving results is done. For the simplicity I did not assume the case, when the database query returns zero rows, in which radius should be automatically extended. You can easily do this, by writing one more method, which will be responsible for extending the radius and adding a simple if...else check in our findBranches() method, to make it loop until your query returns any results. As I said before though, in a database with many entries, executing a web query with a formula complicated like the one we are using, retrieving results might consume a lot of time, and creating a loop, in which you will repeat the same query might lead to a really long time a user will have to wait for a response. In my case, the database was a little bit over 1000 entries and extending the radius and repeating the query did not result in any remarkable performance lost though.</p>
<p>The last part is the actual usage of the class we just wrote. Basically in line 84 we instantiate our class, in line 85 we translate the zip code to geographical length and according to the value returned by this method we use json_encode() function to return the results as a json representation of the array we just created or an error.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>84
85
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$branches</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Branches<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$latlng</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$branches</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">translateZipToLatLng</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$branches</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">findBranches</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$latlng</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'error'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'zip_invalid'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Since our class for retrieving the results in a certain radius is ready, we should move to our client side code.</p>
<p>To make this simple, I will just show you the whole jQuery code and discuss it line by line.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #003366; font-weight: bold;">var</span> mapContainer <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#map'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>GBrowserIsCompatible<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLargeMapControl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">52.519015140666</span><span style="color: #339933;">,</span><span style="color: #CC0000;">13.419671058654785</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#submit'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> zip <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#zip'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> radius <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#radius'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>zip <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;You need to provide a zip code&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #003366; font-weight: bold;">var</span> rand <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'zip.php?rand='</span> <span style="color: #339933;">+</span> rand<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> zip<span style="color: #339933;">:</span> zip<span style="color: #339933;">,</span> radius<span style="color: #339933;">:</span> radius <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> markers <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> descriptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> bounds <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLngBounds<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          map.<span style="color: #660066;">clearOverlays</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#error'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#locations'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#closest_location'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> flag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
          $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">error</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'none'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'No results found or ZIP incorrect'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#error'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'No results found or ZIP incorrect'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
              flag <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span>
              <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #003366; font-weight: bold;">var</span> description <span style="color: #339933;">=</span>
              <span style="color: #3366CC;">'&lt;div class=&quot;branch&quot; style=&quot;float: left;&quot;&gt;'</span> <span style="color: #339933;">+</span>
              <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' (~ '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">distance</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' km)&lt;br /&gt;'</span> <span style="color: #339933;">+</span>
              <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">zip</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' '</span> <span style="color: #339933;">+</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">city</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;br /&gt;'</span> <span style="color: #339933;">+</span>
              <span style="color: #3366CC;">'&lt;span class=&quot;branch_show&quot; style=&quot;cursor: pointer; color: blue; text-decoration: underline;&quot; id=&quot;'</span> <span style="color: #339933;">+</span> i <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;&gt;Show&lt;/span&gt;&lt;/div&gt;'</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> lat <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">latitude</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> lng <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">longitude</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> point <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>lat<span style="color: #339933;">,</span> lng<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            markers<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> marker<span style="color: #339933;">;</span>
            descriptions<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> description<span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#locations'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>description<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>marker<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            bounds.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              marker.<span style="color: #660066;">openInfoWindowHtml</span><span style="color: #009900;">&#40;</span>description<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#closest_location'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>descriptions<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          map.<span style="color: #660066;">setZoom</span><span style="color: #009900;">&#40;</span>map.<span style="color: #660066;">getBoundsZoomLevel</span><span style="color: #009900;">&#40;</span>bounds<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #006600; font-style: italic;">// just in case we did not find anything</span>
          <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>flag<span style="color: #009900;">&#41;</span>
            map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">51.08282186160976</span><span style="color: #339933;">,</span><span style="color: #CC0000;">10.26123046875</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">6</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">else</span>
            map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>bounds.<span style="color: #660066;">getCenter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.branch_show'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            markers<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">openInfoWindowHtml</span><span style="color: #009900;">&#40;</span>descriptions<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>As usually we want to execute the code on onLoad of our document and we do that in line 1. In line 2 we define our container for the map and in line 3 and 4 we check if browser is compatible and if container was found respectively.</p>
<p>In line 5 we instantiate the Gmap2 inside of our map container, we add some controls in line 6 and set the center in line 7. In line 9 we add a event listener which triggers after a user clicks submit button.</p>
<p>In lines 10 to 15 we add a simple check, if the field with the zip code is not empty and it if is we alert an according message and we break the execution of the code. Additionally we define a zip variable in line 10 and radius variable in line 11, which will hold according information to use in our json request.</p>
<p>In line 16 we define rand variable which we pass during our json request to prevent internet explorer to cache the resource. If we would not do that, and changes you would do to zip.php file would not be visible, because IE would use the cached version of this file.</p>
<p>In line 17 we make a request to the zip.php file, which we wrote earlier and we pass the zip code and the radius to our php file. After the calculation is done and you do not have any errors in your php file  we retrieve the information, which we can finally process.</p>
<p>In line 18 and 19 we define two variables as arrays, which will be responsible for holding the information about markers and their corresponding descriptions and in line 20 we instantiate GLatLngBounds object, which we will later use to display the information on our map with matching zoom level.</p>
<p>In line 22 to 26 we remove all markers and we empty all containers holding the information about the places from a previous request. We do that to prevent displaying of the same markers after a user requests the zip.php file again. This way, if a user changes his or hers query, we will be sure that we are displaying only the information relevant to the current request and we wont have any trash from the previous requests. In line 26 we define a flag as boolean to use it later to display error information and redisplay the center of the map.</p>
<p>In line 28 we loop through the information we received in response. If an error was found, we break our loop and we display and alert according information to the user. We also set the flag to false to redisplay center of the map later in the code. This would be lines 29 to 34.</p>
<p>In lines 35 to 39 we put together the HTML code which we will use to display the information about the branches we are going to display on the map.</p>
<p>In lines 40 to 43 we define two variables lat and lng and use them to instantiate GLatLng object which we define as point variable. In line 44 we define marker variable as an instance of GMarker object and in lines 45 and 46 we add it to the arrays we defined earlier. In line 47 we append the description to the container we need to define in our HTML and in line 47 we add markers to the map. In line 48 we extend the bounds of our map by using a method extend() of GLatLngBounds object we defined earlier. This amazing functionality of google maps will let us display a square with all our markers and google map will take care of setting the correct zoom level.</p>
<p>In lines 49 to 51 we add a listener which will be triggered on a click event and will display the description in an info cloud. In line 52 we close the loop and in line 54 we append the first description to the closest_location container. To assure that the first description is really the closest place to the zip code provided by the user, you cannot forget about putting the “ORDER BY distance ASC” in the php code of zip.php file.</p>
<p>In line 56 we set the zoom of our map using our bound object I was mentioning before by utilizing getBoundsZoomLevel method of GMap. As you can see in the description in line 57, if our php code did not return any results, we override the zoom level we defined in line 56 be setting it again to the default value (line 59). On the other hand, if our php code returned any results we set the center of the map using our bounds object again (line 61).</p>
<p>The last part of this code is just a listener which is triggered when a user clicks on the pseudo link show. We retrieve the id of this link and display the according info window on the map.</p>
<p>That would be it. The code is done. I know it is not perfect and accurate either. You can still use it in many different ways. It does not necessarily need to be a zip code search. Using it you can display distances between users and any other stuff connected with distances. In the second part I will show you how you can make this code a little bit more accurate using google maps build in api to show the directions, how to get from the zip code provided by the user to one of the company branches.</p>
<p>If you do not want to copy all the code, here is a <a href="http://www.designing4u.de/examples/calculating-distance/code.zip">zip package</a> for you with working files.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2009/05/zip-code-search-using-jquery-and-google-maps-displaying-results-in-a-defined-radius/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Maps and jQuery &#8211; loading markers when user moves the map</title>
		<link>http://www.designing4u.de/2009/02/google-maps-and-jquery-loading-markers-when-user-moves-the-map/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=google-maps-and-jquery-loading-markers-when-user-moves-the-map</link>
		<comments>http://www.designing4u.de/2009/02/google-maps-and-jquery-loading-markers-when-user-moves-the-map/#comments</comments>
		<pubDate>Thu, 26 Feb 2009 15:28:56 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[add marker]]></category>
		<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=31</guid>
		<description><![CDATA[Recently I was working on a project, in which the goal was to display all company branches within a certain distance, defined by a zip code and a range provided by user input. The solution was pretty simple. A user provides a zip code and the distance range, the script translates the zip code into [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was working on a project, in which the goal was to display all company branches within a certain distance, defined by a zip code and a range provided by user input. The solution was pretty simple. A user provides a zip code and the distance range, the script translates the zip code into geographical length and by using the Haversine formula retrieves all branches from the database and displays them in a Google map. This script was only working when a user submitted the information through a form that was provided for this script. As soon as he or she started to play with the Google map, the information about company's branches outside the initial zip code entered were not updated. When one assumes that a company has for instance 1000 Branches and resides in a small country, displaying other branches, while a user is moving within the Google map may be crucial to their search. I had to come up with an idea, how to solve this problem in my jQuery code. This is my solution.<br />
<span id="more-31"></span><br />
First of all, I had to decide how to handle this event. One possibility was to write my own event listener and trigger it using GEvent.trigger() method. One of the pre-build events provided "out of the box" by Google Map API is "moveend". Using this event, we can send an AJAX request every time a user moves the map, either when he or she drags and drops it or he or she is using provided controls. This approach has definite disadvantages because it might be possible that a user will play with a map a lot and this will lead to several AJAX requests every time he or she does that. On the other hand, you also need to assume that since a user is using the form you provided him or her in the first place, this situation probably won't be that common.</p>
<p>One thing we need to make a decision about before we will start coding is when we should use the Haversine formula and when we should display the markers according to the borders of the map. According to <a href="http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL" target="_blank">this study</a>, using the Haversine formula in a web query on a MySQL database with 800k entries might take up to 8 seconds. Therefore it will be crucial to split our jQuery code into requests concerning retrieving the data based on the Haversine formula and data within the borders of the map. In this example I won't give a complete walk through of how to measure the distance between two points (it will probably be a good subject for another example). In this example I will only show how to display data on an "moveend" event.</p>
<p>Let's take a look at the code. In line 1, we are executing the code after the document is loaded. I would consider it a standard procedure in writing unobtrusive JavaScript. In line 2, we define our map container, which corresponds with a HTML tag whose id equals map. In line 3, we are checking if the user’s browser is compatible with Google Map API and if it is, and our map container also exists, we can finally execute the rest of the code.</p>
<p>In line 5, we are instantiating our Google map and in lines 6 and 7 we are defining some controls for our map, the center (GLatLng) and zoom level (14). In line 9, we would have our code to measure the distance from given zip code and to display the markers on our map. The most interesting part of our code starts in line 14. We add a listener, which triggers on "moveend" event. In line 15, we use the getBounds() method, which returns GLatLngBounds object for us. In lines 16 to 19, we use getSouthWest() and getNorthEast() methods of our GLatLngBounds objects. This methods return GLatLng object, therefore we can directly use its methods to retrieve the geographical lengths of our south west and northeast corner. The last part is pretty simple, and I will leave it to you to adjust it to your needs. In line 20, we use the AJAX request, and we pass all geographical lengths to our php file. We assume that the response will be a valid json_encode() representation of the data retrieved from the database, and in lines 21 to 23, we add our markers to the map. </p>
<p>Here is the code you have been waiting for:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #003366; font-weight: bold;">var</span> mapContainer <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#map'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>GBrowserIsCompatible<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLargeMapControl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">52.519015140666</span><span style="color: #339933;">,</span><span style="color: #CC0000;">13.419671058654785</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#submit'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// here goes the code handling calculatig of distance</span>
        <span style="color: #006600; font-style: italic;">// truncated because it is not a part of this post</span>
&nbsp;
        GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;moveend&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> bounds <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getBounds</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> swLat <span style="color: #339933;">=</span> bounds.<span style="color: #660066;">getSouthWest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">lat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> swLng <span style="color: #339933;">=</span> bounds.<span style="color: #660066;">getSouthWest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">lng</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> neLat <span style="color: #339933;">=</span> bounds.<span style="color: #660066;">getNorthEast</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">lat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> neLng <span style="color: #339933;">=</span> bounds.<span style="color: #660066;">getNorthEast</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">lng</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'helper.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> swLat<span style="color: #339933;">:</span> swLat<span style="color: #339933;">,</span> swLng<span style="color: #339933;">:</span> swLng<span style="color: #339933;">,</span> neLat<span style="color: #339933;">:</span> neLat<span style="color: #339933;">,</span> neLng<span style="color: #339933;">:</span> neLng <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> point <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">lat</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">lng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>marker<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You might ask why we are placing this code inside of jQuery's click event? The answer is simple. This way we can prevent triggering the "moveend" event, before a user uses the form. Only after he or she uses the form, our listener will react to the user's interaction with the map.</p>
<p>Another question might be, why we are not using GBounds.extend() method to adjust the zoom level? An answer for this question is as simple as the last one. We already defined the zoom level by passing the geographical lengths of our rectangle to the php file and since we triggered "moveend" event, we always have an actual part of the map, which a user is seeing at the point of triggering it.</p>
<p>I hope you will like this example. I did not want to provide the whole code for this example, because first of all I wanted to split it into two entries and second of all, I did not want to provide an example which you can copy and past, but rather give a solution for a certain problem and possibility start a discussion about doing it better. Have fun with this one.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2009/02/google-maps-and-jquery-loading-markers-when-user-moves-the-map/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Google Maps and jQuery &#8211; calculating distance between two points</title>
		<link>http://www.designing4u.de/2008/11/google-maps-and-jquery-calculating-distance-between-two-points/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=google-maps-and-jquery-calculating-distance-between-two-points</link>
		<comments>http://www.designing4u.de/2008/11/google-maps-and-jquery-calculating-distance-between-two-points/#comments</comments>
		<pubDate>Tue, 11 Nov 2008 10:39:43 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[KML]]></category>
		<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[drop down]]></category>
		<category><![CDATA[Geolocation]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=28</guid>
		<description><![CDATA[It has been a long time since I wrote my last example, but I've been really busy. Lately I got a request to write an example about calculating the distance between two points using KML file. What I will show you in this example is how to provide an user two drop down fields, which [...]]]></description>
			<content:encoded><![CDATA[<p>It has been a long time since I wrote my last example, but I've been really busy. Lately I got a request to write an example about calculating the distance between two points using KML file. What I will show you in this example is how to provide an user two drop down fields, which will be build according to provided KML file. Choosing each field will show a marker on the map. Changing the drop down field will cause removal of an according marker and placement of it in a new location. Also the markers will have two different colors, so an user can differentiate between starting and ending point. As the last functionality we will add a button. After an user clicks on the button, our function will calculate the rout and display the distance in kilometers. Let's start with a simple class which we will use to display drop down field and retrieve the coordination of requested place.<br />
<span id="more-28"></span></p>
<p>To see, how our final solution will look like, go <a href="http://www.designing4u.de/examples/geo-directions/index.php" title="Google Maps and jQuery - calculating distance between two points" target="_blank">here</a></p>
<h3>1. Geo Class - Geo.class.php</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Geo
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'geo.xml'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'SimpleXMLElement'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getDropDown<span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$option</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$select</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'&lt;select name=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot; id=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$id</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$option</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'include_custom'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> <span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$select</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;option value=&quot;&quot; selected=&quot;selected&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$option</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'include_custom'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/option&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$child</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$select</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;option value=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/option&gt;'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$select</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;/select&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$select</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getLocation<span style="color: #009900;">&#40;</span><span style="color: #000088;">$identifier</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$coordinates</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">children</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$child</span><span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">name</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$identifier</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$temp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span> <span style="color: #000088;">$child</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Point</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">coordinates</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$coordinates</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                    <span style="color: #0000ff;">'lat'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                                    <span style="color: #0000ff;">'lng'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$temp</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$coordinates</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>It wasn't really necessary to put this functions into a class, but it is just a habit <img src='http://www.designing4u.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  We define a protected function _init(), which we call every time we want to use our KML file to retrieve information out of it. The function simplexml_load_file initializes an SimpleXMLElement object for us, which we can use later. The function getDropDown with the first mandatory argument and the second optional will be responsible for displaying the drop down field for us. I think I don't need to explain it in detail. The last function getLocation will retrieve the latitude and longitude from KML file. So let's move to index.php file, which will be the core of this example.</p>
<h3>2. Core file - index.php</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Geo.class.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$geo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Geo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Before we send any headers we include our Geo.class.php file and initialize our Geo class. After that goes the usual <html> tag <meta> tags and the google maps and jQuery scripts. In the <head> section of our document goes the actual jQuery part.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  
  <span style="color: #003366; font-weight: bold;">var</span> mapContainer <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#map'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> markerStart<span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> markerEnd<span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>GBrowserIsCompatible<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>    
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLargeMapControl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
      map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">52.519015140666</span><span style="color: #339933;">,</span><span style="color: #CC0000;">13.419671058654785</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#start'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> location <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>location <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>
          <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
        $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'helper.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> location<span style="color: #339933;">:</span> location <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>markerStart<span style="color: #009900;">&#41;</span>
            map.<span style="color: #660066;">removeOverlay</span><span style="color: #009900;">&#40;</span>markerStart<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          pointA <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">lat</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">lng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          markerStart <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>pointA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>markerStart<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#end'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> location <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>location <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span>
          <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
&nbsp;
        $.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'helper.php'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> location<span style="color: #339933;">:</span> location <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>markerEnd<span style="color: #009900;">&#41;</span>
            map.<span style="color: #660066;">removeOverlay</span><span style="color: #009900;">&#40;</span>markerEnd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          <span style="color: #003366; font-weight: bold;">var</span> blueIcon <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GIcon<span style="color: #009900;">&#40;</span>G_DEFAULT_ICON<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          blueIcon.<span style="color: #660066;">image</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png&quot;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> markerEndOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> icon<span style="color: #339933;">:</span> blueIcon <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
          pointB <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>data.<span style="color: #660066;">lat</span><span style="color: #339933;">,</span> data.<span style="color: #660066;">lng</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          markerEnd <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>pointB<span style="color: #339933;">,</span> markerEndOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>markerEnd<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#submit'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">pointA</span> <span style="color: #339933;">===</span> undefined <span style="color: #339933;">||</span> window.<span style="color: #660066;">pointB</span> <span style="color: #339933;">===</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;You need to choose start and end&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003366; font-weight: bold;">var</span> directions <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GDirections<span style="color: #009900;">&#40;</span>map<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        route <span style="color: #339933;">=</span> Array<span style="color: #009900;">&#40;</span>pointA<span style="color: #339933;">,</span> pointB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>directions<span style="color: #339933;">,</span> <span style="color: #3366CC;">'load'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> dist <span style="color: #339933;">=</span> directions.<span style="color: #660066;">getDistance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #339933;">;</span>
          $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#distance'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>dist<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        directions.<span style="color: #660066;">loadFromWaypoints</span><span style="color: #009900;">&#40;</span>route<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Maybe it is long but pretty straight forward. As you can read in my other examples, we first initialize jQuery, so it starts after our document is loaded. After that we define our map container and our markers. After we check if our browser is compatible and our map container exists, we initialize the map with couple options (large controls, zoom level = 14 and the center of the map). </p>
<p>We add two event listeners, which are triggered on onchange event of our drop downs (with id start and end respectively, the html code will follow). If you use the $option parameter in getDropDown function for displaying custom blank option, we check if the user chose this option and return false if he did. If he chose a valid option, we use an AJAX request and retrieve the geolocation from our helper file. </p>
<p>After our call returns some data, we check, if we have already any markers saved in our map and if we do, we first remove them and then place a new marker according to the option chosen in the drop down. </p>
<p>We repeat the same for the drop down with id 'end'. Actually we could have put this two events into a separate function, but to make this example clear I purposely didn't do that.</p>
<p>The last event listener is triggered, when a user clicks on a 'calculate route' button. First we check, if both points were placed by the user, and if not we display an alert, that he or she has to provide valid starting and ending point. This check might not work in IE, therefore you might need to do some changes, so it also works in IE.</p>
<p>We initialize GDirections Class and define our route with starting and ending point. I also use the getDistance method to display the amount of kilometers between this two points. As the last thing we need to do is to display the directions on the map and voila, we are done <img src='http://www.designing4u.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>What we still need it the rest of index.php file and our helper, and KML file.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">&lt;div id=&quot;navigation&quot;&gt;
        Start <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$geo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDropDown</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'start'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'include_custom'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'please choose...'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
        Stop <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$geo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDropDown</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'end'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'include_custom'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'please choose...'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>
        &lt;input type=&quot;button&quot; name=&quot;submit&quot; id=&quot;submit&quot; value=&quot;calculate route&quot; /&gt;
&lt;/div&gt;
&lt;div id=&quot;distance&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;map&quot; style=&quot;width: 1000px; height: 600px;&quot;&gt;&lt;/div&gt;</pre></td></tr></table></div>

<p>Pretty straight forward. We display our navigation with an empty option 'please choose...', the 'calculate route' button and containers for distance and map.</p>
<h3>3. Our helper - helper.php</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Geo.class.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$geo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Geo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$geo</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLocation</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'location'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Pretty simple, isn't it <img src='http://www.designing4u.de/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  Just initialize the class and call the method to retrieve location with the location argument used in the _GET request.</p>
<h3>4. KML file - geo.xml</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;kml</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://earth.google.com/kml/2.2&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Alexanderplatz<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Center of Berlin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>52.52238370531909,13.409671783447266<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Frankfurter Tor<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>A nice place to live<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>52.51595970749194,13.4527587890625<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Warschauerstr.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Place to go and spend some time with friends<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>52.508568336719854,13.451085090637207<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Revelerstr.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Cassiopeia Club<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>52.50721006978328,13.457050323486328<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Frankfurter Allee<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Shopping Center<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>52.513818166144006,13.474087715148926<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/coordinates<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Point<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Placemark<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/kml<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>It is a file I prepared by myself. As far as I know, you can create them using the Google Earth application. Probably they can be way more complex than this one. But I guess it will be a nice subject for next example <img src='http://www.designing4u.de/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  Good luck with this one and as usually, in case of any question, you know how to find me. Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/11/google-maps-and-jquery-calculating-distance-between-two-points/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Google Maps, jQuery and XML &#8211; saving markers with user input</title>
		<link>http://www.designing4u.de/2008/08/google-maps-jquery-and-xml-saving-markers-with-user-input/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=google-maps-jquery-and-xml-saving-markers-with-user-input</link>
		<comments>http://www.designing4u.de/2008/08/google-maps-jquery-and-xml-saving-markers-with-user-input/#comments</comments>
		<pubDate>Mon, 04 Aug 2008 08:59:54 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[saving markers]]></category>
		<category><![CDATA[user input]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=27</guid>
		<description><![CDATA[I just got a first request from a user of my site to help him with his problem. Let me describe it in detail. Assume that a user is visiting your site and he or she wants to place a marker on a google map displayed on your page. Additionally, we want to get some [...]]]></description>
			<content:encoded><![CDATA[<p>I just got a first request from a user of my site to help him with his problem. Let me describe it in detail. Assume that a user is visiting your site and he or she wants to place a marker on a google map displayed on your page. Additionally, we want to get some input from our user, therefore after he or she clicks on the google map we will provide an info could with a form. Our user will be able to provide a name, a message and a link and submit it. I didn't want to make this example too complicated, therefore handling the links or uploading the files you will have to solve by yourself. Let's move to the code.<br />
<span id="more-27"></span><br />
Our final solution will look more or less like <a title="Google Maps, jQuery and XML - saving markers and user input" href="http://www.designing4u.de/examples/marker-gmap/index.php" target="_blank">this</a>.</p>
<p>In our example we will use two file types - PHP and XML. Our PHP file will be responsible for saving the user input in XML file and displaying the google map with all saved markers. Our XML file will be basically a place, where we will save all the markers saved by an user. It is worth to mention that you can easily replace the XML file with any kind of a database. If you do so, you will for example have possibility to approve each marker saved on your page. Additionally, if you have some kind of authentication system on your site, you can also save according username of a person who placed a marker on your site and provide some kind of statistics for your users. It's up to you, how you will use this code.</p>
<p>After some initial explanation we can move to our PHP file. Let's start with the header of our file:</p>
<p>1. PHP file</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'submit'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$fh</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'markers.xml'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  try <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$e</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000088;">$marker</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addChild</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'marker'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lat'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lat'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lng'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lng'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'msg'</span><span style="color: #339933;">,</span> <span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'msg'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span><span style="color: #339933;">,</span> <span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'link'</span><span style="color: #339933;">,</span> <span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'markers.xml'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">saveXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location:index.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We use PHP function file_get_contents() to load the content of our XML file. Our XML file would be an empty XML file with markers element, which will be a holder for our markers. We initialize a PHP build in class SimpleXMLElement with our file and catch an exception if any error occur. If our user clicks on a save button (the code of this form will follow) we create a child element with according attributes and then save it as a new XML file.</p>
<p>This way we will add new markers to our XML file without loosing any previous information saved in our XML file. After our file is saved we redirect our user to the index page to prevent reloading of the page and saving multiple markers with the same information. I know it is not the cleanest way to do that, but it is just an example which should serve as a blueprint for your code.</p>
<p>You can notice that I use utf8_encode() function to encode our user input into correct charset. Because I encountered major problems with foreign characters and our final solution should work for standard characters as well as for special characters, that is a necessary step to handle this problem. The only thing you should keep in mind is to set the charset of your site to UTF-8.</p>
<p>Don't forget to put that code before the head section of your PHP file, because otherwise you will get an error that the header were already send.</p>
<p>Let's take a look at our jQuery function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> mark<span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> pointA<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>GBrowserIsCompatible<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> m <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#map&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> start <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">63.13450320833446</span><span style="color: #339933;">,</span><span style="color: #CC0000;">16.69921875</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #003366; font-weight: bold;">var</span> zoomLevel <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>start<span style="color: #339933;">,</span> zoomLevel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GSmallMapControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'markers.xml'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'marker'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> lat    <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> lng    <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lng'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> html   <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">;</span>
          html      <span style="color: #339933;">+=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'msg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; &quot;</span><span style="color: #339933;">;</span>
          html      <span style="color: #339933;">+=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'link'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> point  <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>lat<span style="color: #339933;">,</span>lng<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>marker<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
          GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            marker.<span style="color: #660066;">openInfoWindowHtml</span><span style="color: #009900;">&#40;</span>html<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>overlay<span style="color: #339933;">,</span> point<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          map.<span style="color: #660066;">removeOverlay</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          pointA <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GPoint<span style="color: #009900;">&#40;</span>point.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> point.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          mark <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>pointA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          map.<span style="color: #660066;">getCenter</span><span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> lat <span style="color: #339933;">=</span> point.<span style="color: #660066;">y</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> lng <span style="color: #339933;">=</span> point.<span style="color: #660066;">x</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> form <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;
          &lt;form action=&quot;</span>\<span style="color: #3366CC;">&quot;index.php<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> method<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>post<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #3366CC;">&quot; +
          &quot;</span><span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>lat<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>hidden<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">+</span>lat<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span><span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>lng<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>hidden<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">+</span>lng<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span><span style="color: #000066;">Name</span><span style="color: #339933;">:</span>
          <span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>name<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>text<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span>Msg<span style="color: #339933;">:</span>
          <span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>msg<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>text<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span>Link<span style="color: #339933;">:</span>
          <span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>link<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>text<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span><span style="color: #339933;">&lt;</span>input <span style="color: #000066;">name</span><span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>submit<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>submit<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> value<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>save<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">/&gt;</span><span style="color: #3366CC;">&quot;+
          &quot;</span><span style="color: #339933;">&lt;/</span>form<span style="color: #339933;">&gt;</span><span style="color: #3366CC;">&quot;;
          map.openInfoWindowHtml(point,form);
        }
      });
    }
  }
});</span></pre></td></tr></table></div>

<p>In the first part we declare two variables pointA and mark which will later serve to retrieve the longitude and latitude of our marker. We will also reduce the ability of a user to place several markers at the same time.</p>
<p>We check if our browser is compatible with google maps and we look for a place holder for our map. If both of the conditions are fulfilled we display our map and use AJAX request to display all the markers saved in our XML file. We also add an event listener which is triggered every time a user clicks on an existing marker. If it is a case, we display an info cloud with information saved by the user.</p>
<p>In the second part of our function we add another event listener, which is triggered every time a user places a new marker. In our info cloud we will display a form which I mentioned before and save the longitude and latitude in hidden fields. After our user clicks a save button the information from the form will be processed by our PHP file which will create another marker and display it on our google map.</p>
<p>The last part is our XML file:</p>
<p>2. XML file:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;markers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;marker</span> <span style="color: #000066;">lat</span>=<span style="color: #ff0000;">&quot;64.4348920430406&quot;</span> <span style="color: #000066;">lng</span>=<span style="color: #ff0000;">&quot;15.205078125&quot;</span> <span style="color: #000066;">msg</span>=<span style="color: #ff0000;">&quot;Msg&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Name&quot;</span> <span style="color: #000066;">link</span>=<span style="color: #ff0000;">&quot;yes!&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/markers<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<p>As I said before this example is just a blueprint and should be extended by user input validation. As I also said before, you don't really need to use XML file and you can also implement an approval system of each marker placed on your site. You will have to save your information in a database though. You can use my post about <a title="google maps jquery and xml displaying a simple navigation" href="http://www.designing4u.de/2008/05/google-maps-jquery-and-xml-displaying-a-simple-navigation/">displaying a simple navigation</a> to get the idea, how to create an XML file based on information you retrieve from database. I hope this will be helpful. I'm waiting for your feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/08/google-maps-jquery-and-xml-saving-markers-with-user-input/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Google maps, jQuery and XML &#8211; displaying a simple navigation</title>
		<link>http://www.designing4u.de/2008/05/google-maps-jquery-and-xml-displaying-a-simple-navigation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=google-maps-jquery-and-xml-displaying-a-simple-navigation</link>
		<comments>http://www.designing4u.de/2008/05/google-maps-jquery-and-xml-displaying-a-simple-navigation/#comments</comments>
		<pubDate>Thu, 22 May 2008 09:04:26 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[XML]]></category>
		<category><![CDATA[creating XML document]]></category>
		<category><![CDATA[navigation]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=18</guid>
		<description><![CDATA[After I wrote my last example yesterday, I just realized, how easy you can add a simple navigation to your map, whereas after you click on the link an information window will be provided right above the according marker. I think it's a nice way to provide your visitors a possibility to choose what they [...]]]></description>
			<content:encoded><![CDATA[<p>After I wrote my last example yesterday, I just realized, how easy you can add a simple navigation to your map, whereas after you click on the link an information window will be provided right above the according marker. I think it's a nice way to provide your visitors a possibility to choose what they want to see instead of randomly clicking on markers to find out what are they displaying. This time I will also show, how you can easily create an XML document using DomDocument class, which should be installed by default in your PHP5 version, which we will later use to display the markers.<br />
<span id="more-18"></span></p>
<p>If you want to see a working demo of this example just click <a href="http://www.designing4u.de/examples/gmap-navi/" title="google map with navigation" target="_blank">here</a>.</p>
<p>Detailed description of jQuery code you can find in my previous post about <a href="http://www.designing4u.de/2008/05/setting-boundaries-for-google-map-with-more-markers-using-jquery/" title="setting boundaries for google map with more markers using jquery">setting boundaries for google map with more markers using jquery</a>, therefore in this example I will only cover the parts of the code, which weren't explained in previous post. Let's take a closer look at the code, which will generate an XML document. You need to place this code in a separate file, which in our case will be xml.php.</p>
<p>You can create your XML document on many different ways. I will show you here an easy and a nice way, how to do that. Let's start with the nice way, which uses DomDocument class.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$db_host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db_user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db_pass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;pass&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$db_name</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;dbname&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$connect</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$db_pass</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No connection to DB&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$db_name</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;No connection to DB&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$dom</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DomDocument<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'1.0'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'UTF-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$element</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'markers'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendChild</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM google_address&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$marker</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'marker'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">appendChild</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$marker</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lat'</span><span style="color: #339933;">,</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;lat&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'lng'</span><span style="color: #339933;">,</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;lng&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'link'</span><span style="color: #339933;">,</span><span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;link&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$marker</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'desc'</span><span style="color: #339933;">,</span><span style="color: #990000;">utf8_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;desc&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dom</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">saveXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>All the information we are using in this example is stored in MySQL database, therefore we will start with connecting to MySQL database. You will need to create a table with at least four fields (lat, lng, link and desc). The first two fields represent geographical lengths, the third one represents the link name, which will be later displayed in the navigation and the fourth field represents description, which will be later displayed in our info window. </p>
<p>After that we create an instance of our DomDocument class and initialize it with the XML version and set the default encoding (In our case version 1.0 with encoding set to UTF-8). After that we create an element called markers, which will be a root element for our markers and insert it as a child into our XML document. </p>
<p>After that we select all element from our MySQL table and loop through them. In our loop we first create a new element called marker, append it as a child into our root element and set the necessary attributes. After our loop is done with all the results we just echo our XML document using saveXML method. This example only shows, how to display all results from database. In your AJAX request you can pass some attributes to filter the data.</p>
<p>The easier method is based on creating a file called coordinates.xml. You will need to place this code before you do your AJAX request, because otherwise you will get 404 error.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">$db_host = &quot;localhost&quot;;
$db_user = &quot;user&quot;;
$db_pass = &quot;pass&quot;;
$db_name = &quot;dbname&quot;;
&nbsp;
$connect=mysql_connect($db_host, $db_user, $db_pass) or die (&quot;No connection to DB&quot;);
mysql_select_db($db_name) or die(&quot;No connection to DB&quot;);
&nbsp;
$sql = mysql_query(&quot;SELECT * FROM google_address&quot;);
&nbsp;
$text = &quot;<span style="color: #000000; font-weight: bold;">&lt;?</span>xml version<span style="color: #339933;">=</span>\<span style="color: #0000ff;">&quot;1.0<span style="color: #000099; font-weight: bold;">\&quot;</span> encoding=<span style="color: #000099; font-weight: bold;">\&quot;</span>UTF-8<span style="color: #000099; font-weight: bold;">\&quot;</span>?&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$text</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;markers&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$text</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;marker lat=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;lat&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> lng=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;lon&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> html=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;description&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>  label=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;street&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span> /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$text</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&lt;/markers&gt;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;coordinates.xml&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fh</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;w&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fputs</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fh</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This way you just create a string, which you put in an XML file. I don't think that this method needs a further explanation. It's disadvantage is that you cannot really pass any attributes to filter the data. </p>
<p>And finally, what you've been waiting for, our jQuery code for this example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #003366; font-weight: bold;">var</span> mapContainer <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#map&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> bounds <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLngBounds<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> markers <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> descs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> navi <span style="color: #339933;">=</span><span style="color: #3366CC;">''</span><span style="color: #339933;">;</span>
  <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>GBrowserIsCompatible<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>mapContainer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLargeMapControl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GMapTypeControl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GScaleControl<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    $.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'xml.php'</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      $<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'marker'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> lat    <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lat'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> lng    <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'lng'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> link   <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'link'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> desc   <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'desc'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> point  <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>lat<span style="color: #339933;">,</span>lng<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        markers<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> marker<span style="color: #339933;">;</span>
        descs<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> desc<span style="color: #339933;">;</span>
        navi <span style="color: #339933;">+=</span> <span style="color: #3366CC;">'&lt;div class=&quot;click&quot; id=&quot;'</span><span style="color: #339933;">+</span>i<span style="color: #339933;">+</span><span style="color: #3366CC;">'&quot;&gt;'</span><span style="color: #339933;">+</span>link<span style="color: #339933;">+</span><span style="color: #3366CC;">'&lt;/div&gt;'</span><span style="color: #339933;">;</span>
        map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>marker<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        bounds.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>marker<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          marker.<span style="color: #660066;">openInfoWindowHtml</span><span style="color: #009900;">&#40;</span>desc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        i<span style="color: #339933;">++;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">setZoom</span><span style="color: #009900;">&#40;</span>map.<span style="color: #660066;">getBoundsZoomLevel</span><span style="color: #009900;">&#40;</span>bounds<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>bounds.<span style="color: #660066;">getCenter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#navi'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>navi<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.click'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> id <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        markers<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">openInfoWindowHtml</span><span style="color: #009900;">&#40;</span>descs<span style="color: #009900;">&#91;</span>id<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
  <span style="color: #009900;">&#125;</span>		
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The only difference between this and my previous code is that we save all markers and descriptions in an array and create a string, which we use to display our navigation. After our loop is done with the markers provided by xml.php file, it displays the navigation. We also add an event listener, which is triggered, after our visitor clicks on the navigation link. When the even is triggered an information window will be displayed above according marker. </p>
<p>The only thing, which you need to make this code work is the HTML code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;map&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:1000px; height:650px;float:left;&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;navi&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></pre></td></tr></table></div>

<p>That's all. I hope this code will help you to make your site more interesting for your visitors. Have fun with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/google-maps-jquery-and-xml-displaying-a-simple-navigation/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Setting boundaries for google map with more markers using jQuery</title>
		<link>http://www.designing4u.de/2008/05/setting-boundaries-for-google-map-with-more-markers-using-jquery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=setting-boundaries-for-google-map-with-more-markers-using-jquery</link>
		<comments>http://www.designing4u.de/2008/05/setting-boundaries-for-google-map-with-more-markers-using-jquery/#comments</comments>
		<pubDate>Wed, 21 May 2008 16:13:24 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[automatic zoom level]]></category>
		<category><![CDATA[boundaries]]></category>
		<category><![CDATA[GLatLngBounds]]></category>
		<category><![CDATA[GMap2]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=17</guid>
		<description><![CDATA[For a project I'm currently working on I had to display more markers in one google map. No problem at all, but only if all of them are placed in one city. You can just set up default level for the city and display as many markers as you want. In my case I had [...]]]></description>
			<content:encoded><![CDATA[<p>For a project I'm currently working on I had to display more markers in one google map. No problem at all, but only if all of them are placed in one city. You can just set up default level for the city and display as many markers as you want. In my case I had to display latest markers, which were saved all over the country. One would say no problem at all, just use default zoom level for the country and display the markers. What if 10 latest markers were placed in one city though? It would look stupid if they would be displayed covering each other. Additionally we do not want to use an onload event, therefore, we will use jQuery in this example. For this kind of problem google came up with a pretty nice solution.<br />
<span id="more-17"></span><br />
if you want to see a working demo of this example, just click <a href="http://www.designing4u.de/examples/gmap/index.php" target="_blank">here</a>.</p>
<p>One of the classes available in second version of google maps is GLatLngBounds. Instantiating this class constructs a rectangle in geographical lengths. One of the methods, called 'extend' allows us to extend this rectangle to get a perfect zoom level. It means that every time our loop will display a new marker, we will call 'extend' method, which will additionally extend the boundaries of the rectangle, so it contains that given point. An additional functionality, which will be covered by this function is an event listener, which will display info window when a user clicks on one of the markers. At the end we will set the zoom level using one of the methods (getBoundsZoomLevel()) from another class, which can be called though, without instantiating it and use the getCenter() method from GLatLngBounds to get the center of our map. </p>
<p>To save us some time, I decided to skip the part where we are creating our XML document, which contains all necessary information (latitude, longitude and text for info window) to display our markers, therefore if you need that part to be covered as well, just let me know. Let's take a closer look at our code:<br />
[source:javascript]<br />
$(document).ready(function() {<br />
	//our map container and instance of GLatLngBounds<br />
	var mapContainer = $("#map")[0];<br />
	var bounds = new GLatLngBounds();</p>
<p>	//if browser is compatible<br />
	if (GBrowserIsCompatible()) {<br />
		//if div with given id was found<br />
		if(mapContainer) {<br />
			//instance of the GMap2 and some controls<br />
			var map = new GMap2(mapContainer);<br />
			map.addControl(new GLargeMapControl);<br />
			map.addControl(new GMapTypeControl);<br />
			map.addControl(new GScaleControl);<br />
			map.setCenter(new GLatLng(0,0),0);<br />
		}<br />
		//AJAX request<br />
		$.get('coordinates.xml',function(data) {<br />
			//we are looking for all marker and looping through them<br />
			$(data).find('marker').each(function(){<br />
				//serializing some data from the request<br />
				var lat    = $(this).attr('lat');<br />
				var lng    = $(this).attr('lng');<br />
				var desc   = $(this).attr('desc');<br />
				var point  = new GLatLng(lat,lng);<br />
				var marker = new GMarker(point);</p>
<p>				//new marker<br />
				map.addOverlay(marker);<br />
				//extending the boundaries<br />
				bounds.extend(point);<br />
				//and the info window<br />
				GEvent.addListener(marker, "click", function() {<br />
					marker.openInfoWindowHtml(desc);<br />
				});</p>
<p>			});<br />
			//setting the zoom level and the center<br />
			map.setZoom(map.getBoundsZoomLevel(bounds));<br />
			map.setCenter(bounds.getCenter());<br />
		});</p>
<p>	}</p>
<p>});<br />
[/source]<br />
I hope everything is clear. In case of any question just use the comment or contact form. One more thing left to show. Here is the content of xml file:<br />
[source:xml]<br />
<?xml version="1.0" encoding="iso-8859-1"?><br />
<markers><br />
	<marker lat="52.491783" lng="13.420009" desc="Pretty street" /><br />
	<marker lat="52.496375" lng="13.442661" desc="Good Food" /><br />
	<marker lat="52.514122" lng="13.448429" desc="Nice Bar" /><br />
	<marker lat="52.437259" lng="13.328170" desc="Stadion" /><br />
</markers><br />
[/source]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/setting-boundaries-for-google-map-with-more-markers-using-jquery/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Geolocation class &#8211; retrieving longitude and latitude using google maps</title>
		<link>http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=geolocation-class-retrieving-longitude-and-latitude-using-google-maps</link>
		<comments>http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/#comments</comments>
		<pubDate>Tue, 06 May 2008 10:58:43 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[geocoding]]></category>
		<category><![CDATA[Geolocation]]></category>
		<category><![CDATA[latitude]]></category>
		<category><![CDATA[longitude]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=7</guid>
		<description><![CDATA[A while ago I had to prepare a map for a customer. He wanted to be able to use text files with addresses and display them on the map. To be able to do that I had to find a service, which will convert a regular address into geographical longitude and latitude. What would be [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I had to prepare a map for a customer. He wanted to be able to use text files with addresses and display them on the map. To be able to do that I had to find a service, which will convert a regular address into geographical longitude and latitude. What would be better than google maps to do that really simple task? I researched a little bit on google and found a really nice way, how to accomplish that.<br />
<span id="more-7"></span><br />
On Tim Shower's homepage I have found a really nice <a href="http://www.timshowers.com/2008/08/php-geocoding-tutorial-with-the-google-maps-api-part-one/" target="_blank">php geocoding tutorial</a>, which I used to build my solution. For our purposes we will use client for URL (cURL) to retrieve the longitude and latitude of our places. Let's define our class first.<br />
[source:php]<br />
class Geolocation {</p>
<p>	private $url = array();</p>
<p>}<br />
[/source]<br />
We define private array url, in which we will store URLs prepared for cURL request. Let's build a contructor.<br />
[source:php]<br />
class Geolocation {</p>
<p>	private $url = array();</p>
<p>	function __construct($file) {<br />
		$gmapkey = "YOURKEY";<br />
		$address = file($file);<br />
		foreach($address as $addres) {<br />
			$this->url[] = "http://maps.google.com/maps/geo?q=".urlencode($addres)."&#038;output=csv&#038;key=".$gmapkey;<br />
		}<br />
	}<br />
}<br />
[/source]<br />
You need to replace $gmapkey with the key you can sign for at google. Our __constructor reads the text file and saves each line in an array. We then loop through this array and initialize URLs.</p>
<p>In the next method we will loop through the addresses to retrieve the geolocation.</p>
<p>[source:php]<br />
class Geolocation {</p>
<p>	private $url = array();</p>
<p>	function __construct($file) {<br />
		$gmapkey = "YOURKEY";<br />
		$address = file($file);<br />
		foreach($address as $addres) {<br />
			$this->url[] = "http://maps.google.com/maps/geo?q=".urlencode($addres)."&#038;output=csv&#038;key=".$gmapkey;<br />
		}<br />
	}<br />
	public function getLatLng() {<br />
		$i=1;<br />
		foreach($this->url as $urls) {<br />
			$cinit = curl_init();<br />
			curl_setopt($cinit, CURLOPT_URL, $urls);<br />
			curl_setopt($cinit, CURLOPT_HEADER,0);<br />
			curl_setopt($cinit, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);<br />
			curl_setopt($cinit, CURLOPT_FOLLOWLOCATION, 1);<br />
			curl_setopt($cinit, CURLOPT_RETURNTRANSFER, 1);<br />
			$response = curl_exec($cinit);<br />
			curl_close($cinit);</p>
<p>			if(strstr($data,"200")) {<br />
				$data = explode(",",$data);<br />
				echo $i.": ";<br />
				$zoom = $data[1];<br />
				echo $lat = $data[2];<br />
				echo $lon = $data[3]."<br />";<br />
			}</p>
<p>			/*<br />
			$sql = "INSERT INTO..."<br />
			$res = mysql_query($sql);<br />
			echo ($res) ? $i.": lat:".$latitude." lon: ".$longitdue." -- OK<br />" : $i.": ERROR<br />";<br />
			*/<br />
			$i++;<br />
		}<br />
	}<br />
}<br />
[/source]<br />
You can either echo the results or just save them in your MySQL database. You can also make multidimensional array and save some extra information about the certain place. </p>
<p>Let's take a look at how you should use this class.<br />
[source:php]<br />
include('geolocation.class.php');<br />
$geo = new Geolocation('address.txt');<br />
$geo->getLatLng();<br />
[/source]<br />
Basically you need to include the class, initialize it with the name of the file you want to use and getLatLng will do the magic for you. This class is pretty old and you may encounter one problem using it. After 10th request I started to receive the same geolocation for different places. What you can do is to put the foreach loop to sleep for 2 seconds and it should be fine. That's why I actually use iterator in this function. You basically need to add this line right after the loop starts.<br />
[source:php]<br />
if($i%10 == 0) sleep(2);<br />
[/source]</p>
<p>Here is the content of the text file:<br />
[source:html]<br />
Berlin, Dieffenbachstr. 38<br />
Berlin, Cuvrystr. 32<br />
Berlin, Lasdehner Str. 30<br />
[/source]<br />
Basically one address per line. That would be all, have fun with playing and extending this class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Google maps and jQuery &#8211; drop down and clickable maps</title>
		<link>http://www.designing4u.de/2008/05/google-maps-and-jquery-drop-down-and-clickable-maps/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=google-maps-and-jquery-drop-down-and-clickable-maps</link>
		<comments>http://www.designing4u.de/2008/05/google-maps-and-jquery-drop-down-and-clickable-maps/#comments</comments>
		<pubDate>Mon, 05 May 2008 11:21:28 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[add marker]]></category>
		<category><![CDATA[add overlay]]></category>
		<category><![CDATA[drop down list]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[retiriving geographical location]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=3</guid>
		<description><![CDATA[I had to manage google Maps for one of the sites I was working on. Basically there were two possibilities to get the user input. Either a user was forced to use one of the drop down options to define an area of the city he or she is living in, or a user could [...]]]></description>
			<content:encoded><![CDATA[<p>I had to manage google Maps for one of the sites I was working on. Basically there were two possibilities to get the user input. Either a user was forced to use one of the drop down options to define an area of the city he or she is living in, or a user could just click on the exact spot on the map where the marker was placed.<br />
<span id="more-3"></span><br />
To accomplish this task I wanted to use jQuery framework, from two simple reasons. First of all I wanted to create unobtrusive and cross browser compatible code using JavaScript and second of all I wanted to use simplicity, which jQuery gives us to save my time. Thinking about the claim of jQuery "do more, write less" I started to develop my code. Let's first break it into pieces and you can download all the files at the end of this post.</p>
<p>You can also see working demo <a href="http://www.designing4u.de/examples/drop-down-and-clickable-maps/map.html" title="demo" target="_blank">here</a></p>
<p>Let's start with including some necessary libraries. I know this might be obvious for almost all of you, but I want to make it as clear as possible for each one of you reading this post. Please add this two lines in the head section of your document. Please don't forget that we will use v2 of GMap in this example. You can download the jQuery library from their official site <a href="http://www.jquery.com" title="jQuery Home Page" target="_blank">www.jquery.com</a>. To get the google Maps API key for you your site, you should go to <a href="http://code.google.com/apis/maps/signup.html" title="google maps api key" target="_blank">this address</a> and you can sign up for a key for free.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=YOURKEY&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>You can also include the latest jQuery library directly from their site adding this code in the header of your document.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://code.jquery.com/jquery-latest.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></td></tr></table></div>

<p>I don't wont to discuss the advantages or disadvantages of both methods but if you want to know more about it you should check the cache possibilities of your browser. In our example I will use the latest code available from jQuery site.</p>
<p>Let's start to break my code into pieces and explain what it does. First we will define a ready event to our page and declare some global variables which we will use later in our function, so the code defined in the ready event will be executed after the DOM will be loaded. You need to place this code in the head section of your document.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> mark<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pointA<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pointB<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The first thing we need to do is to check if the client's browser is compatible with google maps at all. If this check returns true, we use jQuery to define a place holder for our map. In our case it will be a div tag with an id map. We also assume in this example that it will be the first element in our DOM. We will use Berlin as our example city. If jQuery finds our place holder it initializes the GMap and displays it in our div tag. We set the longitude and latitude in the center of Berlin and we are defining a default zoom level. We add some controls to allow our user to manipulate the map and we we are done with the first step of just displaying the map without including the onload even in the body tag of our document.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> mark<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pointA<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pointB<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>GBrowserIsCompatible<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> m <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#map&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>		
            <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> start <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">52.519563</span><span style="color: #339933;">,</span><span style="color: #CC0000;">13.414306</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> zoomLevel <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
            map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>start<span style="color: #339933;">,</span> zoomLevel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GSmallMapControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In the next step we want to give our user a possibility to choose the location by simply reading his' or hers' input. To accomplish this task we add an event listener, which will be triggered when a user clicks on our map. First we perform a check if a marker doesn't exist on our map and if it does we remove. After that we check if a point is defined and if it is we add an overlay, which displays the marker and save the latitude an longitude in our hidden input fields (the HTML code for this example will be shown at the end). We will need this information later to save it in the database.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> mark<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pointA<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pointB<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>GBrowserIsCompatible<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> m <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#map&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>		
            <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> start <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">52.519563</span><span style="color: #339933;">,</span><span style="color: #CC0000;">13.414306</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> zoomLevel <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
            map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>start<span style="color: #339933;">,</span> zoomLevel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GSmallMapControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>overlay<span style="color: #339933;">,</span> point<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    map.<span style="color: #660066;">removeOverlay</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    pointA <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GPoint<span style="color: #009900;">&#40;</span>point.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> point.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    mark <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>pointA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    map.<span style="color: #660066;">getCenter</span><span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#lng'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #339933;">,</span>point.<span style="color: #660066;">x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#lat'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #339933;">,</span>point.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In the last step we add an event listener, which will be triggered when a user use our drop down menu to choose a district. As I stated before, a user has a form to fill out where he is forced to choose the district of a city. We will use AJAX to retrieve this information from an external php file (map_district.php) to get the geographical location of the districts. It splits the string, which it gets and performs a check, if a marker exists already or not. After that it adds a marker to the map, increases the zoom level and overwrites the location in hidden input tags. </p>
<p>(How to determine the longitude and latitude of a certain place for free, you can read another example of mine <a href="http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/">here </a>.)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> mark<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pointA<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> pointB<span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>GBrowserIsCompatible<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> m <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#map&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>		
            <span style="color: #003366; font-weight: bold;">var</span> map <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #009900;">&#40;</span>m<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> start <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">52.519563</span><span style="color: #339933;">,</span><span style="color: #CC0000;">13.414306</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> zoomLevel <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
            map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>start<span style="color: #339933;">,</span> zoomLevel<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            map.<span style="color: #660066;">addControl</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GSmallMapControl<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            GEvent.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> <span style="color: #3366CC;">'click'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>overlay<span style="color: #339933;">,</span> point<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    map.<span style="color: #660066;">removeOverlay</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    pointA <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GPoint<span style="color: #009900;">&#40;</span>point.<span style="color: #660066;">x</span><span style="color: #339933;">,</span> point.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    mark <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>pointA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    map.<span style="color: #660066;">getCenter</span><span style="color: #009900;">&#40;</span>point<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#lng'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #339933;">,</span>point.<span style="color: #660066;">x</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#lat'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #339933;">,</span>point.<span style="color: #660066;">y</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#city'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">change</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #003366; font-weight: bold;">var</span> district <span style="color: #339933;">=</span> <span style="color: #3366CC;">'district='</span><span style="color: #339933;">+</span>$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                $.<span style="color: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                    type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span>
                    data<span style="color: #339933;">:</span> district<span style="color: #339933;">,</span>
                    url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'map_district.php'</span><span style="color: #339933;">,</span>
                    success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #003366; font-weight: bold;">var</span> cord <span style="color: #339933;">=</span> r.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;|&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                            map.<span style="color: #660066;">removeOverlay</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                        pointB <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #009900;">&#40;</span>cord<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>cord<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        mark <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #009900;">&#40;</span>pointB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        map.<span style="color: #660066;">addOverlay</span><span style="color: #009900;">&#40;</span>mark<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        map.<span style="color: #660066;">setCenter</span><span style="color: #009900;">&#40;</span>pointB<span style="color: #339933;">,</span> <span style="color: #CC0000;">14</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#lat'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #339933;">,</span>cord<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#lng'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'value'</span><span style="color: #339933;">,</span>cord<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This way even if a user will try to erase a marker, we will still have an information about the last placed marker, which we can save in our database. Last thing I need to mention is the content of php and html files.</p>
<p>I will just reduce this example to two districts, but you can add as many as you want. Basically we call this php file in our AJAX request and use POST method to retrieve the location, which we later use to place a marker.</p>
<pre lang="php" line"1">
if($_POST['district']=="Charlottenburg") {
    $lat = "52.51120638909939";
    $lng = "13.299808502197266";
} elseif($_POST['district']=="Friedrichshain") {
    $lat = "52.51360922969275";
    $lng = "13.442888259887695";
}
echo $lat."|".$lng;
</pre>
<p>In the body section of your document you need to place simple drop down list, a div tag with id map and two hidden input fields.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">select</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;city&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;city&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Charlottenburg&quot;</span>&gt;</span>Charlottenburg<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">option</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Friedrichshain&quot;</span>&gt;</span>Friedrichshain<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">option</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">select</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;map&quot;</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width:400px;height:400px;&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lng&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lng&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hidden&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lat&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;lat&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></pre></td></tr></table></div>

<p>That's all. If you want to download the files you can do it <a href="http://www.designing4u.de/examples/drop-down-and-clickable-maps/drop-down-and-clickable-maps.zip" title="drop down and clickable maps">here</a>. I'm waiting for your feedback.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/google-maps-and-jquery-drop-down-and-clickable-maps/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

