<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Google Maps JavaScript API Example</title>
<meta http-equiv="content-type" content="text/html;charset=tis620"/>
<style type="text/css">
v:* {
behavior:url(#default#VML);
}
</style>
<script src="gmaps.CircleOverlay.js" type="text/javascript"></script>
<script src="http://maps.google.com/maps?file=api&v=2&key=ใส่ Maps Key ของคุณครับที่ได้ลงทะเบียนไว้ตรงนี้..." type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
var circle = null;
var circleRadius = 50; // Miles
var map = null;
var isCompatible = GBrowserIsCompatible();
function onLoad() {
map = new GMap(document.getElementById("div_map"));
map.addControl(new GLargeMapControl3D());
map.setCenter(new GLatLng(13.87308,100.862389), 7);
getMarkers();
GEvent.addListener(map, "click", function(overlay, point) {
if (overlay){ // การคลิ๊ก หรือ Mark Click
overlay.openInfoWindowHtml(overlay.infowindow); // open InfoWindow
} else if (point) { // พื้นหลัง
}
});
}
function getMarkers(){
var urlstr="read.php";
var request = GXmlHttp.create();
request.open('GET', urlstr , true); // request XML from PHP with AJAX call
request.onreadystatechange = function () {
if (request.readyState == 4) {
var xmlDoc = request.responseXML;
locations = xmlDoc.documentElement.getElementsByTagName("location");
markers = [];
if (locations.length){
for (var i = 0; i < locations.length; i++) { // cycle thru locations
var newHouse = new GIcon(G_DEFAULT_ICON);
newHouse.iconSize = new GSize(20,20);
newHouse.image = "add_24.png";
markers[i] = new GMarker(new GLatLng(locations[i].getAttribute("lat"),locations[i].getAttribute("lng")), {draggable: false,icon:newHouse});
// Add attributes to the marker so we can poll them later.
// When clicked, an overlay will have these properties.
markers[i].infowindow = "ที่นี่ คือ "+locations[i].getAttribute("name");
// Useful things to store on a marker (Not needed for this example, could be removed)
// Tells you what index in the markers[] array an overlay is
markers[i].markerindex = i;
// Store the location_id of the location the marker represents.
// Very useful to know the true id of a marker, you could then make
// AJAX calls to the database to update the information if you had it's location_id
markers[i].db_id = locations[i].getAttribute("location_id");
markers[i].circle = new CircleOverlay(map.getCenter(), circleRadius, "#336699", 1, 1, '#336699', 0.25);
var polygon = new GPolygon([locations[i].getAttribute("lat"),locations[i].getAttribute("lng")], "#f33f00", 5, 1, "#ff0000", 0.2);
map.addOverlay(polygon);
map.addOverlay(markers[i]);
}
}
}
}
request.send(null);
}
//]]>
</script>
</head>
<body onload="onLoad()">
<center>
<div id="div_map" style="width: 600px; height: 400px"></div>
</center>
</body>
</html>
ส่วนอันนี้ read.php Code (PHP)
<?php
$host = 'localhost';
$user = 'root';
$pass = "1234";
$dbname = 'map';
if (!$db = mysql_connect($host, $user, $pass)) {
echo 'Could not connect to mysql';
exit;
}
if (!mysql_select_db($dbname, $db)) {
echo 'Could not select database';
exit;
}
$query = "SELECT location_id, name, lat, lng FROM locations WHERE 1";
$query = mysql_query($query);
echo "<locations>";
while ($row=mysql_fetch_assoc($query)){
echo '<location id="'.$row['location_id'].'" name="'.$row['name'].'" lat="'.$row['lat'].'" lng="'.$row['lng'].'"/>';
}
echo "</locations>";
?>