位置Marke在关闭模态时消失

时间:2018-01-24 20:30:57

标签: javascript jquery

我不会说英语,对不起这个错误。

我正在使用 bootstrap jquery propeller.in

  

https://github.com/ChadKillingsworth/geolocation-marker

我有以下代码

var map, GeoMarker;

      function initialize() {
        var mapOptions = {
          zoom: 17,
          center: new google.maps.LatLng(-34.397, 150.644),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);

        GeoMarker = new GeolocationMarker();
        GeoMarker.setCircleOptions({fillColor: '#808080'});

        google.maps.event.addListenerOnce(GeoMarker, 'position_changed', function() {
          map.setCenter(this.getPosition());
          map.fitBounds(this.getBounds());
        });

        google.maps.event.addListener(GeoMarker, 'geolocation_error', function(e) {
          alert('There was an error obtaining your position. Message: ' + e.message);
        });

        GeoMarker.setMap(map);
      }

      google.maps.event.addDomListener(window, 'load', initialize);

      if(!navigator.geolocation) {
        alert('Your browser does not support geolocation');
      }
<link href="https://propeller.in/assets/css/propeller.min.css" rel="stylesheet"/>
<link href="https://propeller.in/assets/css/bootstrap.min.css" rel="stylesheet"/>

<!DOCTYPE html>
<html>
  <head>
    <title>Geolocation Marker Example Usage</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map_canvas {
        margin: 0;
        padding: 0;
        height: 100%;
      }
    </style>
    
    
  </head>
  <body>
  <button data-target="#large-dialog" data-toggle="modal" class="btn pmd-ripple-effect btn-primary pmd-z-depth" type="button">Large Modal</button>
<div tabindex="-1" class="modal fade" id="large-dialog" style="display: none;" aria-hidden="true">
	<div class="modal-dialog modal-lg">
		<div class="modal-content">
			<div class="modal-header">
				<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
		
			<div class="modal-body">
              <div id="map_canvas"></div><!-- no working -->
      </div>
			
			</div>
		</div>
	</div>
</div>

    
  </body>
  <script src="https://propeller.in/assets/js/jquery-1.12.2.min.js"></script>
  <script src="https://propeller.in/assets/js/bootstrap.min.js"></script>
  <script src="https://propeller.in/assets/js/propeller.min.js"></script>

<script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="https://chadkillingsworth.github.io/geolocation-marker/geolocation-marker.js"></script>
  
  <script>
$('#large-dialog').on('shown.bs.modal', function (e) {
  initialize();
}); 
</script>


</html>

问题是地图没有在div内部加载,如果我放出它正常工作的模态内容,我想知道如何在一个模态中加载地图,例子使用什么我已经

2 个答案:

答案 0 :(得分:1)

一旦模态打开,您需要初始化地图,因为地图只会加载在可见元素中。

$('#large-dialog').on('shown.bs.modal', function (e) {
  initialize();
});

答案 1 :(得分:0)

您需要绑定事件shown.bs.modal以执行initialize函数:

$('#large-dialog').on('shown.bs.modal', function(e) {...}

并将div的维度更改为固定的。对于此示例,我设置了300px x 300px。

&#13;
&#13;
var map, GeoMarker;

function initialize() {
  var mapOptions = {
    zoom: 17,
    center: new google.maps.LatLng(-34.397, 150.644),
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map_canvas'),
    mapOptions);

  GeoMarker = new GeolocationMarker();
  GeoMarker.setCircleOptions({
    fillColor: '#808080'
  });

  google.maps.event.addListenerOnce(GeoMarker, 'position_changed', function() {
    map.setCenter(this.getPosition());
    map.fitBounds(this.getBounds());
  });

  google.maps.event.addListener(GeoMarker, 'geolocation_error', function(e) {
    alert('There was an error obtaining your position. Message: ' + e.message);
  });

  GeoMarker.setMap(map);
}

google.maps.event.addDomListener(window, 'load', initialize);

if (!navigator.geolocation) {
  alert('Your browser does not support geolocation');
}


$('#large-dialog').on('shown.bs.modal', function(e) {
  initialize();
});
&#13;
html,
body,
#map_canvas {
  margin: 0;
  padding: 0;
  height: 300px;
  width: 300px
}
&#13;
<link href="https://propeller.in/assets/css/propeller.min.css" rel="stylesheet" />
<link href="https://propeller.in/assets/css/bootstrap.min.css" rel="stylesheet" />

<script src="https://propeller.in/assets/js/jquery-1.12.2.min.js"></script>
<script src="https://propeller.in/assets/js/bootstrap.min.js"></script>
<script src="https://propeller.in/assets/js/propeller.min.js"></script>

<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://chadkillingsworth.github.io/geolocation-marker/geolocation-marker.js"></script>


<button data-target="#large-dialog" data-toggle="modal" class="btn pmd-ripple-effect btn-primary pmd-z-depth" type="button">Large Modal</button>
<div tabindex="-1" class="modal fade" id="large-dialog" style="display: none;" aria-hidden="true">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>

        <div class="modal-body">
          <div id="map_canvas"></div>
          <!-- no working -->
        </div>

      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

希望有所帮助!