Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,025

HOME > Google Maps APIs สอนวิธีการใช้ Maps ของ Google เพื่อเรียกใช้งานแผนที่และแสดงผลบนเว็บไซต์ > ตอนที่ 6 : Google Maps API การปักหมุด (Marker) หลายตำแหน่งจากข้อมูลชุด Array



Clound SSD Virtual Server

ตอนที่ 6 : Google Maps API การปักหมุด (Marker) หลายตำแหน่งจากข้อมูลชุด Array

Google Maps API การปักหมุด (Marker) หลายตำแหน่งจากข้อมูลชุด Array ในการปักหมุด (Marker) บน Google Maps ในกรณีที่มีการปักหลายๆ ตำแหน่ง เราสามารถนำตัวแปรของ JavaScript ที่เป็น Array ที่ประกอบด้วยหลายๆ คีย์ ที่จัดเก็บ Location , Latitude และ Longitude มาจัดเก็บค่าสถานที่หลายๆ ที่ได้ และ นำตัวแปรนั้นมาใช้นการปักหมุด ซึ่งจะสามาถช่วยให้การปักหมุด ในหลายๆ จุดทำได้ง่ายและสะดวกยิ่งขึ้น เพราะเราสามารถจะ Loop ค่าจาก Array แล้วปักลงบน Maps ได้เลย




Google Maps API and Marker from Array Object


สร้าง Map ตามปกติ
			var mapOptions = {
			  center: {lat: 13.847860, lng: 100.604274},
			  zoom: 15,
			}
			var maps = new google.maps.Map(document.getElementById("map"),mapOptions);

การสร้างตัวแปร Array จาก JavaScript
		var locations = [
		  ['วัดลาดปลาเค้า', 13.846876, 100.604481],
		  ['หมู่บ้านอารียา', 13.847766, 100.605768],
		  ['สปีดเวย์', 13.845235, 100.602711],
		  ['สเต็ก ลุงหนวด',13.862970, 100.613834]
		];

การ Loop ค่าตัวแปร Array
for (i = 0; i < locations.length; i++) {  

}

ซึ่งเมื่อ Loop แล้วก็ให้แทรก Marker ลงในค่าที่มาจาก Array
			var marker, i, info;

			for (i = 0; i < locations.length; i++) {  

				marker = new google.maps.Marker({
				   position: new google.maps.LatLng(locations[i][1], locations[i][2]),
				   map: maps,
				   title: locations[i][0]
				});

				info = new google.maps.InfoWindow();

			  google.maps.event.addListener(marker, 'click', (function(marker, i) {
				return function() {
				  info.setContent(locations[i][0]);
				  info.open(maps, marker);
				}
			  })(marker, i));

			}



จะเห็นว่าในส่วนของการ Loop เพื่อปักหมุด (Marker) จะมีการ Add ตัว Event สำหรับคลิกที่หมุด และ แสดงผล InfoWindow ของหมุดนั้นๆ ด้วย








ผลลัพธ์ที่ได้



หมุดถูกปักตามจำนวน Location ที่มาจาก Array



เมื่อมีการคลิกที่หมุดจะแสดง Info Window ของ Location นั้นๆ



เมื่อมีการคลิกที่หมุดจะแสดง Info Window ของ Location นั้นๆ

Code ทั้งหมด
<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html {
        height: 100%;
        margin: 0;
        padding: 0;
		text-align: center;
      }

      #map {
        height: 500px;
        width: 600px;
      }
    </style>
  </head>
  <body>
  <div id="map"></div>
    <script>

		var locations = [
		  ['วัดลาดปลาเค้า', 13.846876, 100.604481],
		  ['หมู่บ้านอารียา', 13.847766, 100.605768],
		  ['สปีดเวย์', 13.845235, 100.602711],
		  ['สเต็ก ลุงหนวด',13.862970, 100.613834]
		];

      function initMap() {
			var mapOptions = {
			  center: {lat: 13.847860, lng: 100.604274},
			  zoom: 15,
			}
				
			var maps = new google.maps.Map(document.getElementById("map"),mapOptions);
			
			var marker, i, info;

			for (i = 0; i < locations.length; i++) {  

				marker = new google.maps.Marker({
				   position: new google.maps.LatLng(locations[i][1], locations[i][2]),
				   map: maps,
				   title: locations[i][0]
				});

				info = new google.maps.InfoWindow();

			  google.maps.event.addListener(marker, 'click', (function(marker, i) {
				return function() {
				  info.setContent(locations[i][0]);
				  info.open(maps, marker);
				}
			  })(marker, i));

			}

		}
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAK3RgqSLy1toc4lkh2JVFQ5ipuRB106vU&callback=initMap" async defer></script>
  </body>
</html>









   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2017-04-08 21:59:10 / 2017-04-09 07:38:11
  Download : No files
 Sponsored Links / Related

 
ตอนที่ 1 : Google Maps API (JavaScript) เบื้องต้นกับการใช้งานแผนที่บนของกูเกิลแมพ (Step by Step)
Rating :

 
ตอนที่ 2 : Google Maps API การเปิด-ปิด Control ต่างๆ ที่อยู่บนหน้า Maps แผนที่
Rating :

 
ตอนที่ 3 : Google Maps API การแสดงมุมมองของแผนที่ต่างๆ เช่น Zoom , Map Type
Rating :

 
ตอนที่ 4 : Google Maps API การสร้าง Custom Maps Style กำหนดรูปแบบของแผนที่
Rating :

 
ตอนที่ 5 : Google Maps API การปักหมุด (Marker) บนแผนที่ จาก Lat,Lon และรายละเอียด
Rating :

 
ตอนที่ 7 : Google Maps API การปักหมุด (Marker) หลายตำแหน่งจากข้อมูลชุด Json Object
Rating :

 
ตอนที่ 8 : Google Maps API Geolocation การอ่านและโฟกัสตำแหน่ง Location ปัจจุบัน
Rating :

 
ตอนที่ 9 : Google Maps API Geocoding การค้นหาระบุตำแหน่ง Location บนแผนที่
Rating :

 
ตอนที่ 10 : Google Maps API การปักหมุด (Marker) บนแผนที่ จาก PHP/MySQL (Json)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่