在谷歌地图上获取所有选定的标记

时间:2016-09-12 12:44:41

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

我对选项GET ALL有疑问。这是流程。

首先,当用户打开应用并想要选择位置时,他有angular-google-maps,所有位置都选择了标记。此外,用户已链接下拉选择(首先需要选择国家/地区,并且我在地图上显示该国家/地区的所有标记,然后根据所选国家/地区,我显示下一个下拉列表,其中包含所选国家/地区的状态,并仅显示该州的标记,以及相同的城市。)

所有标记都有信息框,我在其中放置按钮ADD TO CHART。点击ADD TO CHART时,我会在表格列表中显示带有标记ID的信息。

现在,我希望按钮全部显示并显示列表中的所有ID。但是,取决于所选择的下拉菜单,如果只选择国家并按下全部,我列出国家中的所有标记,如果选择状态并单击全部获取,请给我所有标记在该状态。

这是代码"

 $scope.markers = [];
        var infoWindow = new google.maps.InfoWindow();


        var createMarker = function (info) {
            $scope.selectedbenches = info;
            var marker = new google.maps.Marker({
                map: $scope.map,
                position: new google.maps.LatLng(info.lat, info.lng),
                optimized: false,
                title: info.city,
                icon: 'assets/img/marker.png'

            });

            $scope.allSelectedbenches = function () {
                console.log($scope.selectedbenches);
        };
 $scope.myCartItems = [];

            $scope.addToCart = function (item)
            {
                $http.get(serviceBase + 'control/board/' + item)
                        .success(function (item)
                        {
                            $scope.myCartItems.push(item);

                        });
                $scope.input = '';
                localStorage.setItem('todos', JSON.stringify($scope.myCartItems));
            };

            $scope.remove = function (index) {
                $scope.myCartItems.splice(index, 1);
            };
            google.maps.event.addListener(marker, 'click', (function (marker, $scope) {
                return function () {
                    var compiled = $compile(marker.content)($scope);
                    $scope.$apply();
                    infoWindow.setContent(compiled[0]);
                    infoWindow.open($scope.map, marker);
                };
            })(marker, $scope)
                    );
            $scope.markers.push(marker);
            marker.content = '<div class="iw-container" style="color: #000;">'
                    +
                    '<div class="iw-content">'
                    +
                    '<b>'
                    +
                    'Serial number: '
                    +
                    '</b>'
                    +
                    info.serial_n
                    +
                    '</div>'
                    +
                    '<div class="iw-content">'
                    + '<b>'
                    +
                    'Description: '
                    +
                    '</b>'
                    +
                    info.desc
                    +
                    '<b>'
                    +
                    '</div>'
                    +
                    '<div class="iw-content">'
                    +
                    'Last report: '
                    +
                    '</b>'
                    +
                    info.last_report
                    +
                    '<b>'
                    +
                    '</div>'
                    +
                    '<div class="iw-content">'
                    +
                    'Created: '
                    +
                    '</b>'
                    +
                    info.created_at
                    +
                    '</div>' + '<input type="button" value="Add bench" ng-model="input" ng-click="addToCart(' + info.id + ')"/>'
                    +
                    '<div class="iw-content">'
                    +
                    '<img border="0" float="left" src="https://pbs.twimg.com/profile_images/691936998156804096/iveb6l4U.png">'
                    +
                    '<img border="0" float="right" src="http://www.fibrexgroup.com/images/Products/SSB001-main.jpg">'
                    +
                    '</div>'
                    +
                    '</div>'
                    +
                    '<div class="iw-title" style="color: #000;">' + '<h2>' + info.city_name + '</h2>' + '</div>' + marker.content;

        }

        for (i = 0; i < lokacije.length; i++) {
            createMarker(lokacije[i]);
        }

        $scope.openInfoWindow = function (e, selectedMarker) {
            e.preventDefault();
            google.maps.event.trigger(selectedMarker, 'click');
        }




        $scope.clearMarkers = function () {
            for (var i = 0; i < $scope.markers.length; i++) {
                $scope.markers[i].setMap(null);
            }
            $scope.markers.length = 0;
        }


 //Country filter
        $scope.filterMarkersCountry = function () {
            //1.select filtered country
            var filteredCountries;
            var selectedCountry = $scope.country;
            if (selectedCountry == '0') {
                filteredCountries = lokacije;
            } else {
                filteredCountries = lokacije.filter(function (c) {
                    if (c.country_id == selectedCountry.id)
                        return c;
                });
            }
            //2.update markers on map
            $scope.clearMarkers();
            if (filteredCountries.length > 0) {
                for (i = 0; i < filteredCountries.length; i++) {
                    createMarker(filteredCountries[i]);
                }
            }
        };
 //State filter
        $scope.filterMarkersState = function () {
            //1.select filtered cities
            var filteredStates;
            var selectedStates = $scope.state;
            if (selectedStates == '0') {
                filteredStates = lokacije;
            } else {
                filteredStates = lokacije.filter(function (c) {
                    if (c.state_id == selectedStates.id)
                        return c;
                });
            }
            //2.update markers on map
            $scope.clearMarkers();
            if (filteredStates.length > 0) {
                for (i = 0; i < filteredStates.length; i++) {
                    createMarker(filteredStates[i]);
                }
            }
        };
        //City filter
        $scope.filterMarkersCity = function () {
            //1.select filtered cities
            var filteredCities;
            var selectedCities = $scope.city;
            if (selectedCities == '0') {
                filteredCities = lokacije;
            } else {
                filteredCities = lokacije.filter(function (c) {
                    if (c.city_id == selectedCities.id)
                        return c;
                });
            }
            //2.update markers on map
            $scope.clearMarkers();
            if (filteredCities.length > 0) {
                for (i = 0; i < filteredCities.length; i++) {
                    createMarker(filteredCities[i]);
                }
            }
        };

    });

这一切都很好,就像我期望的那样。唯一的问题是获取所有按钮。 这是html

<div class="narudzbe_lokacija">
                    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
                        <h5>Odabir lokacije</h5>
                        <div>
                            <label class="control-label col-lg-12 col-md-12 col-sm-12 col-xs-12">Country: </label>
                            <select id="country" class="form-control" ng-model="country" ng-options="country as country.name for country in countries track by country.id" ng-change="getState(); filterMarkersCountry()">
                                <option value='country_id'>{{country.name}}</option>
                            </select>
                        </div>
                        <div>
                            <label class="control-label col-lg-12 col-md-12 col-sm-12 col-xs-12">States</label>
                            <select id="state" class="form-control" ng-disabled="!country" ng-model="state" ng-options="state as state.name for state in states track by state.id" ng-change="getCity(); filterMarkersState()">
                                <option value='state_id'>{{state.name}}</option>
                            </select>
                        </div>
                        <div>
                            <label class="control-label col-lg-12 col-md-12 col-sm-12 col-xs-12">City</label>
                            <select id="city" class="form-control" ng-disabled="!state" ng-model="city" ng-options="city as city.name for city in cities track by city.id" ng-change="filterMarkersCity()">
                                <option value='city_id'>{{city.name}}</option></select>        
                        </div>
                        <button ng-click="allSelectedbenches()">Get all</button>

                        <br />
                        <br />
                    </div> 

我尝试制作plunker,但我的应用程序有许多安宁,并且很难做出实例

1 个答案:

答案 0 :(得分:0)

这是我的解决方案。

$scope.myCartItems = [];
            $scope.allSelectedbenches = function () {

                if($scope.filteredCities !== undefined){
                    $scope.myCartItems = $scope.filteredCities;
                }
                if($scope.filteredStates !== undefined){
                   $scope.myCartItems = $scope.filteredStates;

                }
                if($scope.filteredCountries !== undefined){
                    $scope.myCartItems = $scope.filteredCountries;

                } 
        };

检查是否未定义,并显示所选的下拉框。这里唯一的问题是,在第一次下拉时我需要选择国家,州和城市。 $ scope.myCartItems总是从国家返回一切...... Thnx