Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1.  
  2. MY PHP CODE :
  3.  
  4. <?php
  5.  
  6. require_once('func.php');
  7.  
  8. if(isset($_POST['Search']))
  9. {
  10.  
  11. if (!empty($_POST['location']))
  12. {
  13.  
  14. $apikey='AIzaSyBNuDJIavmQNKgu1hFtXQGUfnXGqUiBxPg';
  15. $lat="";
  16. $lng="";
  17.  
  18. GetLocation($lat,$lng);
  19. $maps_url = 'https://'.'maps.googleapis.com/'.'maps/api/place/autocomplete/json'.'?input='.urlencode($_POST['location']).'&types=establishment&'.'location='.$lat.','.$lng.'key='.$apikey;
  20. $maps_json = file_get_contents($maps_url);
  21. $maps_array = json_decode($maps_json, true);
  22.  
  23.  
  24. }
  25.  
  26.  
  27. else
  28.  
  29. echo "<script type='text/javascript'>alert('Please Enter A Valid Input');</script>";
  30.  
  31. }
  32.  
  33. ?>
  34.  
  35. MY HTML CODE :
  36.  
  37. <!DOCTYPE html>
  38. <html lang="en">
  39. <head>
  40. <meta charset="utf-8"/>
  41. <title>Google Places Search</title>
  42. </head>
  43. <body>
  44.  
  45. <h1> Search A location in Google Maps : </h1>
  46. <form action="test.php" method="POST">
  47. <input type="text" name="location"/>
  48. <button name ='Search' type='submit'>Search</button>
  49. </form>
  50. <br/>
  51.  
  52.  
  53. <div id="googleMap" style="width:100%;height:400px;"></div>
  54.  
  55. <script>
  56. function myMap()
  57. {
  58. var mapProp=
  59. {
  60. center:new google.maps.LatLng(<?php echo $lat;?>,<?php echo $lng;?>);
  61.  
  62.  
  63. }
  64.  
  65. var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
  66. }
  67.  
  68.  
  69. </script>
  70. </body>
  71. <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBNuDJIavmQNKgu1hFtXQGUfnXGqUiBxPg&callback=myMap"></script>
  72. </html>
  73.  
  74. MY func.php CODE :
  75.  
  76. <?php
  77.  
  78. function GetLocation($lat,$lng)
  79. {
  80.  
  81. $maps_url = 'https://' .
  82. 'maps.googleapis.com/' .
  83. 'maps/api/geocode/json' .
  84. '?address=' . urlencode($_POST['location']);
  85. $maps_json = file_get_contents($maps_url);
  86. $maps_array = json_decode($maps_json, true);
  87. $lat = $maps_array['results'][0]['geometry']['location']['lat'];
  88. $lng = $maps_array['results'][0]['geometry']['location']['lng'];
  89.  
  90. }
  91. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement