<?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; retiriving geographical location</title>
	<atom:link href="http://www.designing4u.de/tag/retiriving-geographical-location/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>Class for retrieving region, city and geolocation</title>
		<link>http://www.designing4u.de/2008/05/class-for-retrieving-region-city-and-geolocation/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=class-for-retrieving-region-city-and-geolocation</link>
		<comments>http://www.designing4u.de/2008/05/class-for-retrieving-region-city-and-geolocation/#comments</comments>
		<pubDate>Mon, 19 May 2008 10:24:29 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[city]]></category>
		<category><![CDATA[Geolocation]]></category>
		<category><![CDATA[Geolocation class]]></category>
		<category><![CDATA[location]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[region]]></category>
		<category><![CDATA[retiriving geographical location]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=16</guid>
		<description><![CDATA[For one of the social platforms I was creating I had to provide profile display according to the location from which the request was made. There are plenty services in the internet, which provide you an accurate database, which will translate IP address into region, city and geographical lengths. I didn't want to spend any [...]]]></description>
			<content:encoded><![CDATA[<p>For one of the social platforms I was creating I had to provide profile display according to the location from which the request was made. There are plenty services in the internet, which provide you an accurate database, which will translate IP address into region, city and geographical lengths. I didn't want to spend any money for this kind of service, so I decided to find a free solution for this task. However, none of the services existing in the internet provides you 100% accuracy, the one that I've found locates my IP correctly. It would be nice, if you could give me some feedback, if this class is working for you as good as it works for me. This example requires URL file-access to be enabled on your server, because we will request the XML data through URL call. In this example we will also use SimpleXMLElement class, which should be enable by default in PHP5, to translate our XML file into an object. We will then recurse received object into a suitable array.<br />
<span id="more-16"></span><br />
Unfortunately my provider doesn't enable URL file-access, therefore I wont be able to show you, how this example works. I tested it on different server though and you can trust me, it works. </p>
<p>The first thing you need to do is to go to <a href="http://www.getpos.de/anmeldung.aspx" target="_blank" title="go to getpos.de">getpos.de</a> and register a new account. Unfortunately this service is only in  German, but I don't think you should have any problem with registering a new account. Let's take a look at our class:<br />
[source:php]<br />
class Geolocation {</p>
<p>	private $ip;<br />
	private $xml_data;<br />
	private $xml;</p>
<p>	public $geo;<br />
	public $error;</p>
<p>	function __construct($ip) {<br />
		$this->ip = $ip;<br />
		$this->xml_data = simplexml_load_file("http://services.getpos.de/ip2loc.asmx/GetIPPosition?UserName=YOUR_USERNAME&#038;Password=YOUR_PASSWORD&#038;IPAddress=".$this->ip);<br />
	}</p>
<p>	public function findGeo() {<br />
		if(!$this->xml_data) {<br />
			$this->error = "service unavailable";<br />
			return false;<br />
		}<br />
		$this->xml = new SimpleXMLElement($this->xml_data);<br />
		$this->RecurseXML($this->xml,$this->geo);<br />
	}</p>
<p>	private function RecurseXML($xml,$vals,$parent=""){<br />
		$child_count = 0;<br />
		foreach($xml as $key=>$value) {<br />
			$child_count++;<br />
			$k = ($parent == "") ? (string)$key : $parent.".".(string)$key;</p>
<p>			if($this->RecurseXML($value,$this->geo,$k) == 0)<br />
				$this->geo[$k] = (string)$value;<br />
		}<br />
		return $child_count;<br />
	}<br />
}<br />
[/source]<br />
We define IP address, XML data and an instance of SimpleXMLObject as private properties and our error handler and actual geo data as public properties. In our constructor we pass IP address and we use simplexml_load_file function to interpret the XML file into an object. Obviously you need to replace YOUR_USERNAME and YOUR_PASSWORD with the information you will use to register at getpos.de. In findGeo() method we first check, if the service is available, and if not we show error message. If our service is available though, we instantiate SimpleXMLObject with the XML data. We than use RecurseXML() method to translate our object into a suitable array. The credit for RecurseXML() goes to transglobe at gmx dot de. I found this function in the comments section of <a href="http://de3.php.net/manual/en/function.simplexml-element-children.php" target="_blank" title="SimpleXML element children">PHP manual</a> and changed it a little bit to suit our needs.</p>
<p>Let's take a closer look, how to use our class:<br />
[source:php]<br />
include("geolocation.class.php");<br />
$geo = new Geolocation($_SERVER['REMOTE_ADDR']);</p>
<p>$geo->findGeo();<br />
echo (isset($geo->error)) ? $geo->error : '';</p>
<p>echo "geo['returncode']:".$geo->geo['returncode']."<br />";<br />
echo "geo['country.code']:".$geo->geo['country.code']."<br />";<br />
echo "geo['country.name']:".$geo->geo['country.name']."<br />";<br />
echo "geo['region.code']:".$geo->geo['region.code']."<br />";<br />
echo "geo['region.name']:".$geo->geo['region.name']."<br />";<br />
echo "geo['city']:".$geo->geo['city']."<br />";<br />
echo "geo['position.longitude']:".$geo->geo['position.longitude']."<br />";<br />
echo "geo['position.latitude']:".$geo->geo['position.latitude']."<br />";<br />
echo "geo['returncode']:".$geo->geo['returncode']."<br />";<br />
[/source]<br />
We include our class and instantiate it with the remote address of a visitor. We than call findGeo() method, which will translate the IP address for us. We can than echo our results or just use them to display relevant information for our users. Obviously, if a user is connected through a proxy or some AOL users in Europe, who are connected through American servers, this class wont retrieve correct information. You need to take into consideration though, that about 65% of your visitors will get correct content. I hope it is clear enough. If you encounter any problems with this class please let me know. As mentioned before, it would be also nice if you could give me any feedback about the accuracy of this service.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/class-for-retrieving-region-city-and-geolocation/feed/</wfw:commentRss>
		<slash:comments>4</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>

