Guest User

Untitled

a guest
Dec 3rd, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. <!DOCTYPE html >
  2. <head>
  3. <meta name="viewport" content="width=device-width,initial-scale=1">
  4. <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
  5. <title>Using MySQL and PHP with Google Maps</title>
  6. <style>
  7. /* Always set the map height explicitly to define the size of the div
  8. * element that contains the map. */
  9. #map {
  10. height: 100%;
  11. }
  12. /* Optional: Makes the sample page fill the window. */
  13. html, body {
  14. height: 100%;
  15. margin: 0;
  16. padding: 0;
  17. }
  18. </style>
  19. </head>
  20.  
  21. <body>
  22. <div id="map"></div>
  23. <script>
  24. /*var customLabel = {
  25. restaurant: {
  26. label: 'R'
  27. },
  28. bar: {
  29. label: 'B'
  30. }
  31. };*/
  32.  
  33. function initMap() {
  34. var bodyHeight = $('body').height();
  35. $("#map").css('height',bodyHeight);
  36. var map = new google.maps.Map(document.getElementById('map'), {
  37. center: new google.maps.LatLng(-33.863276, 151.207977),
  38. zoom: 12
  39. });
  40. var infoWindow = new google.maps.InfoWindow;
  41.  
  42. // Change this depending on the name of your PHP or XML file URL:https://storage.googleapis.com/mapsdevsite/json/mapmarkers2.xml
  43. downloadUrl('php/json.php', function(data) {
  44. var xml = data.responseXML;
  45. var markers = xml.documentElement.getElementsByTagName('marker');
  46. Array.prototype.forEach.call(marker, function(markerElem) {
  47. var title = markerElem.getAttribute('title');
  48. var comment = markerElem.getAttribute('comment');
  49. //var type = markerElem.getAttribute('type');
  50. var point = new google.maps.LatLng(
  51. parseFloat(markerElem.getAttribute('lat')),
  52. parseFloat(markerElem.getAttribute('lng')));
  53.  
  54. var infowincontent = document.createElement('div');
  55. var strong = document.createElement('strong');
  56. strong.textContent = title
  57. infowincontent.appendChild(strong);
  58. infowincontent.appendChild(document.createElement('br'));
  59.  
  60. var text = document.createElement('text');
  61. text.textContent = comment
  62. infowincontent.appendChild(text);
  63. //var icon = customLabel[type] || {};
  64. var marker = new google.maps.Marker({
  65. map: map,
  66. position: point,
  67. //label: icon.label
  68. });
  69. marker.addListener('click', function() {
  70. infoWindow.setContent(infowincontent);
  71. infoWindow.open(map, marker);
  72. });
  73. });
  74. });
  75. }
  76.  
  77.  
  78.  
  79. function downloadUrl(url, callback) {
  80. var request = window.ActiveXObject ?
  81. new ActiveXObject('Microsoft.XMLHTTP') :
  82. new XMLHttpRequest;
  83.  
  84. request.onreadystatechange = function() {
  85. if (request.readyState == 4) {
  86. request.onreadystatechange = doNothing;
  87. callback(request, request.status);
  88. }
  89. };
  90.  
  91. request.open('GET', url, true);
  92. request.send(null);
  93. }
  94.  
  95. function doNothing() {}
  96. </script>
  97. <script async defer
  98. src="https://maps.googleapis.com/maps/api/js?key=******&callback=initMap">
  99. </script>
  100. </body>
  101. </html>
  102.  
  103. <?php
  104. ini_set("display_errors", On);
  105. error_reporting(E_ALL);
  106. require("phpsqlajax_dbinfo.php");
  107.  
  108. function parseToXML($htmlStr)
  109. {
  110. $xmlStr=str_replace('<','<',$htmlStr);
  111. $xmlStr=str_replace('>','>',$xmlStr);
  112. $xmlStr=str_replace('"','"',$xmlStr);
  113. $xmlStr=str_replace("'",'&#39;',$xmlStr);
  114. $xmlStr=str_replace("&",'&',$xmlStr);
  115. return $xmlStr;
  116. }
  117.  
  118. // Opens a connection to a MySQL server
  119. $dsn= "$dbtype:host=$dbhost; dbname=$database; charset=utf8;"
  120.  
  121. try{
  122. $pdo=new PDO($dsn, $username, $password);
  123. $pdo->setAttribute(PDO::ATTR_ERRMODE,
  124. PDO::ERRMODE_EXCEPTION);
  125. $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  126. }catch(PDOException $Exception){
  127. die('エラー' .$Exception->getMessage());
  128. }
  129.  
  130. try{
  131. $pdo->beginTransaction();
  132. $sql="SELECT * FROM marker";
  133. $stmh = $pdo->prepare($sql);
  134. $stmh->execute();
  135.  
  136. header("Content-type: text/xml");
  137.  
  138. // Start XML file, echo parent node
  139. echo '<markers>';
  140.  
  141. // Iterate through the rows, printing XML nodes for each
  142. while ($row=$stmh -> fetchObject()){
  143. // Add to XML document node
  144. echo '<marker ';
  145. echo 'title="' . parseToXML($row['name']) . '" ';
  146. echo 'comment="' . parseToXML($row['address']) . '" ';
  147. echo 'lat="' . $row['Latitude'] . '" ';
  148. echo 'lng="' . $row['Longitude'] . '" ';
  149. //echo 'type="' . $row['type'] . '" ';
  150. echo '/>';
  151. }
  152.  
  153. // End XML file
  154. echo '</markers>';
  155.  
  156. $pdo=null;
  157. }
  158.  
  159. catch(PDOException $Exception)
  160. {
  161. die("No Database");
  162. }
  163. ?>
  164.  
  165. <?php
  166. $dbtype="mysql";
  167. $dbhost="localhost";
  168. $username="root";
  169. $password="root";
  170. $database="localmaker";
  171. ?>
Add Comment
Please, Sign In to add comment