【问题标题】:Angular Google Maps Directive zoom too close after "place_changed" event“place_changed”事件后,Angular Google Maps Directive 缩放太近
【发布时间】:2015-10-14 21:45:03
【问题描述】:

我目前正在为 DHL 开发一个商店查找器应用程序,可以在 storefinder.hashfff.com/app/index.html 上查看它

在这个应用程序中,我使用了 angular-google-maps 库,它提供了一些简洁的功能,尽管我认为直接使用 Google Maps API 会是一个更好的选择,因为 Googles API 文档更详细,但是, Angular 新手,我认为它会有所帮助。

我的搜索框与名为“place_changed”的事件侦听器相关联,该侦听器在设置自动完成后触发,该自动完成将自动完成作为参数。

events: {
            place_changed: function(autocomplete) {

                var searchString = autocomplete.gm_accessors_.place.Sc.formattedPrediction;
                var searchCountry = searchString.split(',').pop().trim(); 
                var searchCity = searchString.split(',');
                var jsonQuery = "http://dhl.hashfff.com/api/dhl_store_finder_api.php/?country=" + searchCountry;

                // Filter search results by search term. City, Address or Country
                $.getJSON(jsonQuery , function(data) {
                    $scope.$apply(function() {
                        $scope.stores = _.filter(data, function(search) {
                            console.log(search.address2.toLowerCase().indexOf(searchCity[0].toLowerCase()));
                            return search.city.toLowerCase() == searchCity[0].toLowerCase() || search.address2.toLowerCase().indexOf(searchCity[0].toLowerCase()) > -1 || search.country.toLowerCase().indexOf(searchCity[0].toLowerCase()) > -1;
                        });
                        $('.cd-panel-search').addClass('is-visible');
                    });
                });



                place = autocomplete.getPlace();

                if (place.address_components) {
                    // For each place, get the icon, place name, and location.
                    newMarkers = [];
                    var bounds = new google.maps.LatLngBounds();

                    var marker = {
                      id:place.place_id,
                      place_id: place.place_id,
                      name: place.address_components[0].long_name,
                      latitude: place.geometry.location.lat(),
                      longitude: place.geometry.location.lng(),
                      options: {
                        visible:false
                      },
                      templateurl:'window.tpl.html',
                      templateparameter: place
                    };

                    newMarkers.push(marker);

                    bounds.extend(place.geometry.location);

                    $scope.map.bounds = {
                        northeast: {
                          latitude: bounds.getNorthEast().lat(),
                          longitude: bounds.getNorthEast().lng()
                        },
                        southwest: {
                          latitude: bounds.getSouthWest().lat(),
                          longitude: bounds.getSouthWest().lng()
                        }
                    }

                    _.each(newMarkers, function(marker) {
                        marker.closeClick = function() {
                          $scope.selected.options.visible = false;
                          marker.options.visble = false;
                          return $scope.$apply();
                        };
                        marker.onClicked = function() {
                          $scope.selected.options.visible = false;
                          $scope.selected = marker;
                          $scope.selected.options.visible = true;
                        };
                    });

                    $scope.map.markers = newMarkers;
                }
            }
        }

发生的情况是,在自动完成触发后,它会转到搜索到的位置,但缩放设置为太接近的最大值。我知道 map.setZoom(5) 是通常的答案,但我在此事件侦听器中没有可用的地图对象。

我希望有人对 Google Maps Angular 指令有经验并可以帮助我。如果您需要任何其他代码,我很乐意更新查询。

【问题讨论】:

    标签: javascript angularjs google-maps-api-3 zooming


    【解决方案1】:

    边界的创建是没有用的,因为当 LatLngBounds 确实具有相同的 NE 和 SW 时(在您的示例中就是这种情况,因为您只有一个地点/位置),您可以简单地设置地图的中心:

                $scope.map.center={
                              latitude: bounds.getNorthEast().lat(),
                              longitude: bounds.getNorthEast().lng()
                            };
    

    区别:地图的缩放不会被修改(就像使用 fitBounds 时一样)

    当你想设置缩放时,例如:

    $scope.map.zoom=5;
    

    【讨论】:

    • 太棒了!谢谢你。 :)
    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    相关资源
    最近更新 更多