vincentieo
vincentieo

Reputation: 938

requesting details with google places api

I have been reading quite a bit but still a little muddy. I am trying to build an app that gets business listings in google places and displays the 'details' such as address, reviews, image, location, description.

I dont want to perform a search as I already know the business name and location (the user will not be searching but viewing a page navigated to)

I have been following this: https://developers.google.com/maps/documentation/javascript/places#place_details_requests

and using the code form here: Google Places API - Places detail request undefined

function initialize() {
    var pyrmont = new google.maps.LatLng(50.458447,-3.509552);

    map = new google.maps.Map(document.getElementById('map_canvas'), {
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      center: pyrmont,
      zoom: 15
    });

    var request = {
      location: pyrmont,
      radius: 5000,
      types: ['bar']
    };
    infowindow = new google.maps.InfoWindow();
    service = new google.maps.places.PlacesService(map);
    service.search(request, callback);
  }

  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 + "<br />" + place.formatted_address +"<br />" + place.website + "<br />" + place.rating + "<br />" + place.formatted_phone_number);
      infowindow.open(map, this);
    });
  }

HTML:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>
</head>
<body>
    <div id="content">

        <div id="map_canvas">
            map
        </div>

    </div>
</body>

What is the easiest way to perform the get request and have it print in the html for specific business listings?

Upvotes: 2

Views: 1476

Answers (1)

Scott
Scott

Reputation: 3732

I can't help you with the javascript code. But I can tell you that you need to look up your business twice. Once in the Places API to get the unique PlaceID, then a second time in the Details API (using the PlaceID). Store the PlaceID so you never need to perform the first lookup again. Reviews and hours can change over time.

Upvotes: 1

Related Questions