首先先建立一個名為wholocations的資料表 內有id,lat&lon(經緯度),description(內文或HTML語法)
- CREATE TABLE wholocations (
- id int(11) NOT NULL auto_increment,
- lat decimal(10,6) NOT NULL default '0.000000',
- lon decimal(10,6) NOT NULL default '0.000000',
- description varchar(255) NOT NULL default '',
- PRIMARY KEY (id)
- ) TYPE=MyISAM;
複製代碼 再來就是將HEADER的部份填入您的API KEY,這個大家都知道
- <script type="text/javascript">
- //<![CDATA[
- var map = new GMap2(document.getElementById("map"));
- map.addControl(new GLargeMapControl());
- map.addControl(new GMapTypeControl());
- map.addControl(new GScaleControl());
- map.setCenter(new GLatLng(51.512161, -0.14110), 11, G_NORMAL_MAP);
- // Creates a marker whose info window displays the given number
- function createMarker(point, number)
- {
- var marker = new GMarker(point);
- // Show this markers index in the info window when it is clicked
- var html = number;
- GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
- return marker;
- };
複製代碼 最後則是將php的資料庫資訊填入,如資料庫位置,資料庫名稱,使用者名稱/密碼 填入之後該php程式就會自資料庫裡去將所有的經緯度,description資訊帶出來 透過echo的方式把圖釘點一個個產生出來
- <?php
- $link = mysql_connect("[database server]", "[username]", "[password]") or die("Could not connect: " . mysql_error());
- mysql_selectdb("[database name]",$link) or die ("Can\'t use dbmapserver : " . mysql_error());
- $result = mysql_query("SELECT * FROM wholocations",$link);
- if (!$result)
- {
- echo "no results ";
- }
- while($row = mysql_fetch_array($result))
- {
- echo "var point = new GLatLng(" . $row['lat'] . "," . $row['lon'] . ");\n";
- echo "var marker = createMarker(point, '" . addslashes($row['description']) . "');\n";
- echo "map.addOverlay(marker);\n";
- echo "\n";
- }
- mysql_close($link);
- ?>
- //]]>
- </script>
- </body>
複製代碼
</html>資料來源:Google Maps via PHP/Mysql Tutorial - API version 2 http://www.map-server.com/googlemaps/tutorial_api2.html
本文內容由 woff 提供
|