Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- MY PHP CODE :
- <?php
- require_once('func.php');
- if(isset($_POST['Search']))
- {
- if (!empty($_POST['location']))
- {
- $apikey='AIzaSyBNuDJIavmQNKgu1hFtXQGUfnXGqUiBxPg';
- $lat="";
- $lng="";
- GetLocation($lat,$lng);
- $maps_url = 'https://'.'maps.googleapis.com/'.'maps/api/place/autocomplete/json'.'?input='.urlencode($_POST['location']).'&types=establishment&'.'location='.$lat.','.$lng.'key='.$apikey;
- $maps_json = file_get_contents($maps_url);
- $maps_array = json_decode($maps_json, true);
- }
- else
- echo "<script type='text/javascript'>alert('Please Enter A Valid Input');</script>";
- }
- ?>
- MY HTML CODE :
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8"/>
- <title>Google Places Search</title>
- </head>
- <body>
- <h1> Search A location in Google Maps : </h1>
- <form action="test.php" method="POST">
- <input type="text" name="location"/>
- <button name ='Search' type='submit'>Search</button>
- </form>
- <br/>
- <div id="googleMap" style="width:100%;height:400px;"></div>
- <script>
- function myMap()
- {
- var mapProp=
- {
- center:new google.maps.LatLng(<?php echo $lat;?>,<?php echo $lng;?>);
- }
- var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
- }
- </script>
- </body>
- <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBNuDJIavmQNKgu1hFtXQGUfnXGqUiBxPg&callback=myMap"></script>
- </html>
- MY func.php CODE :
- <?php
- function GetLocation($lat,$lng)
- {
- $maps_url = 'https://' .
- 'maps.googleapis.com/' .
- 'maps/api/geocode/json' .
- '?address=' . urlencode($_POST['location']);
- $maps_json = file_get_contents($maps_url);
- $maps_array = json_decode($maps_json, true);
- $lat = $maps_array['results'][0]['geometry']['location']['lat'];
- $lng = $maps_array['results'][0]['geometry']['location']['lng'];
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement