简体   繁体   English

自定义Google Map v3控件位置问题

[英]Custom Google Map v3 Controls Position Issue

I'm having an issue with three custom controls I have created for a map application. 我为地图应用程序创建的三个自定义控件存在问题。

According to the API documentation: 根据API文档:

The API places controls at each position by the order of an index property; API按照索引属性的顺序将控件放置在每个位置。 controls with a lower index are placed first. 具有较低索引的控件放在第一位。 For example, two custom controls at position BOTTOM_RIGHT will be laid out according to this index order, with lower index values taking precedence. 例如,将根据该索引顺序布置位置BOTTOM_RIGHT的两个自定义控件,其中较低的索引值优先。 By default, all custom controls are placed after placing any API default controls. 默认情况下,所有自定义控件都放置在放置任何API默认控件之后。 You can override this behavior by setting a control's index property to be a negative value. 您可以通过将控件的index属性设置为负值来覆盖此行为。 Custom controls cannot be placed to the left of the logo or to the right of the copyrights. 自定义控件不能放置在徽标的左侧或版权的右侧。

Therefore, I added the three controls to the same position, giving each an index value, and expected them to fit accordingly. 因此,我将三个控件添加到同一位置,为每个控件指定一个索引值,并期望它们能够相应地适合。

When the controls are first loaded, they are on top of one another, instead of fitting to match their respective index values. 第一次加载控件时,它们位于另一个顶部,而不适合于匹配它们各自的索引值。

However, when I perform an action such as hovering over another default control (such as the zoom control), the custom controls appear correctly. 但是,当我执行诸如将鼠标悬停在另一个默认控件(例如缩放控件)上的操作时,自定义控件会正确显示。

Here is what I have tried to fix the problem: 这是我尝试解决的问题:

  • Setting the position of the controls in CSS (does not work since control positioning can only be custom if you wrap the controls) 在CSS中设置控件的位置(此操作无效,因为只有在包装控件后才能自定义控件的位置)
  • Delaying the time for each control button to be added 延迟添加每个控制按钮的时间
  • Tried triggering mouseover actions of other controls since this manually shows the controls in the correct position 尝试触发其他控件的鼠标悬停操作,因为这会手动将控件显示在正确的位置

Any help or insight in appreciated. 任何帮助或见解表示赞赏。 I know I mentioned wrapping the controls allows for custom position (according to here ), but is there any other way I can get this to work without doing so? 我知道我提到过包装控件允许自定义位置(根据here ),但是还有其他方法可以使它生效吗?

My apologies, I tried uploading screenshots but apparently I am not popular enough. 抱歉,我尝试上传屏幕截图,但显然我并不受欢迎。 Here is a JsFiddle . 这是一个JsFiddle

The JsFiddle shows how I am adding these controls only when the user has selected a specific input: JsFiddle显示了仅当用户选择了特定输入时,我才如何添加这些控件:

$('#toggle').on('click', function(){
    if ($(this).is(':checked')){
        $(pointSelDiv).css('display', 'block');
        $(polySelDiv).css('display', 'block');
        $(circSelDiv).css('display', 'block');   
    }else{
        $(pointSelDiv).css('display', 'none');
        $(polySelDiv).css('display', 'none');
        $(circSelDiv).css('display', 'none');   
    }
});

Thanks again! 再次感谢!

This is happening because Google Maps API needs to know the width and the height of your control elements to know where to position them - when the map is rendered. 之所以发生这种情况,是因为Google Maps API需要知道控件元素的widthheight ,才能知道它们的放置位置-地图渲染时。 By initially setting them to display: none , you are hiding it from the actual layout of your page as well - it's as if the element's not there. 通过最初将它们设置为display: none ,您也将其隐藏在页面的实际布局中-好像该元素不在那里。 Use visibility: hidden instead - setting the visibility to hidden will still hide the element on the screen, but it is still present in the layout. 使用visibility: hidden -将visibility设置为“ hidden仍会隐藏屏幕上的元素,但它仍存在于布局中。 For reference: http://www.w3schools.com/css/css_display_visibility.asp 供参考: http : //www.w3schools.com/css/css_display_visibility.asp

Also, I suggest rather than individually setting these CSS attributes to your custom control elements, add a class (you can do this via jquery's .addClass() ) to these elements and toggle just by targeting the class. 另外,我建议不要将这些CSS属性单独设置为自定义控件元素,而是将一个类(可以通过jquery的.addClass() )添加到这些元素中,然后仅通过定位该类即可进行切换。 I've updated your jsfiddle here . 我在这里更新了您的jsfiddle。

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

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