Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. //THIS IS MY map.html
  2. <!DOCTYPE html >
  3. <head>
  4. <meta name="viewport" content="initial-scale=1.0, user-scalable=no"
  5. />
  6. <meta http-equiv="content-type" content="text/html; charset=UTF-
  7. 8"/>
  8. <title>From Info Windows to a Database: Saving User-Added Form
  9. Data</title>
  10. <style>
  11. /* Always set the map height explicitly to define the size of the
  12. div
  13. * element that contains the map. */
  14. #map {
  15. height: 100%;
  16. }
  17. /* Optional: Makes the sample page fill the window. */
  18. html, body {
  19. height: 100%;
  20. margin: 0;
  21. padding: 0;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="map" height="460px" width="100%"></div>
  27. <div id="form">
  28. <table>
  29. <tr><td>Name:</td> <td><input type='text' id='name'/> </td> </tr>
  30. <tr><td>Address:</td> <td><input type='text' id='address'/> </td>
  31. </tr>
  32. <tr><td>Type:</td> <td><select id='type'> +
  33. <option value='bar' SELECTED>bar</option>
  34. <option value='restaurant'>restaurant</option>
  35. </select> </td></tr>
  36. <tr><td></td><td><input type='button' value='Save'
  37. onclick='saveData()'/></td></tr>
  38. </table>
  39. </div>
  40. <div id="message">Location saved</div>
  41. <script>
  42. var map;
  43. var marker;
  44. var infowindow;
  45. var messagewindow;
  46.  
  47. function initMap() {
  48. var california = {lat: 37.4419, lng: -122.1419};
  49. map = new google.maps.Map(document.getElementById('map'), {
  50. center: california,
  51. zoom: 13
  52. });
  53.  
  54. infowindow = new google.maps.InfoWindow({
  55. content: document.getElementById('form')
  56. });
  57.  
  58. messagewindow = new google.maps.InfoWindow({
  59. content: document.getElementById('message')
  60. });
  61.  
  62. google.maps.event.addListener(map, 'click', function(event) {
  63. marker = new google.maps.Marker({
  64. position: event.latLng,
  65. map: map
  66. });
  67.  
  68.  
  69. google.maps.event.addListener(marker, 'click', function() {
  70. infowindow.open(map, marker);
  71. });
  72. });
  73. }
  74.  
  75. function saveData() {
  76. var name = escape(document.getElementById('name').value);
  77. var address = escape(document.getElementById('address').value);
  78. var type = document.getElementById('type').value;
  79. var latlng = marker.getPosition();
  80. var url = 'map.php?name=' + name + '&address=' + address +
  81. '&lat=' + latlng.lat() + '&lng=' + latlng.lng() +
  82. '&type=' + type;
  83.  
  84. downloadUrl(url, function(data, responseCode) {
  85.  
  86. if (responseCode == 200 && data.length <= 1) {
  87. infowindow.close();
  88. messagewindow.open(map, marker);
  89. }
  90. });
  91. }
  92.  
  93. function downloadUrl(url, callback) {
  94. var request = window.ActiveXObject ?
  95. new ActiveXObject('Microsoft.XMLHTTP') :
  96. new XMLHttpRequest;
  97.  
  98. request.onreadystatechange = function() {
  99. if (request.readyState == 4) {
  100. request.onreadystatechange = doNothing;
  101. callback(request.responseText, request.status);
  102. }
  103. };
  104.  
  105. request.open('GET', url, true);
  106. request.send(null);
  107. }
  108.  
  109. function doNothing () {
  110. }
  111.  
  112. </script>
  113. <script async defer
  114. src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDKUgH-
  115. lPLT16oiVuXnA4_Qu2xLWxpm5S8&callback=initMap">
  116. </script>
  117. </body>
  118. </html>
  119.  
  120. // THIS IS MY map.php
  121. <?php
  122. require("mapinfo.php");
  123.  
  124. // Gets data from URL parameters.
  125. $name = $_GET['name'];
  126. $address = $_GET['address'];
  127. $lat= $_GET['lat'];
  128. $lat = round($lat,4);
  129. $lng= $_GET['lng'];
  130. $lng = round($lng,4);
  131. $type = $_GET['type'];
  132.  
  133. // Opens a connection to a MySQL server.
  134. $connection=mysql_connect ("localhost", $username, $password);
  135. if (!$connection) {
  136. die('Not connected : ' . mysql_error());
  137. }
  138.  
  139. // Sets the active MySQL database.
  140. $db_selected = mysql_select_db($database, $connection);
  141. if (!$db_selected) {
  142. die ('Can't use db : ' . mysql_error());
  143. }
  144.  
  145. // Inserts new row with place data.
  146. $query = sprintf("INSERT INTO markers " .
  147. " (id, name, address, lat, lng, type ) " .
  148. " VALUES (NULL, '%s', '%s', '%s', '%s', '%s');",
  149. mysql_real_escape_string($name),
  150. mysql_real_escape_string($address),
  151. mysql_real_escape_string($lat),
  152. mysql_real_escape_string($lng),
  153. mysql_real_escape_string($type));
  154.  
  155. $result = mysql_query($query);
  156.  
  157. if (!$result) {
  158. die('Invalid query: ' . mysql_error());
  159. }
  160.  
  161. ?>
  162.  
  163. <?php
  164. $username="username";
  165. $password="password";
  166. $database="mapexp2";
  167. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement