3

我正在使用Angular Google Maps来获取带有搜索框的地图。

我正在尝试将搜索限制为“地址”和国家/地区(法国),因此我想使用文档中指定的搜索框指令的“自动完成”选项。

问题是autocomplete:true我的代码中的位确实有效地限制了搜索,但它阻止了事件的触发(地图和标记没有得到更新)。但是控制台没有错误。

但是,如果我注释掉或删除autocomplete:true,则事件触发(地图和标记被刷新),但搜索不限于法国和地址......

这是我的代码:

        var events = {
            places_changed:function (searchBox) {
                var place = searchBox.getPlaces();
                if (!place || place == 'undefined' || place.length == 0) {
                    console.log('no place data :(');
                    return;
                }

                // refresh the map
                $scope.map = {
                    center:{
                        latitude:place[0].geometry.location.lat(),
                        longitude:place[0].geometry.location.lng()
                    },
                    zoom:10
                };

                // refresh the marker
                $scope.marker = {
                    id:0,
                    options:{ draggable:false },
                    coords:{
                        latitude:place[0].geometry.location.lat(),
                        longitude:place[0].geometry.location.lng()
                    }
                };

            }
        };

        $scope.searchbox = {
            template:'searchbox.tpl.html',
            events:events,
            options:{
                autocomplete:true,
                types:['address'],
                componentRestrictions:{
                    country:'fr'
                }
            }
        };

在 html 文件中:

    <div style="height: 500px;">
        <script type="text/ng-template" id="searchbox.tpl.html">
            <input type="text"
                   placeholder="{{'searchbox.google.maps' | text}}"
                   style="width:270px; height:30px;">
        </script>

        <ui-gmap-google-map center='map.center' zoom='map.zoom'>

            <!--SEARCHBOX-->
            <ui-gmap-search-box template="searchbox.template"
                                events="searchbox.events"
                                position="BOTTOM_RIGHT"
                                options="searchbox.options"></ui-gmap-search-box>
            <!--MARKER-->
            <ui-gmap-marker coords="marker.coords" 
                            options="marker.options" 
                            events="marker.events"
                            idkey="marker.id">
            </ui-gmap-marker>

        </ui-gmap-google-map>

    </div>

我究竟做错了什么 ?有没有办法获得对搜索和触发事件的限制?

先感谢您 !

4

1 回答 1

7

好的,我想通了,所以我回答我自己的问题,以防其他人被卡住!

如果autocomplete:true,则不是places_changedand searchBox.getPlaces(),而是place_changedand searchBox.getPlace()。而且我们没有得到一系列位置,而只有一个位置

这是一个工作代码:

  var events = {
        place_changed:function (searchBox) {
            var place = searchBox.getPlace();
            if (!place || place == 'undefined') {
                console.log('no place data :(');
                return;
            }

            // refresh the map
            $scope.map = {
                center:{
                    latitude:place.geometry.location.lat(),
                    longitude:place.geometry.location.lng()
                },
                zoom:10
            };

            // refresh the marker
            $scope.marker = {
                id:0,
                options:{ draggable:false },
                coords:{
                    latitude:place.geometry.location.lat(),
                    longitude:place.geometry.location.lng()
                }
            };

        }
    };
于 2015-08-17T17:10:47.037 回答