ผมใช้ php+mysql อยากได้ code เขี่ยนที่ คลิกที่ชื่อที่เก็บในฐาน แล้วให้มัน วิ่งไปหา พิกัด ในแผนที่ ครับ
และ code แบ่งกลุ่ม
ตย.ที่เขียนอยู่ครับ
$CONFIG{'hostname'} = '127.0.0.1';
$CONFIG{'user'} = 'root';
$CONFIG{'password'} = '367270';
$CONFIG{'db'} = 'test';
# WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING
#
# This script will attempt to create the database specified in $CONFIG{'db'}
# and a table called 'geo' in that database.
#
# WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING - WARNING
$DBH = DB_Connect();
// Make sure the table exists
assertTable();
// define vars
$desc = '';
$lat = '';
$lon = '';
$type = '';
// read POST variables if present
foreach ($_POST as $name => $value) {
$$name = $value;
}
// If post variables desc,lat and long are not empty then we're inserting
if ($desc && $lat && $lon) {
if (is_numeric($lat) && is_numeric($lon) && is_string($desc)&& is_string($type)) {
if ($lat < -85 || $lat > 85 || $lon < -180 || $lon > 180) {
print "You've entered invalid values";
exit;
}
$desc = addslashes(substr($desc,0,200));
$type = addslashes(substr($type,0,200));
$sql = "insert into geoPoints values ( null , $lat, $lon, '$desc','$type' )";
$result = mysql_query($sql,$DBH) or DBError("LINE: " .__LINE__. " $sql");
print "Point inserted<br><a href=\"{$_SERVER{'PHP_SELF'}}\">Back to the map</a>\n";
exit;
}
}
// Get the javascript defining the points after inserting.
$jsPointsArray = getPointsJS();
$pageHTML = <<<EOH
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="
http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8>
<title>Google Maps JavaScript API - PHP/DB Example</title>
<script src="/key.js"></script>
<script>
var scriptTag = '<' + 'script src="
http://maps.google.com/maps?file=api&v=2&key=' + 'ABQIAAAAUZcg0LDyHXOu2Bvuy8lPihT2yXp_ZAY8_ufC3CFXhHIE1NvwkxTtrGuv6Sq4mA3Ym0t1J7DQwJVn9w' + '">'+'<'+'/script>';
document.write(scriptTag);
</script>
<script type="text/javascript">
var mArray = Array();
var map;
var centerPoint = new GLatLng(13.078071,99.689453);
function load() {
doLoad();
$jsPointsArray
addMarkers();
}
function doLoad() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.setCenter(centerPoint, 7);
map.addControl(new GScaleControl());
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());
GEvent.addListener(map, 'click', mapClick);
}
}
function addMarkers() {
if (mArray.length) {
var bounds = new GLatLngBounds();
for (n=0 ; n < mArray.length ; n++ ) {
var mData = mArray[n].split(';');
var point = new GLatLng(mData[0],mData[1]);
bounds.extend(point);
var marker = createMarker(point, mData[2]);
map.addOverlay(marker);
}
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
}
}
function createMarker(point, title) {
var marker = new GMarker(point,{title:title});
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml('<div style="width:250px;">' + title + '<hr>Lat: ' + point.y + '<br>Lon: ' + point.x + '</div>');
});
return marker;
}
function mapClick(marker, point) {
if (!marker) {
oLat = document.getElementById("lat");
oLat.value = point.y;
oLon = document.getElementById("lon");
oLon.value = point.x;
oDesc = document.getElementById("desc");
oDesc.value = 'New desc';
otype = document.getElementById("type");
otype.value = 'New type';
}
}
</script>
</head>
<body onload="load()" onunload="GUnload()">
<div id="msg" style="width: 680px; border: 1px solid gray;font: bold 12px verdana;padding:3px;margin:10px;">
Note: Markers from all different visitors are going into the same database and are displayed together.<br>
They could be separated, but I chose to keep it simple. I'll empty the databse once in a while.
</div>
<div id="map" style="width: 700px; height: 500px"></div>
<div id="formDiv">
<table cellspacing="0" cellpadding="2">
<form method="POST">
<tr>
<td colspan="2" align="center">Add a marker</td>
</tr>
<tr>
<td>Description</td>
<td><input id="desc" name="desc"> Example: 'My marker</td>
</tr>
<tr>
<td>Latitude</td>
<td><input id="lat" name="lat"> Example: 40.0</td>
</tr>
<tr>
<td>Longitude</td>
<td><input id="lon" name="lon"> Example: -101.5</td>
</tr>
<tr>
<td>Type</td>
<td><SELECT NAME="type"id="type">
<option>Home</option>
<option>GAS</option>
<option>Hotel</option>
</SELECT></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Add Point"></td>
</tr>
</form>
</table>
</div>
</body>
</html>
EOH;
print $pageHTML;
print DoQueriy('Table contents',"select * from geoPoints order by id");
///////////////////////////// Functions ///////////////////////////////////////
function getPointsJS() {
global $DBH;
$sql = "select * from geoPoints";
$result = mysql_query($sql,$DBH) or DBError("LINE: " .__LINE__. " $sql");
$nRows = mysql_num_rows($result);
$javaScript = '';
if ($nRows) {
while ($row = mysql_fetch_assoc($result)) {
$row{'description'} = addslashes($row{'description'});
$row{'description'} = str_replace(';',',',$row{'description'});
$javaScript .= "mArray.push('{$row{'lat'}};{$row{'lon'}};{$row{'description'}}')\n";
}
print "Selected $nRows rows\n";
}
else {
print "No points found in database\n";
}
return $javaScript;
}
function assertTable() {
global $DBH;
// Create table, if it does not already exist
$createTableSQL = <<<EOS
CREATE TABLE IF NOT EXISTS geoPoints (
id int(11) NOT NULL auto_increment,
lat decimal(12,8) NOT NULL,
lon decimal(12,8) NOT NULL,
description varchar(255) NOT NULL,
type varchar(50) NOT NULL,
PRIMARY KEY (id)
) TYPE=MyISAM;
EOS;
$sql = $createTableSQL;
$result = mysql_query($sql,$DBH) or DBError("LINE: " .__LINE__. " $sql");
}
function DBError($sql){
print "Error: \n" . mysql_error() . "\n";
print "<hr>\n";
print "$sql\n";
print "<hr>\n";
exit;
}
function DB_Connect() {
global $CONFIG;
$DBH = mysql_connect($CONFIG{'hostname'}, $CONFIG{'user'}, $CONFIG{'password'}) or DBError("LINE: " .__LINE__. " Connect");;
$sql = "create database IF NOT EXISTS {$CONFIG{'db'}}";
$result = mysql_query($sql,$DBH) or DBError("LINE: " .__LINE__. " $sql");
mysql_select_db($CONFIG{'db'}, $DBH) or DBError("LINE: " .__LINE__. " Select DB");;
return $DBH;
}
function DoQueriy($title,$sql){
global $DBH;
$HTML = '';
$result = mysql_query($sql,$DBH) or DBError($sql);
$colCount = mysql_num_fields($result);
$rowCount = mysql_num_rows($result);
$HTML .= "<table cellspacing=\"0\" cellpadding=\"0\" border>\n";
$HTML .= "<tr>\n";
$HTML .= " <td colspan=\"$colCount\" style=\"padding:3px;font-weight:bold;text-align:center;\">\n";
$HTML .= " $title\n";
$HTML .= " </td>\n";
$HTML .= "</tr>\n";
if ($rowCount) {
if ($row = mysql_fetch_assoc($result)) {
$HTML .= "<tr>\n";
foreach ($row as $name => $value) {
$HTML .= " <td style=\"padding:3px;\">\n";
$HTML .= $name; // หัวตาราง
$HTML .= " </td>\n";
}
$HTML .= "</tr>\n";
}
mysql_data_seek($result,0);
while ($row = mysql_fetch_assoc($result)) {
$HTML .= "<tr>\n";
foreach ($row as $name => $value) {
$HTML .= " <td style=\"padding:3px;\">\n";
$HTML .= $value; // ข้อมูล
$HTML .= " </td>\n";
}
$HTML .= "</tr>\n";
}
}
else {
$HTML .= "<tr>\n";
$HTML .= " <td colspan=\"$colCount\" style=\"padding:3px;font-weight:bold;text-align:center;\">\n";
$HTML .= " Empty\n";
$HTML .= " </td>\n";
$HTML .= "</tr>\n";
}
$HTML .= "</table>\n";
$HTML .= "<br>\n";
return $HTML;
}
?>