Advertisement
Bedhoel

Nearby Place

Sep 21st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.74 KB | None | 0 0
  1. <?php
  2. $lat     = $_REQUEST['lat'];
  3. $long    = $_REQUEST['long'];
  4. $radius  = (empty($_REQUEST['radius']) ? '500' : $_REQUEST['radius']);       //meter
  5. $zoom    = (empty($_REQUEST['zoom']) ? '17' : $_REQUEST['zoom']);           // 1 to 22
  6. $place   = (empty($_REQUEST['place']) ? 'hospital' : $_REQUEST['place']);  // 1 to 22
  7. ?>
  8. <!DOCTYPE html>
  9. <html>
  10.   <head>
  11.     <title>Place Searches</title>
  12.     <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
  13.     <meta charset="utf-8">
  14.     <style>
  15.       /* Always set the map height explicitly to define the size of the div
  16.        * element that contains the map. */
  17.       #map {
  18.         height: 100%;
  19.       }
  20.       /* Optional: Makes the sample page fill the window. */
  21.       html, body {
  22.         height: 100%;
  23.         margin: 0;
  24.         padding: 0;
  25.       }
  26.     </style>
  27.     <script>
  28.       // This example requires the Places library. Include the libraries=places
  29.       // parameter when you first load the API. For example:
  30.       // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
  31.  
  32.       var map;
  33.       var infowindow;  
  34.            
  35.       function initMap() {
  36.         var pyrmont = {lat: <?php echo $lat ?>, lng: <?php echo $long ?>};
  37.         map = new google.maps.Map(document.getElementById('map'), {
  38.           center: pyrmont,
  39.           zoom: <?php echo $zoom ?>
  40.         });
  41.  
  42.         infowindow = new google.maps.InfoWindow();
  43.         var service = new google.maps.places.PlacesService(map);
  44.         service.nearbySearch({
  45.           location: pyrmont,
  46.           radius: <?php echo $radius ?>,
  47.           type: ['<?php echo $place ?>']
  48.         }, callback);
  49.       }
  50.  
  51.       function callback(results, status) {
  52.         if (status === google.maps.places.PlacesServiceStatus.OK) {
  53.           for (var i = 0; i < results.length; i++) {
  54.             createMarker(results[i]);
  55.           }
  56.         }
  57.       }
  58.  
  59.       function createMarker(place) {
  60.         var placeLoc = place.geometry.location;
  61.         var marker = new google.maps.Marker({
  62.           map: map,
  63.           position: place.geometry.location
  64.         });
  65.  
  66.         google.maps.event.addListener(marker, 'click', function() {
  67.           infowindow.setContent(
  68.             place.name);
  69.           infowindow.open(map, this);
  70.           var myloc = place.geometry.location.toString();
  71.           var location = myloc.slice(1, -1);
  72.           var arr = location.split(',');
  73.           var latitute   =  arr[0];
  74.           var longtitude =  arr[1];
  75.         });
  76.       }
  77.    
  78.     </script>
  79.   </head>
  80.   <body>
  81.     <div id="map"></div>
  82.     <script async defer src="https://maps.googleapis.com/maps/api/js?callback=initMap&libraries=places&fields=photos,formatted_address,name,rating,opening_hours,geometry&key=YOUR_API_KEY"></script>
  83.   </body>
  84. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement