Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Map API to find nearby restaurant around my place

I am working on Google map api [Here is my to find out near by restaurant. . . When I run this, I get only current location. I am not receiving any information related to restaurant data.

<script>

      function initMap() {
          var map = new google.maps.Map(document.getElementById('map'), {
              center: { lat: 25.276987, lng: 55.296249 },
              zoom: 15
          });

          var infoWindow = new google.maps.InfoWindow({ map: map });

          if (navigator.geolocation) {
              navigator.geolocation.getCurrentPosition(function (position) {

                  var pos = {
                      lat: position.coords.latitude,
                      lng: position.coords.longitude     
                  };
                  infoWindow.setPosition(pos);
                  infoWindow.setContent('Location found.');
                  map.setCenter(pos);
                  var marker = new google.maps.Marker({
                      map: map,
                      position: pos.getPlace().location
                  });
                  var service = new google.maps.places.PlacesService(map);
                  service.nearbySearch({
                      location: rak,
                      radius: 5500,
                      type: ['restaurant'] // not resturant
                  }, callback);

              }, function () {
                  handleLocationError(true, infoWindow, map.getCenter());
              });
          }
          else {            
              handleLocationError(false, infoWindow, map.getCenter());
          }
      }
like image 610
Orion Rigel Avatar asked Mar 26 '16 13:03

Orion Rigel


1 Answers

Maybe you are encountering an error with the nearbySearch location tag. You didn't initiate the variable rak and it will return a null value.

Set the Script

<script
    src="https://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=places&callback=initMap"
    async defer></script>

Initialize you variables

var map;
var infowindow;
var myPlace = {lat: 25.276987, lng: 55.296249 };

Call your nearBySearch

var service = new google.maps.places.PlacesService(map);
            service.nearbySearch({
                location : myPlace,
                radius : 5500,
                type : [ 'restaurant' ]
            }, callback);

Check the result of search and then create a marker for each found location

function callback(results, status) {
            if (status === google.maps.places.PlacesServiceStatus.OK) {
                for (var i = 0; i < results.length; i++) {
                    createMarker(results[i]);
                }
            }
        }

function createMarker(place) {
            var placeLoc = place.geometry.location;
            var marker = new google.maps.Marker({
                map : map,
                position : place.geometry.location
            });

            google.maps.event.addListener(marker, 'click', function() {
                infowindow.setContent(place.name);
                infowindow.open(map, this);
            });
        }

You can find this code in this document, it will explain well the proper coding and how to use the api. Also here is the list of supported location type.

I hope this helps.

like image 197
Mr.Rebot Avatar answered Sep 17 '22 07:09

Mr.Rebot