TShopping

 找回密碼
 註冊
搜索
TShopping 精選文章 電腦網絡 查看內容

Mysql/php結合Google Maps Api

2011-8-14 22:57| 發佈者: woff| 查看: 937| 評論: 0|原作者: test|來自: google

摘要: 首先先建立一個名為wholocations的資料表 內有id,latlon(經緯度),description(內文或HTML語法)CREATE TABLE wholocations ( id int(11) NOT NULL auto_increment, lat decimal(10,6) NOT NULL default '0.000000', l ...
 
首先先建立一個名為wholocations的資料表
內有id,lat&lon(經緯度),description(內文或HTML語法)
  1. CREATE TABLE wholocations (
  2.   id int(11) NOT NULL auto_increment,
  3.   lat decimal(10,6) NOT NULL default '0.000000',
  4.   lon decimal(10,6) NOT NULL default '0.000000',
  5.   description varchar(255) NOT NULL default '',
  6.   PRIMARY KEY  (id)
  7. ) TYPE=MyISAM;
複製代碼

再來就是將HEADER的部份填入您的API KEY,這個大家都知道
  1. <script type="text/javascript">
  2. //<![CDATA[

  3. var map = new GMap2(document.getElementById("map"));
  4. map.addControl(new GLargeMapControl());
  5. map.addControl(new GMapTypeControl());
  6. map.addControl(new GScaleControl());
  7. map.setCenter(new GLatLng(51.512161, -0.14110), 11, G_NORMAL_MAP);

  8. // Creates a marker whose info window displays the given number
  9. function createMarker(point, number)
  10. {
  11. var marker = new GMarker(point);
  12. // Show this markers index in the info window when it is clicked
  13. var html = number;
  14. GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
  15. return marker;
  16. };
複製代碼

最後則是將php的資料庫資訊填入,如資料庫位置,資料庫名稱,使用者名稱/密碼
填入之後該php程式就會自資料庫裡去將所有的經緯度,description資訊帶出來
透過echo的方式把圖釘點一個個產生出來
  1. <?php
  2. $link = mysql_connect("[database server]", "[username]", "[password]") or die("Could not connect: " . mysql_error());
  3. mysql_selectdb("[database name]",$link) or die ("Can\'t use dbmapserver : " . mysql_error());

  4. $result = mysql_query("SELECT * FROM wholocations",$link);
  5. if (!$result)
  6. {
  7. echo "no results ";
  8. }
  9. while($row = mysql_fetch_array($result))
  10. {
  11. echo "var point = new GLatLng(" . $row['lat'] . "," . $row['lon'] . ");\n";
  12. echo "var marker = createMarker(point, '" . addslashes($row['description']) . "');\n";
  13. echo "map.addOverlay(marker);\n";
  14. echo "\n";
  15. }

  16. mysql_close($link);
  17. ?>

  18. //]]>
  19. </script>

  20. </body>
複製代碼


</html>資料來源:Google Maps via PHP/Mysql Tutorial - API version 2
http://www.map-server.com/googlemaps/tutorial_api2.html

本文內容由 woff 提供



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-27 05:37 , Processed in 0.075515 second(s), 19 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

返回頂部