【问题标题】:Google maps load default and different location on click谷歌地图在点击时加载默认和不同的位置
【发布时间】:2018-06-08 02:11:28
【问题描述】:

我正在尝试在页面上加载默认位置,并在用户单击链接时进行更改。我试图寻找一些我在网上找到的脚本,但我无法修复它。 我确信我已经很接近了,但我已经尝试了一个星期 =( 有人可以帮我吗? =)

html
<div class="container-fluid" style="padding: 0;">
  <div class="col-lg-12">
    <div class="col-lg-3 sidebarMap">
      <h3>Casa Central SFV</h3>
      <p class="formInfo" style="border-bottom:0px;">Dirección:
        <a class="openmap" role="button" data-id="Casa Central">Ver en mapa</a>
      </p>
      <h3>Oficinas Belén</h3>
      <p class="formInfo" style="border-bottom:0px;">Dirección:
        <a class="openmap" role="button" data-id="Oficina Belen">Ver en mapa</a>
      </p>
    </div>

    <div class="col-lg-9" id="contactMap">
    </div>
  </div>

</div>

JS

var map;
var marker;
var uluru;

function initMap(longs, latts, markerTitle) {
  uluru = new google.maps.LatLng(longs, latts);
  // var uluru = {lat: -28.458342, lng: -65.770767};
  var map = new google.maps.Map(document.getElementById('contactMap'), {
    zoom: 17,
    center: uluru
  });
  var image = 'img/contacto/icon-map.png';
  var marker = new google.maps.Marker({
    position: uluru,
    map: map,
    icon: image,
    title: markerTitle,
    animation: google.maps.Animation.DROP,
    maxWidth: 200,
    maxHeight: 200
  });

  $('#mapModal').on('shown.bs.tab', function() {
    google.maps.event.trigger(map, "resize");
    map.setCenter(uluru);
  });
}


// Start Map Modals 
$(document).ready(function() {
  var myMapId = $(this).data('id');
  myMapId == "Casa Central";
  initMap(-28.458342, -65.770767, "Casa Central");
});

$(document).on("click", ".openmap", function() {
  var myMapId = $(this).data('id');
  if (myMapId == "Casa Central") {
    initMap(-28.458342, -65.770767, "Casa Central");
  } else if (myMapId == "Oficina Belen") {
    initMap(-27.652671, -67.028263, "Oficina Belén");
  }

});

CSS

#contactMap {
  height: 400px;
  width: 75%;
  float: right;
  z-index: 10;
}

.sidebarMap {
  position: absolute;
  top: 0px;
  left: 0;
  z-index: 1000;
  padding: 5px 15px 5px 25px;
  overflow-x: hidden;
  overflow-y: auto;
  background-color: #FFF;
}

我什至不能做我的小提琴作品 D: https://jsfiddle.net/Karsp/9z6dpk7j/

它在我的文件中一切正常,只是默认不加载第一个位置。

【问题讨论】:

  • 我在您的代码中看不到任何问题,只有 jquery 和 popper.js (bootstrap) 丢失。检查这个小提琴更新:jsfiddle.net/beaver71/g4jmmrks
  • 哦,谢谢!在我的代码中,我有一个图像,因为我忘了把那个代码从小提琴上取下来,所以没有用。我将检查在原始代码中进行这些更改。谢谢!
  • 再次嗨,我确实在小提琴上修复了它,但没有在我的代码中修复它哈哈哈我做了同样的修改,但是当页面加载时,地图没有显示任何位置。但是,如果我单击按钮,它会起作用。有什么建议吗?临时链接:magiayte.com/delparque/contacto.php非常感谢!!

标签: javascript jquery google-maps google-maps-markers


【解决方案1】:

在您的网站中,我注意到initMap() 函数被调用了两次:

尝试以这种方式更改 Google Maps API 参考:

https://maps.googleapis.com/maps/api/js?key=xyz&callback=initialize

和代码来自:

$(document).ready( function () {   
    var myMapId = $(this).data('id');
    myMapId == "Casa Central"; 
    initMap(-28.458342,-65.770767,"Casa Central");
});

到:

function initialize() {   
    var myMapId = $(this).data('id');
    myMapId == "Casa Central"; 
    initMap(-28.458342,-65.770767,"Casa Central");
};

P.S.:initialize() 函数必须是全局可访问的

【讨论】:

  • 哦,非常感谢!!这解决了所有问题:D 感谢您抽出宝贵的时间,祝您度过愉快的一周!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-03
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多