Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 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. $dsn= "$dbtype:host=$dbhost; dbname=$db_name; charset=utf8;"
  118.  
  119. try{
  120. $pdo=new PDO($dsn, $username, $password);
  121. $pdo->setAttribute(PDO::ATTR_ERRMODE,
  122. PDO::ERRMODE_EXCEPTION);
  123. $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  124. }catch(PDOException $Exception){
  125. die('エラー' .$Exception->getMessage());
  126. }
  127.  
  128. try{
  129. $pdo->beginTransaction();
  130. $sql="SELECT * FROM marker";
  131. $stmh = $pdo->prepare($sql);
  132. $stmh->execute();
  133.  
  134. header("Content-type: text/xml");
  135.  
  136. // Start XML file, echo parent node
  137. echo '<markers>';
  138.  
  139. // Iterate through the rows, printing XML nodes for each
  140. while ($row=$stmh -> fetch(PDO::FETCH_ASSOC)){
  141. // Add to XML document node
  142. echo '<marker ';
  143. echo 'title="' . parseToXML($row['name']) . '" ';
  144. echo 'comment="' . parseToXML($row['address']) . '" ';
  145. echo 'lat="' . $row['Latitude'] . '" ';
  146. echo 'lng="' . $row['Longitude'] . '" ';
  147. //echo 'type="' . $row['type'] . '" ';
  148. echo '/>';
  149. }
  150.  
  151. // End XML file
  152. echo '</markers>';
  153.  
  154. $pdo=null;
  155. }
  156.  
  157. catch(PDOException $Exception)
  158. {
  159. die("No Database");
  160. }
  161. ?>
  162.  
  163. <?php
  164. $dbtype="mysql";
  165. $dbhost="localhost";
  166. $username="root";
  167. $password="root";
  168. $database="localmaker";
  169. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement