简体   繁体   English

Google Map的信息框中的图像滑块

[英]Image slider in Google Map's Infobox

I am having the Infobox (a Infowindow with more controls) popup when the user clicks on a marker on the map. 当用户点击地图上的标记时,我正在弹出信息框(具有更多控件的Infowindow)弹出窗口。 In this popup window, I am currently trying to insert a jQuery image slider, in particular NivoSlider. 在这个弹出窗口中,我正在尝试插入一个jQuery图像滑块,特别是NivoSlider。

Problem: However, this image slider only works when its on a normal HTML page, and does not work at all when inside the Infobox. 问题:但是,此图像滑块仅在普通HTML页面上有效,并且在信息框内部时根本不起作用。 I need the image to work inside the infobox. 我需要的图像信息框里面工作。

jQuery/JS Code jQuery / JS代码

The part that loads the jQuery image slider is enclosed by <div id="slider class="nivoSlider"><img...../><img....../></div>" 加载jQuery图像滑块的部分由<div id="slider class="nivoSlider"><img...../><img....../></div>"括起来

$(window).load(function() {

$('#slider').nivoSlider(); $( '#滑块')nivoSlider(); //loads the nivoslider in the div #slider //在div #slider中加载nivoslider

$("#search_button").click(function(e){ e.preventDefault(); var search_location = $("#search_location").val(); $.getJSON(........................., function() { $(“#search_button”)。click(function(e){e.preventDefault(); var search_location = $(“#search_location”)。val(); $ .getJSON(.......... ...............,function(){

        for( i = 0; i < json.length; i++) {

            // Place markers on map
            var latLng = new google.maps.LatLng(json[i].lat, json[i].lng);
            var marker = new google.maps.Marker({
                position: latLng,
                map: map
            });
            markers.push(marker);

            // Create infowindows
            var boxText = '<div id="infobox"> \
                                <div id="infobox_header_title"> \
                                    <span id="infobox_header_price">$' + json[i].price + '</span> \
                                    <span id="infobox_header_room">' + json[i].bedroom + 'br </span> \
                                    <span id="infobox_header_address">' + json[i].address_1 + '</span> \
                                </div> \
                                <div id="slider" class="nivoSlider"> \
                                    <img src="/images/cl/' +  json[i].photos[0] + '.jpg" /> \
                                    <img src="/images/cl/' +  json[i].photos[1] + '.jpg" /> \
                                </div> \
                            </div>';
            var infoboxOptions = {
                content: boxText,
                disableAutoPan: false,
                maxWidth: 0,
                pixelOffset: new google.maps.Size(-140, 0),
                zIndex: null,
                boxStyle: { 
                },
                closeBoxMargin: "10px 2px 2px 2px",
                closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
                infoBoxClearance: new google.maps.Size(1, 1),
                isHidden: true,
                pane: "floatPane",
                enableEventPropagation: false
                };

            var infobox = new InfoBox(infoboxOptions);
            infobox.open(map, markers[i]);
            infoboxes.push(infobox);

            // Create Listeners
            markers[i]._index = i;
            google.maps.event.addListener(markers[i], 'click', function() {

                //infoboxes[this._index].open(map, markers[i]);
                infoboxes[this._index].show();

            });
        };

        // Fill screen with markers
        mapAutoCenter(markers);

    });

What I think is causing this: Probably something to do with the <div> in the infobox not being created when the $('#slider').nivoSlider() is called right at the start 我认为是造成这种情况的原因:可能是在开始时调用$('#slider').nivoSlider()时未创建信息框中的<div>的原因

Additional Info: I'm using Google Maps API V3, with PHP and Codeigniter framework. 附加信息:我正在使用Google Maps API V3,PHP和Codeigniter框架。

The slider works by binding an event listener, which will be lost when the infoWindow is removed from the DOM tree (Google maps removes the infoWindow from the DOM tree when it is not visible). 滑块通过绑定事件侦听器来工作,当将事件窗口从DOM树中删除时,该事件侦听器将丢失(当Google地图不可见时,该消息窗将从DOM树中删除信息窗口)。 Even when the infoWindow appears, the listener will still be gone. 即使出现infoWindow,侦听器仍将消失。

You will have to rewrite the slider listener part of your NivoSlider plugin. 您将不得不重写NivoSlider插件的滑块侦听器部分。 Here's some guidance. 这是一些指导。

Write a click handler, mousemove handler and a mouseup handler function and put it somewhere (probably inside the the NivoSlider plugin so you don't pollute the gloal space). 编写一个单击处理程序,mousemove处理程序和mouseup处理程序函数,并将其放在某个位置(可能在NivoSlider插件内部,这样就不会污染整个空间)。 Something like this: 像这样的东西:

$.fn.nivoSlider.handlers = {
    sliderClickHandler: function (event) {
        // register mousemove and mouseup handlers
        $(document).bind('mousemove', $.fn.nivoSlider.handlers.sliderMMoveHandler);
        $(document).bind('mouseup', $.fn.nivoSlider.handlers.sliderMouseupHandler)
    }, 

    sliderMMoveHandler: function (event) {
        // process mousemove event (move the slider, change images, etc. put your logic here)
    },

    sliderMouseupHandler: function (event) {
        // unregister the listeners since the click event has completed
        $(document).unbind('mouseup', $.fn.nivoSlider.handlers.sliderMouseupHandler)
        $(document).unbind('mousemove', $.fn.nivoSlider.handlers.sliderMMoveHandler);
    }
};

Now, register the sliderClickHandler directly in the HTML as an onclick event: 现在,直接在HTML中注册sliderClickHandler作为onclick事件:

<div id="slider" class="nivoSlider" onclick="$.fn.nivoSlider.handlers.sliderClickHandler()">

Notice, you couldn't do something like this: 请注意,您无法执行以下操作:

onclick="sliderClickHandler()"

because you're putting the listener directly into the DOM element which will be executing from the global space (and will not have any other namespace bound to it, unlike typical javascript where we are so used to functions being closures and encapsulating/remembering where they were defined). 因为您将侦听器直接放入将从全局空间执行的DOM元素中(并且没有任何其他命名空间绑定到该元素,所以与典型的javascript不同,我们习惯于将这些函数用作闭包和封装/记住它们的位置)被定义)。

There is one major issue with this workaround (but I can't think of a better way to do it). 这种解决方法存在一个主要问题(但我想不出一种更好的方法)。 If you will have more than one NivoSlider on your page (which you probably will), then naturally you will want the handler functions to behave differently depending on which NivoSlider you are interacting with. 如果您的页面上有多个NivoSlider(您可能会这样做),那么您自然会希望处理程序函数的行为取决于您与之交互的NivoSlider。 Since you can't create handlers on the fly as closures on each instance, you will have to have unique ID's for each NivoSlider and the handler's will have to act depending on which ID is currently the active object. 由于您不能在每个实例上动态创建处理程序作为闭包,因此每个NivoSlider都必须具有唯一的ID,并且处理程序必须根据当前是哪个活动对象来执行操作。 (So you'll have to store the currently active NivoSlider's ID somewhere). (所以你必须在某处存储当前活动的NivoSlider的ID)。

Good luck. 祝好运。 Definitely doable, though not very fun. 绝对可行,但不是很有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM