<?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>tanomsak.com &#187; Google Map</title>
	<atom:link href="http://www.tanomsak.com/index.php/tag/google-map/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tanomsak.com</link>
	<description>Random Thoughts on Random Things by Tanomsak</description>
	<lastBuildDate>Wed, 29 Dec 2010 11:40:09 +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>เพิ่มหมุดจาก Google Maps ลง Database</title>
		<link>http://www.tanomsak.com/index.php/2008/12/google-maps-insert-database/</link>
		<comments>http://www.tanomsak.com/index.php/2008/12/google-maps-insert-database/#comments</comments>
		<pubDate>Wed, 24 Dec 2008 10:32:00 +0000</pubDate>
		<dc:creator>tanomsak</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Google Map]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.tanomsak.com/?p=150</guid>
		<description><![CDATA[ในเวป ddproperty.com ที่ผมกับเพื่อนดูแลอยู่เนี้ย เราให้คนที่ลงประกาศทุกคนต้องระบุตำแหน่งด้วยว่า อสังหาฯที่กำลังลงประกาศตำแหน่งอยู่ตรงไหน ผมก็ใ้ช้ google maps นี้แหละครับในการระบุตำแหน่ง คราวนี้เราจะมาดูกันว่า จะใช้ Google Maps เพิ่มตำแหน่งหมุดใน Database ยังไง จริงๆแล้ว trick หลักๆก็คือว่าเราจะเอาค่าตำแหน่ง lat, lng ของจุดที่เราต้องการออกมาจาก Google Maps ได้ยังไง ส่วนการบันทึกค่าลง database ก็ทำเหมือนกับการส่งค่าจาก form ตามปกติ ขั้นตอนคร่าวๆเป็นแบบนี้ครับ หาค่า lat, lng จากตำแหน่งที่ click บนแผนที่ เขียนค่า lat, lng ไว้ใน hidden fields ของ form ที่เราจะใช้ส่งค่ากลับไปฝั่ง server ทางฝั่ง server ก็รับกา่ร submit แล้วก็เขียนค่าลง database ง่ายไหมครับ ลองมาดูกันทีละขั้น [...]]]></description>
			<content:encoded><![CDATA[<div style="FLOAT: right; MARGIN-LEFT: 20px"><!--zickr--></div>
<p>ในเวป <a title="บ้านมือสอง ขายบ้าน" href="http://www.ddproperty.com">ddproperty.com</a> ที่ผมกับเพื่อนดูแลอยู่เนี้ย เราให้คนที่ลงประกาศทุกคนต้องระบุตำแหน่งด้วยว่า อสังหาฯที่กำลังลงประกาศตำแหน่งอยู่ตรงไหน ผมก็ใ้ช้ google maps นี้แหละครับในการระบุตำแหน่ง</p>
<p style="TEXT-ALIGN: center"><img class="size-full wp-image-151 aligncenter" title="ddproperty_post" src="http://www.tanomsak.com/wp-content/uploads/2008/12/ddproperty_post.jpg" alt="" width="500" height="275" /></p>
<p>คราวนี้เราจะมาดูกันว่า จะใช้ Google Maps เพิ่มตำแหน่งหมุดใน Database ยังไง <span id="more-150"></span>จริงๆแล้ว trick หลักๆก็คือว่าเราจะเอาค่าตำแหน่ง lat, lng ของจุดที่เราต้องการออกมาจาก Google Maps ได้ยังไง ส่วนการบันทึกค่าลง database ก็ทำเหมือนกับการส่งค่าจาก form ตามปกติ ขั้นตอนคร่าวๆเป็นแบบนี้ครับ</p>
<ol>
<li>หาค่า lat, lng จากตำแหน่งที่ click บนแผนที่</li>
<li>เขียนค่า lat, lng ไว้ใน hidden fields ของ form ที่เราจะใช้ส่งค่ากลับไปฝั่ง server</li>
<li>ทางฝั่ง server ก็รับกา่ร submit แล้วก็เขียนค่าลง database</li>
</ol>
<p>ง่ายไหมครับ ลองมาดูกันทีละขั้น</p>
<h3>ขั้นแรก หาค่า lat, lng จากตำแหน่งที่ click บนแผนที่</h3>
<p>Google Maps API มี function ต่างๆที่เตรียมไว้ให้ใช้งานเยอะมากครับ รวมถึงเมื่อผู้ใช้ Interactive กับแผนที่เช่น Click บนแผนที่ ซูมเข้า ซูมออก หรือว่าจะขยับหมุดก็มี Event เกี่ยวข้องทั้งหมด สิ่งที่เราต้องทำเพื่อหาตำแหน่งที่ผู้ใช้งาน Click บนแผนที่ก็คือสร้าง Listerner เพื่อมาดักจับ Event เหล่านี้ ซึ่งทำได้อย่างนี้ครับ</p>
<pre class="brush: jscript; title: ; notranslate"> GEvent.addListener(map, &amp;quot;click&amp;quot;, function(overlay, latlng) { /*เรา check ค่า overlay เพื่อดูว่าเป็นการกดบนแผนที่ หรือว่ากดบน overlay ที่อยู่บนแผนที่เช่น marker โดยตัวแปล overlay จะมีค่าเป็น null ในกรณีที่กดบนแผนที่เปล่าๆ*/ if (!overlay) { var lat = latlng.lat(); var lng = latlng.lng(); } }); </pre>
<p>โดย Event &#8220;click&#8221; เนี้ยจะส่งค่าตัวแปลไปให้ function ที่เราเขียนขึ้นมารับด้วย คือ overlay และ <code>latlng โดย overlay จะเป็นตัวบอกว่าเป็นการ click บนแผนที่ หรือว่า click บน overlay ส่วน latlng จะเป็นตัวแปลประเภท <a href="http://code.google.com/apis/maps/documentation/reference.html#GLatLng">GLatLng</a> ซึ่งมี method lat(), lng() เพื่อหาค่า lat, lng</code> แค่นี้เราก็จะได้ค่า lat, lng ของตำแหน่งที่ click บนแผนที่แล้วครับ</p>
<h3>เขียนค่า lat, lng ไว้ใน hidden fields ของ form ที่เราจะใช้ส่งค่ากลับไปฝั่ง server</h3>
<p>อันนี้ก็ตรงไปตรงมาครับ โดยเราจะำสร้าง form ที่มี hidden fields ชื่อ lat กับ lng ไว้ แล้วทำการ update ค่าทุกครั้งที่มีกาีร click บนแผนที่</p>
<pre class="brush: xml; title: ; notranslate">&lt;/p&gt; &lt;form action=&amp;quot;/new_marker&amp;quot;&gt; &lt;p&gt;&lt;input name=&amp;quot;marker[name]&amp;quot; size=&amp;quot;30&amp;quot;/&gt; &lt;input name=&amp;quot;marker[address]&amp;quot; size=&amp;quot;30&amp;quot;/&gt; &lt;input type=&amp;quot;hidden&amp;quot; id=&amp;quot;marker_lat&amp;quot; name=&amp;quot;marker[lat]&amp;quot;/&gt; &lt;input type=&amp;quot;hidden&amp;quot; id=&amp;quot;marker_lng&amp;quot; name=&amp;quot;marker[lng]&amp;quot;/&gt; &lt;input type=&amp;quot;submit&amp;quot; name=&amp;quot;commit&amp;quot; value=&amp;quot;submit&amp;quot;/&gt;&lt;/p&gt; &lt;/form&gt; &lt;p&gt;</pre>
<p>แล้วก็จัดกา่รแก้ไข javascript ของเราให้ Update value ของ hidden fields lat, lng สังเกตุว่า ผมใช้ $(&#8216;marker_lat&#8217;) แทนการ <em>getElementById</em> เพราะผมใช้ Prototype Framework อันนี้เพราะผมชินครับ ส่วนใครจะใช้ javascript แบบมาตราฐานก็ตามสะดวกครับ</p>
<pre class="brush: jscript; title: ; notranslate"> var map; var present = null; function updateHiddenFields(marker) { latlng = marker.getLatLng(); var lng = latlng.lng(); var lat = latlng.lat(); $('marker_lat').value = lat; $('marker_lng').value = lng; } function addMarker(latlng) { if (!present) { /*ใส่ options draggable เป็น true เพื่อให้สามารถย้ายตำแหน่งของหมุดได้ ส่วน bouncy เป็น false เพื่อไม่ให้หมุดเด้งหลังการย้ายตำแหน่ง*/ var newMarker = new GMarker(latlng, { draggable: true, bouncy: false}); map.addOverlay(newMarker); /*ตรวจจับ Event dragend เพื่อทำการ update ค่า lat,lng เมื่อมีย้ายหมุด*/ GEvent.addListener(newMarker, &amp;quot;dragend&amp;quot;, function() { updateHiddenFields(newMarker); }); updateHiddenFields(newMarker); map.panTo(latlng); present = true; } } function load() { if (GBrowserIsCompatible()) { map = new GMap2(document.getElementById(&amp;quot;map&amp;quot;)); map.setCenter(new GLatLng(37.4419, -122.1419), 13); GEvent.addListener(map, &amp;quot;click&amp;quot;, function(overlay, latlng) { if (!overlay) { addMarker(latlng); } }); } } </pre>
<p>เนื่องจากผมต้องการให้ระบุตำแหน่งทีละหนึ่งอัน (หนึ่งอสังหาฯ หนึ่งตำแหน่ง) เลยต้องมีตั้งค่าตัวแปล present เอาไว้ check ไม่ให้เพิ่มหมุดซ้ำ</p>
<h3>ทางฝั่ง server รับกา่ร submit แล้วก็เขียนค่าลง database</h3>
<p>ที่นี้ก็จัดกา่ร insert ค่าที่ได้รับจาก form ลง database ครับ</p>
<pre class="brush: ruby; title: ; notranslate"> def new_marker _marker = params[:marker] @marker = Marker.new(_marker) @marker.save! end </pre>
<p>เท่านี้ก็เป็นอันเรียบร้อยครับ <a href="http://tanomsak.com/tutorial-4.html">ลองดูตัวอย่าง</a> ลองดู Firebug console นะครับว่า ค่า value ของ hiddle fields เปลี่ยนหลังจากย้ายหมุดตามที่ต้องการหรือเปล่า <img class="aligncenter size-full wp-image-160" title="tutorail-4" src="http://www.tanomsak.com/wp-content/uploads/2008/12/tutorail-4.jpg" alt="" width="499" height="316" /></p>
<p class="zoundry_raven_tags"><!-- Tag links generated by Zoundry Raven. Do not manually edit. http://www.zoundryraven.com --> <span class="ztags"><span class="ztagspace">Del.icio.us</span> : <a class="ztag" rel="tag" href="http://del.icio.us/tag/Google%20Map">Google Map</a>, <a class="ztag" rel="tag" href="http://del.icio.us/tag/tutorial">tutorial</a></span><br />
<span class="ztags"><span class="ztagspace">Technorati</span> : <a class="ztag" rel="tag" href="http://www.technorati.com/tag/Google+Map">Google Map</a>, <a class="ztag" rel="tag" href="http://www.technorati.com/tag/tutorial">tutorial</a></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tanomsak.com/index.php/2008/12/google-maps-insert-database/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
	</channel>
</rss>

