Angular google maps搜索框指令:尝试限制搜索时不会触发事件(使用自动完成选项)

时间:2015-08-15 11:26:19

标签: angularjs google-maps autocomplete search-box

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

我正在尝试将搜索限制为“地址”和国家(法国),因此我想使用搜索框指令的“自动填充”选项,如{{3}中所指定的那样}。

问题是我的代码中的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>

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

提前谢谢!

1 个答案:

答案 0 :(得分:7)

好的,我明白了,所以我回答了我自己的问题,万一其他人被卡住了!

如果autocomplete:true,那么它不是places_changedsearchBox.getPlaces(),而是place_changedsearchBox.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()
                }
            };

        }
    };