搜索时如何使用数据库中的php在googleMap中显示多个标记?

e5njpo68  于 2021-09-23  发布在  Java
关注(0)|答案(1)|浏览(458)

我尝试了一个简单的搜索功能来查找医院名称,然后它会显示在Map上,我在搜索时使用了$u post superglobal,它会在数据库中查找结果。代码稍微起作用,稍微起作用时,它只在Map上显示一个标记,即使它应该根据您在文本框中输入的文本显示多个结果。

  1. <?php
  2. if(isset($_POST['search'])){
  3. $term = $_POST['term'];
  4. $query = "SELECT * from hospitals WHERE hname LIKE '%$term%' ";
  5. $search_query = mysqli_query($connection, $query);
  6. if(!$search_query){
  7. die("QUERY FAILED" . mysqli_error($connection));
  8. }
  9. $count = mysqli_num_rows($search_query);
  10. if($count == 0){
  11. echo "<script>
  12. alert('No Result!.');
  13. window.location.href='Index.php';
  14. </script>";
  15. } else if (empty($term)){
  16. echo "<script>
  17. alert('Please fill up.');
  18. window.location.href='Index.php';
  19. </script>";
  20. } else {
  21. $select_all_hospitals_query = mysqli_query($connection, $query);
  22. while($row = mysqli_fetch_assoc($select_all_hospitals_query)){
  23. $post_hname = $row['hname'];
  24. $post_address = $row['Address'];
  25. $post_lat = $row['lat'];
  26. $post_longi = $row['lng'];
  27. $coordinates = 'new google.maps.LatLng( '. $row['lat']. ','. $row['lng']. '),';
  28. }
  29. }
  30. }
  31. ?>
  32. <html>
  33. <html lang="en">
  34. <head>
  35. <meta charset="utf-8">
  36. <title>LabSeek</title>
  37. <link rel="canonical" href="https://getbootstrap.com/docs/5.0/examples/sidebars/">
  38. <link rel="stylesheet" type="text/css" href="map/map.css" />
  39. <link href="styles/sidebars.css" rel="stylesheet">
  40. <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
  41. <!-- <script src="map/map.js"></script> -->
  42. </head>
  43. <body>
  44. <div id="map">
  45. <!-- Async script executes immediately and must be after any DOM elements used in callback. -->
  46. <script
  47. src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBQbnrTvBW0ML5bVlgsgMhYCqdRR04d7k0&callback=initMap&libraries=&v=weekly"
  48. async
  49. ></script>
  50. <script>
  51. function initMap() {
  52. var options = {
  53. zoom:14,
  54. center: { lat: 14.586225342331847, lng: 120.99824317301842 }
  55. }
  56. //new map
  57. var map = new
  58. google.maps.Map(document.getElementById('map'),options);
  59. //add marker from search
  60. function addMarker(coords){
  61. var marker = new google.maps.Marker({
  62. position: coords,map:map, });
  63. }
  64. addMarker({<?php echo 'lat:'. $post_lat .', lng:'. $post_longi; ?>});
  65. }
  66. </script>
  67. </div>
  68. </html>
4szc88ey

4szc88ey1#

以下内容完全未经测试!这个 Object Orientated 使用风格 mySQLi 要比 procedural 由于在sql语句中直接使用post数据,因此您的代码容易受到sql注入的攻击。您应该考虑使用准备好的语句来减轻这种威胁。
似乎可以简化php代码以生成一些json数据,一旦页面加载完毕,就可以在javascript代码中使用这些数据。数据是在post请求后返回的,因此该表单可能位于此处未详细说明的另一页上。您可以轻松地将表单放在同一页面上,并且无需使用javascript重定向,甚至无需使用ajax重定向即可获得更清晰的效果,无需重新加载/查看页面(这将增加google记录的Map加载)
记录集转换为json,并作为javascript变量写入页面。加载Map后,您将遍历此json数据,并为结果中的每条记录添加一个新标记。

  1. <?php
  2. $json=false;
  3. if( isset( $_POST['search'] ) ){
  4. $term='%'.$_POST['term'].'%';
  5. $query='SELECT * from hospitals WHERE hname LIKE ?';
  6. $stmt=$db->prepare( $query );
  7. $stmt->bind_param('s',$term);
  8. $stmt->execute();
  9. $res=$stmt->get_result();
  10. $data=$res->fetch_all( MYSQLI_ASSOC );
  11. $json=json_encode( $data );
  12. }
  13. ?>
  14. <!DOCTYPE html>
  15. <html lang='en'>
  16. <head>
  17. <meta charset='utf-8'>
  18. <title>LabSeek</title>
  19. <link rel='canonical' href='https://getbootstrap.com/docs/5.0/examples/sidebars/'>
  20. <link rel='stylesheet' type='text/css' href='map/map.css' />
  21. <link rel='stylesheet' href='styles/sidebars.css' />
  22. <script src='https://polyfill.io/v3/polyfill.min.js?features=default'></script>
  23. <script>
  24. <?php
  25. #Convert the PHP variable into Javascript variable
  26. printf('const json=%s;',$json ?: '[]');
  27. ?>
  28. function initMap() {
  29. var options = {
  30. zoom:14,
  31. center: { lat: 14.586225342331847, lng: 120.99824317301842 }
  32. }
  33. var map = new google.maps.Map( document.getElementById('map'), options );
  34. function addMarker( coords, args ){
  35. // using `Object.assign` allows you to easily
  36. // add more properties when you call this function
  37. let opts=Object.assign({
  38. position:coords,
  39. map:map
  40. },args);
  41. return new google.maps.Marker(opts);
  42. }
  43. if( json && Object.keys( json ).length > 0 ){
  44. Object.keys( json ).forEach( key=>{
  45. let obj=json[key];
  46. let latlng=new google.maps.LatLng( obj.lat, obj.lng );
  47. let args={
  48. 'Address':obj.Address,
  49. 'Name':obj.hname
  50. };
  51. addMarker(latlng,args)
  52. })
  53. }
  54. }
  55. </script>
  56. </head>
  57. <body>
  58. <div id='map'></div>
  59. <script async defer src='//maps.googleapis.com/maps/api/js?key=AIzaSyBQbnrTvBW0ML5bVlgsgMhYCqdRR04d7k0&callback=initMap'></script>
  60. </body>
  61. </html>
展开查看全部

相关问题