google 地图 api 无法读取 placesservice

google maps api cannot read placesservice

我在使用 Google 的地图 API 及其 PlacesService 时遇到问题。即使我已经正确加载了地点库,它仍然显示 "Cannot read property 'PlacesService' of undefined"。地图本身可以工作并加载。有任何想法吗?这是代码:

<div id="map-canvas"></div>

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3&libraries=place‌s"></script>
<script type="text/javascript">
    var myLatlng;
    var map;
    var marker;

    function initialize() {
        myLatlng = new google.maps.LatLng(fooLat, fooLng);

        var mapOptions = {
            zoom: 17,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            scrollwheel: false,
            draggable: true
        };

        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        var service = new google.maps.places.PlacesService(map);

        var request = { placeId: 'fooPlaceId'};
        service.getDetails(request, callback);

        function callback (place, status) {
            if (status == google.maps.places.PlacesServiceStatus.OK) {
                marker = new google.maps.Marker({
                    position: place.position,
                    map: map,
                    title: place.name
                });
            }
        };
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

更新: 我实际上只是尝试了一段代码 from Google itself,它给了我同样的错误。

答案如下:我直接从某个页面(此处、Whosebug 或 Google 的 API 示例页面之一)复制了 include 行。问题似乎在于,无论出于何种原因,它都有一个不可见的字符在我的 CMS 编辑器没有选择的库的名称中。所以复制时要小心!

此示例需要 Places 库。当您第一次使用 API 键加载 API 时,包括 libraries=places 参数。

例如:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">