【问题标题】:slider inside google map info window谷歌地图信息窗口内的滑块
【发布时间】:2012-09-17 18:42:46
【问题描述】:

问题:我正在尝试在谷歌地图的信息窗口中集成一个滑块(轮播)。

1.滑块的 jQuery 代码:

<head>
...
    <script src="js/jquery.anythingslider.js"></script>
    <script>
       // Set up Sliders
        // **************         
        $(function(){
            $('#mySlider').anythingSlider({
                mode                : 'f',   // fade mode - new in v1.8!
                resizeContents      : false, // If true, solitary images/objects in the panel will expand to fit the viewport
                expand              : true,
                navigationSize      : 3,     // Set this to the maximum number of visible navigation tabs; false to disable

                onSlideBegin: function(e,slider) {
                    // keep the current navigation tab in view
                    slider.navWindow( slider.targetPage );
                }
            });
        });
    </script>
  ...
 </head>

2。 Html 代码。这是滑块在常规 html 页面中的工作方式:

 <div style="width:450px;height:150px;">        
    <ul id="mySlider">  <!-- id corresponds to id used in Jquery code  -->

       <li>
         Content of Slide 1
       </li>
       <li>
          Content of Slide 2
       </li>
       <li>
          Content of slide 3
       </li>

     </ul>
  </div>

现在,我对 JavaScript 没有太多经验,所以如何让 jquery 代码在信息窗口中可访问。换句话说,我应该把他的滑块代码放在哪里?

编辑: 这是我迄今为止尝试过的,但没有运气

(function() {  
// Defining variables that need to be available to some functions
var map, infoWindow;
window.onload = function() {
    // Creating a map
    var options = {
        zoom: 3,
        center: new google.maps.LatLng(37.09, -95.71),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById('map'), options);
    // Adding a marker to the map
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(40.756054, -73.986951),
        map: map,
        title: 'Click me'
    });
    google.maps.event.addListener(marker, 'click', function() {


        // add some content to userLi1 
        var userLi1 = '<li>Some awesome content for the 1st list item</li>';

        // add some content userLi2
        var userLi2 = '<li>Some awesome content for the 2nd list item</li>'


        // Check to see if an InfoWindow already exists
        if (!infoWindow) {
            infoWindow = new google.maps.InfoWindow();

        }


        // Setting the content of the InfoWindow to the detail map
        //infoWindow.setContent(detailDiv);

        infoWindow.setContent('<div style = "width:450px;height:150px;"><ul id="mySlider">' + userLi1 + userLi2 + '</ul></div>');

        // Opening the InfoWindow
        infoWindow.open(map, marker);   

    });

    // initiate slider here
    $('#mySlider').anythingSlider({
        mode                : 'f',   // fade mode - new in v1.8!
        resizeContents      : false, // If true, solitary images/objects in the panel will expand to fit the viewport
        expand              : true,
        navigationSize      : 3,     // Set this to the maximum number of visible navigation tabs; false to disable

        onSlideBegin: function(e,slider) {
            // keep the current navigation tab in view
            slider.navWindow( slider.targetPage );
        }
    });
};
})();

当我运行代码时,Jquery 根本不会被触发。

编辑 2:解决方案我通过使用 McMaster 代码并将相关的 jquery 代码包装起来解决了这个问题

 google.maps.event.addListener(infowindow, 'domready', function(){
 }); 

这是完整的代码:

 $(document).ready(function() {   // runs jquery when document is ready

function initialize() {
    var mapDiv = document.getElementById('map');
    map = new google.maps.Map(mapDiv, {
        zoom: 3,
        center: new google.maps.LatLng(37.09, -95.71),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    infowindow = new google.maps.InfoWindow({
        content: "holding..."
    });

    // looks for map, when tiles are loaded, fire function addmarkers                 
    google.maps.event.addListenerOnce(map, 'tilesloaded', addMarkers);

}

function addMarkers() {
    var lat =37.09;
    var lng = -95-71;
    var latLng = new google.maps.LatLng(
        lat,
        lng);
    var marker = new google.maps.Marker({
        position: latLng,
        map: map
    });
    // add some content to userLi1
    var userLi1 = '<li>Some awesome content for the 1st list item</li>';

    // add some content userLi2
    var userLi2 = '<li>Some awesome content for the 2nd list item</li>'
    // set marker content
    marker.html = '<div style = "width:450px;height:150px;"><ul id="mySlider">' + userLi1 + userLi2 + '</ul></div>';
    // add listener
    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(marker.html);
        infowindow.open(map, marker);
    });

}

initialize();      
google.maps.event.addListener(infowindow, 'domready', function(){
    // initiate slider here
    $('#mySlider').anythingSlider({
        mode                : 'f',   // fade mode - new in v1.8!
        resizeContents      : false, // If true, solitary images/objects in the panel will expand to fit the viewport
        expand              : true,
        navigationSize      : 3,     // Set this to the maximum number of visible navigation tabs; false to disable

        onSlideBegin: function(e,slider) {
            // keep the current navigation tab in view
            slider.navWindow( slider.targetPage );
        }
    });
}); 


});

【问题讨论】:

  • +1 为 google.maps.event.addListener(infowindow, 'domready', function(){ });

标签: javascript jquery google-maps


【解决方案1】:

你实际上是在文档上直接制作一个 DOM 元素

你需要使用这样的东西:

infoWindow.setContent('<div id="mySlider"><ul><li>' + userLi1 + '</li><li>' + userLi2 + '</li></ul></div>');

编辑:

这是根据我的一个项目修改的完全有效的脚本以满足您的需求:

$(document).ready(function() {   // runs jquery when document is ready

     function initialize() {
            var mapDiv = document.getElementById('map');
            map = new google.maps.Map(mapDiv, {
                 zoom: 3,
                center: new google.maps.LatLng(37.09, -95.71),
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });
            infowindow = new google.maps.InfoWindow({
                content: "holding..."
            });

           // looks for map, when tiles are loaded, fire function addmarkers                 
            google.maps.event.addListenerOnce(map, 'tilesloaded', addMarkers);

          }

          function addMarkers() {
                var lat =37.09;
                var lng = -95-71;
              var latLng = new google.maps.LatLng(
                                                lat,
                                                lng);
              var marker = new google.maps.Marker({
                position: latLng,
                map: map
              });
             // add some content to userLi1 
            var userLi1 = '<li>Some awesome content for the 1st list item</li>';

            // add some content userLi2
            var userLi2 = '<li>Some awesome content for the 2nd list item</li>'
              // set marker content
              marker.html = '<div style = "width:450px;height:150px;"><ul id="slider2">' + userLi1 + userLi2 + '</ul></div>';
             // add listener 
             google.maps.event.addListener(marker, 'click', function() {
                    infowindow.setContent(marker.html);
                  infowindow.open(map, marker);
                });

          }

          initialize();      
          // initiate slider here
        $('#mySlider').anythingSlider({
            mode                : 'f',   // fade mode - new in v1.8!
            resizeContents      : false, // If true, solitary images/objects in the panel will expand to fit the viewport
            expand              : true,
            navigationSize      : 3,     // Set this to the maximum number of visible navigation tabs; false to disable

            onSlideBegin: function(e,slider) {
                // keep the current navigation tab in view
                slider.navWindow( slider.targetPage );
            }
        });

});​

【讨论】:

  • 感谢您的回复,但是当我使用您的方法时,我会在 infoWindow 中获得 [object HTMLLIElement] 列表。还编辑了帖子,我忘了指定javascript代码根本不会触发滑块代码。
  • 如果我将 $ 符号放在开头的函数之前,则地图根本不会加载。否则,如果我删除它,我会得到与以前相同的结果:jquery 根本不加载。
  • 有现场版我可以看看吗?我希望看到实际的问题。看看这个jsFiddle,我重新组织了你的代码......我上次忽略了一些常见错误($(document).ready()$(window).load())。你的谷歌地图启动脚本是什么样的?
  • 终于有时间了,所以我重新配置了你的整个 jquery 代码,到目前为止它对我有用(实际上没有滑块插件,但语法是正确的)。看看最后一个jsFiddle 抱歉,花了一整天的时间才找到解决方案.. 你知道的全职工作...... :-/
  • 首先,我非常感谢您花时间帮助我。现在,当我运行您的代码时,根本不会加载疯狂的代码。你用的是哪个版本的jquery,可能是这个问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-10
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多