Utilizing Google Maps API to showcase detailed information from Google Places API on an interactive infowindow

I'm a bit uncertain about how to proceed with this task.

What I aim to achieve: I want to showcase the information that one would typically find by searching for the nearest farmers' market on Google Maps.

I desire an Info window that not only provides the address and hours of operation but also includes any other relevant details available through Google Maps for the particular business.

Current situation: Currently, I have an Angular JS code block that populates an array structured like this:

var locations = [
  ['Findlay Market', 39.115398, -84.518481, 5],
  ['Hyde Park Farmers' Market', 39.139601, -84.442496, 4],
  ['Lettuce Eat Well Farmers' Market', 39.166134, -84.611613, 3],
  ['College Hill Farm Market', 39.195641, -84.545453, 2],
  ['Anderson Farmers' Market', 39.078364, -84.350539, 1]
];

Using the Infowindow, I am able to display the name of each farmers' market.

While I understand that I could create a new table in my database to store hours and additional data for each market, and then use this to generate an Angular JS card within the Infowindow... I was hoping there might be a way to leverage the Google Map API along with the Google Places API to access this information directly without the need for storing it locally.

Credit goes to uksz for providing me with the following code snippet which proved useful in fetching the required details:

<!DOCTYPE html>
<html>
    <head>
    <title>Place details</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
        html, body, #map-canvas {
            height: 100%;
            margin: 0px;
            padding: 0px
        }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>
    <script>
    function initialize() {
        var map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: new google.maps.LatLng(39.115398, -84.518481),
    zoom: 15
});

var request = {
    location: map.getCenter(),
    radius: '500',
    query: 'Findlay Market'
};

var service = new google.maps.places.PlacesService(map);
service.textSearch(request, callback);

function callback(results, status) {
    if (status == google.maps.places.PlacesServiceStatus.OK) {
        var marker = new google.maps.Marker({
            map: map,
            place: {
                placeId: results[1].place_id,
                location: results[1].geometry.location
            }
        });
        var infowindow = new google.maps.InfoWindow();
        google.maps.event.addListener(marker, 'click', function() {
            var content = results[1].name + '<br>' + 
                          results[1].formatted_address + '<br>' +
                          results[1].opening_hours + '<br>' +
                          results[1].place_id + '<br>';
            infowindow.setContent(content);
            infowindow.open(map, this);
            });
        }
    }
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
    <body>
        <div id="map-canvas"></div>
    </body>
</html>

Answer №1

It seems like you're asking about displaying information from the Google Places API. One way to do this is by using the getDetails() function provided by Google. You can retrieve an object containing the information you need. If you don't want to keep this object permanently, you could simply delete it whenever the user switches views, ensuring that the data isn't stored in your database for too long.

Hope that helps!

If you'd like more information on the getDetails() function, check out the full manual here:

https://developers.google.com/maps/documentation/javascript/places#place_details_requests

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to share props with all routes in vue.js?

I need to make sure that all components have access to the BASE_URL variable. Here is an example of my App.js: <template> <router-view></router-view> </template> <script> import addJoke from './components/addJoke.vue&ap ...

"Printed within the custom directive, the ng model value shows up as undefined

I am currently in the process of creating a custom directive that involves a simple template consisting of an input type textarea. I am assigning the ng-model to ngmodel and creating a link function where I am trying to capture the value of ngmodel. Howeve ...

Markers on Google Maps in React JS only appear upon page refresh

Greetings for taking the time to review this. I am fetching data through an AJAX call to a geocoder and then to google maps to display markers. The map loads correctly and the data is passed to the markers, but only when I manually refresh the page. I have ...

What causes the v-for in a template to only update when the input text changes?

I'm currently working on a Vue.js code, but I'm facing an issue where the template isn't updating when new data is added to the input text. My goal is for the list to update when the @click event occurs. Visit this link for reference metho ...

Switching the language on Datatables interface

Currently, I am utilizing angular-datatables in my project. Is there a way to change the language of the table interface to another language? Specifically, I am looking to translate terms like "Show entries", "Search:", and "Showing 1 to 10 of 20 entries ...

I am attempting to save the input value to an array in local storage using VUEX. However, my current method is not producing the desired results

I am facing an issue where I am trying to push an input value and store it in an array in the local storage using VUEX. I have created an array in the state for this purpose. Initially, I push the value and then I stringify it to set the value to the sto ...

What steps should I take to make index.html the default page on my http server?

I have set up an AngularJS application in a Node environment. I am using the http-server module to serve the files to the browser. Despite everything working smoothly, I'm struggling to make index.html the default file when the server launches. I hav ...

jwplayer - track viewing time - monetize by the minute - trigger action based on duration

My goal is to track the time duration that someone watches a video, ideally by triggering an action every minute. I'm aiming to create a pay-per-minute system where a credit is withdrawn from the user for each minute they watch. If this setup isn&apo ...

Avoiding Rejected Promise: Warning for Error [ERR_HTTP_HEADERS_SENT] due to Issue with setInterval and Axios.post Error Management

I attempted to address this warning by researching online. Unfortunately, I couldn't find a solution, so I am reaching out with this question. The current warning that I am encountering is: (node:39452) UnhandledPromiseRejectionWarning: Error [ERR_H ...

phpif (the current date is after a certain specific date, do

Can someone please help me solve this problem? I want to prevent echoing a variable if the date has already expired. Currently, my PHP code displays: Match 1 - April 1, 2015 Match 2 - April 8, 2015 What I need is for Match 1 to not be echoed if the cur ...

How to locate every JavaScript event connected with an HTML element by utilizing Google Chrome

Looking for an easy method to identify all JavaScript events linked to a specific HTML element in Chrome? For example: HTML element <a href="#" class="cancel_link refresh_page">Cancel</a> Script: <script type="text/javascript"> $ ...

Searching for a way to sort out role-specific usernames in a dropdown menu using AngularJS?

Is there a way to effectively filter usernames based on specific roles in a dropdown using AngularJS? Hello, I am trying to filter the usernames with the 'KP' role in the dropdown list. Please refer to My plunker for more information. Init ...

Add a specific CSS class to a div element depending on the status of a checkbox

Is there a way to dynamically append data based on the selected items in a checkbox table? I want to be able to add multiple or single items by clicking the add button. The key is to avoid appending duplicate data that already exists in the view. Any sugge ...

StealJS Module Repathing Techniques

There seems to be an issue with my setup, so I welcome all inquiries. I am utilizing an npm package called xrm-mock for a MS CRM mocking framework. Here is how I have configured it: steal.config({ meta: { "dependencyModule": { deps ...

Finding the right way to cancel a Firestore stream within a Vue component using the onInvalidate callback

Currently, I am utilizing Vue 3 to develop a Firebase composable that is responsible for subscribing to an onSnapshot() stream. I have been attempting to unsubscribe from this stream by invoking the returned unsubscribe() function within both watchEffect ...

React Native SectionList Divided by Dates

I have a task to reformat my date object where I have an array of dates in the following format: Object { "FREETIME": "2021-04-19 11:30:00", }, Object { "FREETIME": "2021-04-19 12:00:00", }, Object ...

What are the steps for generating and implementing shared feature files in Cucumber?

I am currently utilizing Cucumber to define some tests for the project I am working on, but as the application grows larger, I find myself in need of a more efficient structure. project | feature_files | | app1.js | | app2.js | | app3.js ...

A contact form overlaying a muted Google Maps view in an HTML website

Currently, I am in the process of developing a website that features a contact form. It would be great if I could incorporate Google Maps as a background, but with a greyed-out effect instead of a plain white page. After trying out a few solutions, I ende ...

The function being called within useEffect is rendering too quickly, causing it to break. However, it functions correctly after

When using React-id-swiper to load images for products, I encountered an issue with setting the useState 'loading' state to false after all images have been successfully loaded. In this case, there are 5 products. To achieve this, I implemented ...

Troubleshooting the Timepicker import issue in ant design version 5.0.3

After updating ant design to version 5.0.3, I encountered the Uncaught Error: Cannot find module 'antd/lib/time-picker/style' at webpackMissingModule issue. Any suggestions on how to resolve this? I am seeking a solution for the error coming fro ...