|
 |
|
การใช้ ajax ส่งค่า id ไปยังไฟล์ php จาก marker ที่คลิก มาแสดงใน infowindow |
|
 |
|
|
 |
 |
|
<script type="text/javascript" src="../js/ajax.googleapis.js"></script>
<script type="text/javascript">
var map; // กำหนดตัวแปร map ไว้ด้านนอกฟังก์ชัน เพื่อให้สามารถเรียกใช้งาน จากส่วนอื่นได้
var GGM; // กำหนดตัวแปร GGM ไว้เก็บ google.maps Object จะได้เรียกใช้งานได้ง่ายขึ้น
var infowindow;
function initialize() { // ฟังก์ชันแสดงแผนที่
GGM=new Object(google.maps); // เก็บตัวแปร google.maps Object ไว้ในตัวแปร GGM
// กำหนดจุดเริ่มต้นของแผนที่
var my_Latlng = new GGM.LatLng(6.950674, 100.412816);
// กำหนด DOM object ที่จะเอาแผนที่ไปแสดง ที่นี้คือ div id=map_canvas
var my_DivObj=$("#map_canvas")[0];
// กำหนด Option ของแผนที่
var myOptions = {
zoom: 11, // กำหนดขนาดการ zoom
center: my_Latlng , // กำหนดจุดกึ่งกลาง
mapTypeId:GGM.MapTypeId.ROAD, // กำหนดรูปแบบแผนที่
mapTypeControlOptions:{ // การจัดรูปแบบส่วนควบคุมประเภทแผนที่
position:GGM.ControlPosition.TOP, // จัดตำแหน่ง
style:GGM.MapTypeControlStyle.DROPDOWN_MENU // จัดรูปแบบ style
}
};
map = new GGM.Map(my_DivObj,myOptions);// สร้างแผนที่และเก็บตัวแปรไว้ในชื่อ map
$.ajax({
url:"c_map_xml.php", // ใช้ ajax ใน jQuery เรียกใช้ไฟล์ xml
type: "post",
data: {s_xml: $("#s_xml").val()},
success:function(xml){
$(xml).find('marker').each(function(i){ // วนลูปดึงค่าข้อมูลมาสร้าง marker
var markerID=$(this).attr("id");// นำค่าต่างๆ มาเก็บไว้ในตัวแปรไว้ใช้งาน
var markerName=$(this).find("name").text(); // นำค่าต่างๆ มาเก็บไว้ในตัวแปรไว้ใช้งาน
var markerLat=$(this).find("latitude").text(); // นำค่าต่างๆ มาเก็บไว้ในตัวแปรไว้ใช้งาน
var markerLng=$(this).find("longitude").text(); // นำค่าต่างๆ มาเก็บไว้ในตัวแปรไว้ใช้งาน
var markerLatLng=new GGM.LatLng(markerLat,markerLng);
var my_Marker = new GGM.Marker({ // สร้างตัว marker
position:markerLatLng, // กำหนดไว้ที่เดียวกับจุดกึ่งกลาง
map: map, // กำหนดว่า marker นี้ใช้กับแผนที่ชื่อ instance ว่า map
title:markerName + markerLat // แสดง title เมื่อเอาเมาส์มาอยู่เหนือ
});
infowindow = new GGM.InfoWindow({
content:$.ajax({
url:'placeDetail.php?placeID'+markerName,//ใช้ ajax ใน jQuery ดึงข้อมูลจากไฟล์ placeDetail.php มาแสดง
async:false
}).responseText
});
// กำหนด event ให้กับตัว marker เมื่อคลิกที่ตัว marker ให้แสดง infowindows
GGM.event.addListener(my_Marker, 'click', function() {
infowindow.open(map,my_Marker); // ให้แสดง infowindows รายละเอียดสถานที่ ทุกครั้งที่โหลดแผนที่แล้ว
});
// console.log($(this).find("id").text());
var mapCircle = new GGM.Circle({ // สร้างตัว circle
strokeColor: "#CC0000", // สีของเส้นสัมผัส หรือสีขอบโดยรอบ
strokeOpacity: 0.2, // ความโปร่งใส ของสีขอบโดยรอบ กำหนดจาก 0.0 - 0.1
strokeWeight: 1, // ความหนาของสีขอบโดยรอบ เป็นหน่วย pixel
fillColor: "#0022CC", // สีของวงกลม circle
fillOpacity: 0.03, // ความโปร่งใส กำหนดจาก 0.0 - 0.1
map: map, // กำหนดว่า circle นี้ใช้กับแผนที่ชื่อ instance ว่า map
center:my_Latlng, // ตำแหน่งศูนย์กลางของวลกลม ในที่นี้ใช้ตำแหน่งเดียวกับ ตัว marker
radius:5000 // รัศมีวงกลม circle ทีสร้าง หน่ายเป็น เมตร
});
});
}
});
}
$(function(){
// โหลด สคริป google map api เมื่อเว็บโหลดเรียบร้อยแล้ว
// ค่าตัวแปร ที่ส่งไปในไฟล์ google map api
// v=3.2&sensor=false&language=th&callback=initialize
// v เวอร์ชัน่ 3.2
// sensor กำหนดให้สามารถแสดงตำแหน่งทำเปิดแผนที่อยู่ได้ เหมาะสำหรับมือถือ ปกติใช้ false
// language ภาษา th ,en เป็นต้น
// callback ให้เรียกใช้ฟังก์ชันแสดง แผนที่ initialize
$("<script/>", {
"type": "text/javascript",
src: "http://maps.google.com/maps/api/js?v=3.2&sensor=false&language=th&callback=initialize"
}).appendTo("body");
});
</script>
Tag : PHP, MySQL, jQuery, JAVA

|
ประวัติการแก้ไข 2016-05-19 14:31:07
|
 |
 |
 |
 |
Date :
2016-05-18 16:49:44 |
By :
btsong |
View :
1805 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Learn the basic posting first baby 
|
 |
 |
 |
 |
Date :
2020-07-01 19:17:52 |
By :
PhrayaDev |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|