【问题标题】:Drop Down Menu and Google Maps下拉菜单和谷歌地图
【发布时间】:2015-11-11 21:20:36
【问题描述】:

我有一个下拉菜单来显示不同的 Google 地图。 Dining 地图出现了,但是当我切换到另一张地图时,比如 Parks,它会在左上角显示一个带有标记图标的灰色框,并且您会在其下方看到另一张地图的 1 秒视图/闪烁。当我在每个“邻居列表”类中添加“可见”时,它会起作用,但是会同时显示多张地图,而不是只显示一张。

HTML

   <div class="explore">
    <h4>Explore Your New Neighborhood</h4>
    <select id="neighborhood-select">
      <option value="Dining">Dining</option>
      <option value="Shopping">Shopping</option>
      <option value="Schools">Schools</option>
      <option value="Parks">Parks</option>
      <option value="Hospitals">Hospitals</option>
      <option value="Colleges">Colleges</option>
    </select>
  </div>
  <div class="explore-images featured-image">
    <div class="neighborhood-listing Dining visible">
      <div class="neighborhood-btns">
       <div id="map-dining"></div>
      </div>
    </div>
    <div class="neighborhood-listing Shopping ">
      <div class="neighborhood-btns">
        <div id="map-shopping"></div>
      </div>
    </div> 
    <div class="neighborhood-listing Parks ">
      <div class="neighborhood-btns">
        <div id="map-parks"></div>
      </div>
    </div> 
    <div class="neighborhood-listing Hospitals ">
      <div class="neighborhood-btns">
        <div id="map-hospitals"></div>
      </div>
    </div> 
    <div class="neighborhood-listing Colleges ">
      <div class="neighborhood-btns">
        <div id="map-colleges"></div>
      </div>
    </div>

    <div class="neighborhood-listing Schools ">
      <div class="neighborhood-btns"> 
      <div id="map-schools"></div>
      </div>
    </div>
  </div>

CSS

.explore-images .neighborhood-listing,
.explore-images .leasing-plan {
display: none;
}
.explore-images .neighborhood-listing.visible,
.explore-images .leasing-plan.visible {
display: block;
}

.explore {
padding: 2.5% 5%;
background-color: #f2efef;
}
.explore h4 {
margin-top: 0;
margin-bottom: .5em;
}

/* .directory-btns, */
.neighborhood-btns {
position: relative;
top: 0;
right: 0;
}

@media screen and (max-width: 480px) {
.desktop {
    display: none;
}
.mobile {
    display: block;
}
}

#map-dining, #map-shopping, #map-schools, #map-parks, #map-hospitals, #map-colleges{
width:100%;
height: 400px;
}

我希望从下拉菜单中选择每张地图时都能正确显示(如“餐饮”),并防止出现另一张地图的 1 秒视图(在过渡期间),基本上让它顺利过渡到下一张选定的地图。

链接到JS Fiddle


已解决

我真的很喜欢 Godwin 的 jsfiddle 的平滑过渡,只要调整大小也一样平滑!我也非常喜欢 geocode zip 的 jsfiddle 的工作方式,调整大小非常完美!只是过渡不太顺利,因为在过渡期间它仍然有米色的盒子视图,左上角有标记。所以我所做的就是将这两个代码结合起来,这几乎就是我想要的。非常感谢大家,我真的很感谢你们的帮助!

使用工作代码编辑 JS Fiddle: https://jsfiddle.net/sf82/fbt9p701/1/

CSS 更改:

.explore-images .neighborhood-listing,
.explore-images .leasing-plan {
    visibility: hidden;
    position: absolute;
    height: 400px;
    width: 100%;
}
.explore-images .neighborhood-listing.visible,
.explore-images .leasing-plan.visible {
    display: block;         
    visibility: visible;
}

添加了JS:

jQuery(document).ready(function ($) {
$('#leasing-select, #neighborhood-select, #store-directories-select').change(function () {
    var new_image = $(this).val();
    $('.explore-images .visible').fadeOut().removeClass('visible');
    $('.explore-images .' + new_image).fadeIn(function () {
        resizeMap(new_image);
    }).addClass('visible');

});
});

function resizeMap(type) {
var typeL = type.toLowerCase();
switch (typeL) {
    case 'dining':
        google.maps.event.trigger(map, 'resize');
        map.setCenter(mapOptions.center);
        break;
    case 'shopping':
        google.maps.event.trigger(map2, 'resize');
        map2.setCenter(mapOptions.center);
        break;
    case 'schools':
        google.maps.event.trigger(map3, 'resize');
        map3.setCenter(mapOptions.center);
        break;
    case 'parks':
        google.maps.event.trigger(map4, 'resize');
        map4.setCenter(mapOptions.center);
        break;
    case 'hospitals':
        google.maps.event.trigger(map5, 'resize');
        map5.setCenter(mapOptions.center);
        break;
    case 'colleges':
        google.maps.event.trigger(map6, 'resize');
        map6.setCenter(mapOptions.center);
        break;
    default:
        break;
}
}

【问题讨论】:

    标签: javascript html css google-maps


    【解决方案1】:

    如果目标元素是 display: none,Google 地图将无法正确呈现。您可以通过使用visibilityposition: absolute 属性来解决此问题:http://jsfiddle.net/dd8q234b/7/。但是,这仍然存在调整大小的问题。

    .explore-images .neighborhood-listing,
    .explore-images .leasing-plan {
        visibility: hidden;
        position: absolute;
        height: 400px;
        width: 100%;
    }
    .explore-images .neighborhood-listing.visible,
    .explore-images .leasing-plan.visible {
        visibility: visible;
    }
    

    更好的解决方案可能是使用相同的地图,但在选择更改时重置标记:https://developers.google.com/maps/documentation/javascript/examples/marker-remove

    【讨论】:

      【解决方案2】:

      您是否在 div 中切换类以使它们可见/不可见?

      我遇到了同样的问题,我发现如果加载到不可见元素中,地图将无法正确呈现。 我通过在显示 div 之后初始化地图解决了这个问题,因此您可以在切换类时尝试初始化地图(或者您所做的任何使 div 可见/不可见的操作)。

      【讨论】:

        【解决方案3】:

        你需要在地图显示后触发'resize'事件,然后重置地图中心:

        jQuery(document).ready(function ($) {
            $('#leasing-select, #neighborhood-select, #store-directories-select').change(function () {
                var new_image = $(this).val();
                $('.explore-images .visible').fadeOut().removeClass('visible');
                $('.explore-images .' + new_image).fadeIn(function () {
                    resizeMap(new_image);
                }).addClass('visible');
        
            });
        });
        
        function resizeMap(type) {
            var typeL = type.toLowerCase();
            switch (typeL) {
                case 'dining':
                    google.maps.event.trigger(map, 'resize');
                    map.setCenter(mapOptions.center);
                    break;
                case 'shopping':
                    google.maps.event.trigger(map2, 'resize');
                    map2.setCenter(mapOptions.center);
                    break;
                case 'schools':
                    google.maps.event.trigger(map3, 'resize');
                    map3.setCenter(mapOptions.center);
                    break;
                case 'parks':
                    google.maps.event.trigger(map4, 'resize');
                    map4.setCenter(mapOptions.center);
                    break;
                case 'hospitals':
                    google.maps.event.trigger(map5, 'resize');
                    map5.setCenter(mapOptions.center);
                    break;
                case 'colleges':
                    google.maps.event.trigger(map6, 'resize');
                    map6.setCenter(mapOptions.center);
                    break;
                default:
                    break;
            }
        }
        

        proof of concept fiddle

        注意:我必须从 initialize 函数中删除“地图”变量的本地定义,并删除您的“地图调整大小”事件侦听器。

        代码 sn-p:

        jQuery(document).ready(function($) {
          $('#leasing-select, #neighborhood-select, #store-directories-select').change(function() {
            var new_image = $(this).val();
            $('.explore-images .visible').fadeOut().removeClass('visible');
            $('.explore-images .' + new_image).fadeIn(function() {
              resizeMap(new_image);
            }).addClass('visible');
        
          });
        });
        
        function resizeMap(type) {
          var typeL = type.toLowerCase();
          switch (typeL) {
            case 'dining':
              google.maps.event.trigger(map, 'resize');
              map.setCenter(mapOptions.center);
              break;
            case 'shopping':
              google.maps.event.trigger(map2, 'resize');
              map2.setCenter(mapOptions.center);
              break;
            case 'schools':
              google.maps.event.trigger(map3, 'resize');
              map3.setCenter(mapOptions.center);
              break;
            case 'parks':
              google.maps.event.trigger(map4, 'resize');
              map4.setCenter(mapOptions.center);
              break;
            case 'hospitals':
              google.maps.event.trigger(map5, 'resize');
              map5.setCenter(mapOptions.center);
              break;
            case 'colleges':
              google.maps.event.trigger(map6, 'resize');
              map6.setCenter(mapOptions.center);
              break;
            default:
              break;
          }
        }
        
        var map, map2, map3, map4, map5, map6;
        var mapOptions;
        
        function initialize() {
          var myCenter = new google.maps.LatLng(39.458342, -74.633672);
        
        
          var locations = [
            ['Cavallino Nero', 39.456832, -74.660559, 2],
            ['IHOP', 39.453284, -74.651666, 3],
            ['Longhorn Steakhouse', 39.453224, -74.641467, 4],
            ['Chilis Grill and Bar', 39.450375, -74.639798, 5],
            ['Applebees', 39.452392, -74.631073, 6]
          ];
        
          var locationsA = [
            ['Cavallino Nero', 39.456832, -74.660559, 2],
            ['IHOP', 39.453284, -74.651666, 3],
            ['Longhorn Steakhouse', 39.453224, -74.651666, 4],
            ['Chilis Grill and Bar', 39.450375, -74.639798, 5],
            ['Applebees', 39.452392, -74.631073, 6]
          ];
        
          var locationsB = [
            ['Cavallino Nero', 39.456832, -74.660559, 2],
            ['IHOP', 39.453284, -74.651666, 3],
            ['Longhorn Steakhouse', 39.453224, -74.651666, 4],
            ['Chilis Grill and Bar', 39.450375, -74.639798, 5],
            ['Applebees', 39.452392, -74.631073, 6]
          ];
        
          var locationsC = [
            ['Cavallino Nero', 39.456832, -74.660559, 2],
            ['IHOP', 39.453284, -74.651666, 3],
            ['Longhorn Steakhouse', 39.453224, -74.651666, 4],
            ['Chilis Grill and Bar', 39.450375, -74.639798, 5],
            ['Applebees', 39.452392, -74.631073, 6]
          ];
        
          var locationsD = [
            ['Cavallino Nero', 39.456832, -74.660559, 2],
            ['IHOP', 39.453284, -74.651666, 3],
            ['Longhorn Steakhouse', 39.453224, -74.651666, 4],
            ['Chilis Grill and Bar', 39.450375, -74.639798, 5],
            ['Applebees', 39.452392, -74.631073, 6]
          ];
        
          var locationsE = [
            ['Cavallino Nero', 39.456832, -74.660559, 2],
            ['IHOP', 39.453284, -74.651666, 3],
            ['Longhorn Steakhouse', 39.453224, -74.651666, 4],
            ['Chilis Grill and Bar', 39.450375, -74.639798, 5],
            ['Applebees', 39.452392, -74.631073, 6]
          ];
        
          mapOptions = {
            center: new google.maps.LatLng(39.458342, -74.633672),
            zoom: 13
          };
        
          map = new google.maps.Map(document.getElementById("map-dining"), mapOptions);
          map2 = new google.maps.Map(document.getElementById("map-shopping"), mapOptions);
          map3 = new google.maps.Map(document.getElementById("map-schools"), mapOptions);
          map4 = new google.maps.Map(document.getElementById("map-parks"), mapOptions);
          map5 = new google.maps.Map(document.getElementById("map-hospitals"), mapOptions);
          map6 = new google.maps.Map(document.getElementById("map-colleges"), mapOptions);
        
          var infowindow = new google.maps.InfoWindow();
          var marker, markerA, markerB, markerC, markerC, markerD, markerE, i;
        
          for (i = 0; i < locations.length; i++) {
            marker = new google.maps.Marker({
              position: new google.maps.LatLng(locations[i][1], locations[i][2]),
              map: map,
              icon: 'http://maps.google.com/intl/en_us/mapfiles/ms/micons/restaurant.png'
            });
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
              return function() {
                infowindow.setContent(locations[i][0]);
                infowindow.open(map, marker);
              }
            })(marker, i));
          }
          for (i = 0; i < locationsA.length; i++) {
            markerA = new google.maps.Marker({
              position: new google.maps.LatLng(locationsA[i][1], locationsA[i][2]),
              map: map2,
              icon: 'http://maps.google.com/intl/en_us/mapfiles/ms/micons/purple-dot.png'
            });
            google.maps.event.addListener(markerA, 'click', (function(markerA, i) {
              return function() {
                infowindow.setContent(locationsA[i][0]);
                infowindow.open(map2, markerA);
              }
            })(markerA, i));
          }
          for (i = 0; i < locationsB.length; i++) {
            markerB = new google.maps.Marker({
              position: new google.maps.LatLng(locationsB[i][1], locationsB[i][2]),
              map: map3,
              icon: 'http://maps.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png'
            });
            google.maps.event.addListener(markerB, 'click', (function(markerB, i) {
              return function() {
                infowindow.setContent(locationsB[i][0]);
                infowindow.open(map3, markerB);
              }
            })(markerB, i));
          }
          for (i = 0; i < locationsC.length; i++) {
            markerC = new google.maps.Marker({
              position: new google.maps.LatLng(locationsC[i][1], locationsC[i][2]),
              map: map4,
              icon: 'http://maps.google.com/intl/en_us/mapfiles/ms/micons/tree.png'
            });
            google.maps.event.addListener(markerC, 'click', (function(markerC, i) {
              return function() {
                infowindow.setContent(locationsC[i][0]);
                infowindow.open(map4, markerC);
              }
            })(markerC, i));
          }
          for (i = 0; i < locationsD.length; i++) {
            markerD = new google.maps.Marker({
              position: new google.maps.LatLng(locationsD[i][1], locationsD[i][2]),
              map: map5,
              icon: 'http://maps.google.com/intl/en_us/mapfiles/ms/micons/yellow-dot.png'
            });
            google.maps.event.addListener(markerD, 'click', (function(markerD, i) {
              return function() {
                infowindow.setContent(locationsD[i][0]);
                infowindow.open(map5, markerD);
              }
            })(markerD, i));
          }
          for (i = 0; i < locationsE.length; i++) {
            markerE = new google.maps.Marker({
              position: new google.maps.LatLng(locationsE[i][1], locationsE[i][2]),
              map: map6,
              icon: 'http://maps.google.com/intl/en_us/mapfiles/marker_whiteA.png'
            });
            google.maps.event.addListener(markerE, 'click', (function(markerE, i) {
              return function() {
                infowindow.setContent(locationsE[i][0]);
                infowindow.open(map6, markerE);
              }
            })(markerE, i));
          }
        
          var marker1 = new google.maps.Marker({
            position: myCenter,
            map: map,
            title: "Evergreen"
          });
          var marker2 = new google.maps.Marker({
            position: myCenter,
            map: map2,
            title: "Evergreen"
          });
          var marker3 = new google.maps.Marker({
            position: myCenter,
            map: map3,
            title: "Evergreen"
          });
          var marker4 = new google.maps.Marker({
            position: myCenter,
            map: map4,
            title: "Evergreen"
          });
          var marker5 = new google.maps.Marker({
            position: myCenter,
            map: map5,
            title: "Evergreen"
          });
          var marker6 = new google.maps.Marker({
            position: myCenter,
            map: map6,
            title: "Evergreen"
          });
        
        }
        
        google.maps.event.addDomListener(window, 'load', initialize);
        .explore-images .neighborhood-listing,
        .explore-images .leasing-plan {
          display: none;
        }
        .explore-images .neighborhood-listing.visible,
        .explore-images .leasing-plan.visible {
          display: block;
        }
        .explore {
          padding: 2.5% 5%;
          background-color: #f2efef;
        }
        .explore h4 {
          margin-top: 0;
          margin-bottom: .5em;
        }
        /* .directory-btns, */
        
        .neighborhood-btns {
          position: relative;
          top: 0;
          right: 0;
        }
        @media screen and (max-width: 480px) {
          .desktop {
            display: none;
          }
          .mobile {
            display: block;
          }
        }
        #map-dining,
        #map-shopping,
        #map-schools,
        #map-parks,
        #map-hospitals,
        #map-colleges {
          width: 100%;
          height: 400px;
        }
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script src="https://maps.googleapis.com/maps/api/js"></script>
        <div class="explore">
          <h4>Explore Your New Neighborhood</h4>
        
          <select id="neighborhood-select">
            <option value="Dining">Dining</option>
            <option value="Shopping">Shopping</option>
            <option value="Schools">Schools</option>
            <option value="Parks">Parks</option>
            <option value="Hospitals">Hospitals</option>
            <option value="Colleges">Colleges</option>
          </select>
        </div>
        <div class="explore-images featured-image">
          <div class="neighborhood-listing Dining visible">
            <div class="neighborhood-btns">
              <div id="map-dining"></div>
            </div>
          </div>
          <div class="neighborhood-listing Shopping ">
            <div class="neighborhood-btns">
              <div id="map-shopping"></div>
            </div>
          </div>
          <div class="neighborhood-listing Parks ">
            <div class="neighborhood-btns">
              <div id="map-parks"></div>
            </div>
          </div>
          <div class="neighborhood-listing Hospitals ">
            <div class="neighborhood-btns">
              <div id="map-hospitals"></div>
            </div>
          </div>
          <div class="neighborhood-listing Colleges ">
            <div class="neighborhood-btns">
              <div id="map-colleges"></div>
            </div>
          </div>
          <div class="neighborhood-listing Schools ">
            <div class="neighborhood-btns">
              <div id="map-schools"></div>
            </div>
          </div>
        </div>

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-04
          • 1970-01-01
          • 2023-04-04
          相关资源
          最近更新 更多