PlacesService and the need of html node

雨燕双飞 提交于 2019-12-22 10:57:28

问题


<html>
    <head>
        <script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places&language=pt-PT" type="text/javascript"></script>
        <script type="text/javascript">
            function initialize() {
                var request = {
                    placeId: 'ChIJO_PkYRozGQ0R0DaQ5L3rAAQ'
                };

                service = new google.maps.places.PlacesService(document.getElementById('places'));
                service.getDetails(request, callback);

                function callback(place, status) {
                    if (status == google.maps.places.PlacesServiceStatus.OK) {
                        console.log(place);
                    }
                }
            }
            google.maps.event.addDomListener(window, 'load', initialize);
        </script>
    </head>

    <body>  <div id="places"></div> </body>

</html>

I have this code that works well, but i don't know why i need to use

PlacesService(document.getElementById('some id')); 

If i use only PlacesService(); I will get an error. The html_attributions is none: html_attributions: Array[0]length: 0

So, is my code fine, or what should I do?

http://jsfiddle.net/c6p14g4d/


回答1:


(If you haven't figured it out 6 months later) Your code will work and should adhere to Google's policies surrounding the PlacesService.

From my understanding, "attributions" are some sort of information attributing the source of the information to a particular partner, for example, a review from Yelp (I don't know if they partner with Yelp, but that's the idea). Obviously the partner wants the end consumer of the information to know who provided it.

The idea is that when you perform a search with the PlacesService, if there is an attached map, it will render "attributions" automatically. Thus, if you don't have a map, the library will attempt to render the attributions into an html node. Hiding that node (or the map) is against the policy.

It's important to note that the html_attributions array might not always be empty in all cases when using the library, but as long as you provide a visible node in the DOM, you're fine, since the library automatically populates the node with attributions if they are there.




回答2:


From the documentation, that needs to be either a HTMLDivElement or a Map:

Constructor                                     Description
PlacesService(attrContainer:HTMLDivElement|Map) Creates a new instance of the PlacesService that renders attributions in the specified container.


来源:https://stackoverflow.com/questions/27990005/placesservice-and-the-need-of-html-node

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!