【问题标题】:Google Maps API v3 Second Map Resize and CenterGoogle Maps API v3 Second Map Resize and Center
【发布时间】:2012-09-27 11:08:13
【问题描述】:

我忘记了 wordpress 上的嵌入地图,并移至谷歌地图 api v3。 所以,我正在使用这个:

<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=true"></script>

还有这个功能:

function initialize() {

//replace 0's on next line with latitude and longitude numbers from earlier on in tutorial.
var myLatlng = new google.maps.LatLng(40.654372, -7.914174);
var myLatlng1 = new google.maps.LatLng(30.654372, -6.914174);
    var myOptions = {
      zoom: 16,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

    var myOptions1 = {
      zoom: 16,
      center: myLatlng1,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }

//here's where we call the marker.
//getElementById must be the same as the id you gave for the container of the map
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var map1 = new google.maps.Map(document.getElementById("map_canvas1"), myOptions1);
 //google.maps.event.trigger(map1, 'resize');
 //map1.setCenter(myLatlng1);

        var marker = new google.maps.Marker({
      position: myLatlng,
      title:"ADD TITLE OF YOUR MARKER HERE"
  });

        var contentString = '<div id="content">'+
    '<div id="siteNotice">'+
    '<\/div>'+
    '<h2 id="firstHeading" class="firstHeading">ADD TITLE HERE<\/h2>'+
    '<div id="bodyContent">'+
    '<p style="font-size:1em">ADD DESCRIPTION HERE<\/p>'+
    '<\/div>'+
    '<\/div>';

var infowindow = new google.maps.InfoWindow({
    content: contentString
});

google.maps.event.addListener(marker, 'click', function() {
  infowindow.open(map,marker);
});      

  // To add the marker to the map, call setMap();
  marker.setMap(map);  
  }

  function loadScript() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
    document.body.appendChild(script);
  }

  window.onload = loadScript;

如您所见,声明了两个映射。我是我的网站,我使用水平选项卡在显示地图之间交替显示。

问题是我在此处的其他主题中发现的问题,但那里提供的解决方案似乎不起作用。正如您在我评论的行中看到的那样:

//google.maps.event.trigger(map1, 'resize');

//map1.setCenter(myLatlng1);

此代码无法使第二张地图居中。地图不能正确地放在 div 上,中心图标在左下角,这是常见的问题。

我想你已经知道问题所在了,但是如果你需要图片,我会毫无问题地提供它们。

标签代码在 wordpress 上的外观:

    [wptabs type="accordion" mode="horizontal"]

[wptabtitle] Portugal[/wptabtitle] [wptabcontent]

<div id="map_canvas" style="width: 100%; height: 300px; border: 1px solid;"></div>
[/wptabcontent]

[wptabtitle] Brasil[/wptabtitle] [wptabcontent]

<div id="map_canvas1" style="width: 100%; height: 300px; border: 1px solid;"></div>

[/wptabcontent]

[/wptabs]

【问题讨论】:

  • 当您使用选项卡查看地图时,我认为您需要在更改与每个地图对应的选项卡时调用此事件,因为地图在放置在隐藏元素中时“不存在”。
  • 我猜你是对的,因为如果我把第二张地图和第一张放在一起,一切都很好。但我该怎么做呢?你能帮帮我吗?
  • 我不太了解wordpress。您可以显示这些选项卡是如何生成的?所以我试着帮忙。
  • 通过插件实现。我更新了主帖的代码。

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


【解决方案1】:

好吧,看来这个插件使用jQuery UI,那么你可以这样:

$('#tabs').bind('tabsshow', function(event, ui) {
    if (ui.panel.id == "tab-map-2") {
        google.maps.event.trigger(map1, 'resize');
        map1.setCenter(myLatlng1);
    }
});

#tabs 位于 div ID 之外,tab-map-2 是第二张地图的标签面板 ID。

希望这会有所帮助。

【讨论】:

  • 但是我可以把这个 jquery 部分放在我有生成地图代码的 javascript 文件中?还是我应该把它放在不同的文件中?
  • 初始化地图和标签后,你可以放在任何地方。
  • 嗯,它不工作,我把代码放在初始化函数结束后。但我不知道我在#tabs 和tab-maps-2 方面做得是否正确。我必须创建一个 id="tabs" 的 div 我把标签的代码放在哪里?关于“tab-maps-2”,我猜在这个插件中,每个选项卡的 id 由标签 [wptabtitle][/wptabtitle] 之间的文本给出,所以我想我必须将它命名为“tabs-map-2” .
  • 您可以在浏览器上打开 HTML 代码来检查这些 ID。通过this demo,插件为外部 div 生成 wp-tabs-1。标签 ID 是每个标签的文本。
  • 我按照你的建议去做。所以,从代码来看,我猜外部 div 被命名为“wp-accordion-1”,因为这是他们在这个插件上允许的一种风格。但是关于这个“手风琴”的第二个标签,我在源代码中看不到任何 id。如果你愿意,你可以在这里检查自己:explicacoes-brunosoutinho.com/contactos/
猜你喜欢
  • 2023-04-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多