I do not speak English, sorry for the mistakes.
I’m using bootstrap, jquery, propeller.in and
I have the following code
JavaScript
x
31
31
1
var map, GeoMarker;
2
3
function initialize() {
4
var mapOptions = {
5
zoom: 17,
6
center: new google.maps.LatLng(-34.397, 150.644),
7
mapTypeId: google.maps.MapTypeId.ROADMAP
8
};
9
map = new google.maps.Map(document.getElementById('map_canvas'),
10
mapOptions);
11
12
GeoMarker = new GeolocationMarker();
13
GeoMarker.setCircleOptions({fillColor: '#808080'});
14
15
google.maps.event.addListenerOnce(GeoMarker, 'position_changed', function() {
16
map.setCenter(this.getPosition());
17
map.fitBounds(this.getBounds());
18
});
19
20
google.maps.event.addListener(GeoMarker, 'geolocation_error', function(e) {
21
alert('There was an error obtaining your position. Message: ' + e.message);
22
});
23
24
GeoMarker.setMap(map);
25
}
26
27
google.maps.event.addDomListener(window, 'load', initialize);
28
29
if(!navigator.geolocation) {
30
alert('Your browser does not support geolocation');
31
}
JavaScript
1
53
53
1
<link href="https://propeller.in/assets/css/propeller.min.css" rel="stylesheet"/>
2
<link href="https://propeller.in/assets/css/bootstrap.min.css" rel="stylesheet"/>
3
4
<!DOCTYPE html>
5
<html>
6
<head>
7
<title>Geolocation Marker Example Usage</title>
8
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
9
<meta charset="utf-8">
10
<style>
11
html, body, #map_canvas {
12
margin: 0;
13
padding: 0;
14
height: 100%;
15
}
16
</style>
17
18
19
</head>
20
<body>
21
<button data-target="#large-dialog" data-toggle="modal" class="btn pmd-ripple-effect btn-primary pmd-z-depth" type="button">Large Modal</button>
22
<div tabindex="-1" class="modal fade" id="large-dialog" style="display: none;" aria-hidden="true">
23
<div class="modal-dialog modal-lg">
24
<div class="modal-content">
25
<div class="modal-header">
26
<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
27
28
<div class="modal-body">
29
<div id="map_canvas"></div><!-- no working -->
30
</div>
31
32
</div>
33
</div>
34
</div>
35
</div>
36
37
38
</body>
39
<script src="https://propeller.in/assets/js/jquery-1.12.2.min.js"></script>
40
<script src="https://propeller.in/assets/js/bootstrap.min.js"></script>
41
<script src="https://propeller.in/assets/js/propeller.min.js"></script>
42
43
<script src="https://maps.googleapis.com/maps/api/js"></script>
44
<script src="https://chadkillingsworth.github.io/geolocation-marker/geolocation-marker.js"></script>
45
46
<script>
47
$('#large-dialog').on('shown.bs.modal', function (e) {
48
initialize();
49
});
50
</script>
51
52
53
</html>
The problem is that the map does not load inside the div, if I put out the modal content it works normally, I would like to find out how the map can be loaded inside a modal, with examples using what I already have
Advertisement
Answer
You need to bind the event shown.bs.modal
to execute the initialize
function:
JavaScript
1
2
1
$('#large-dialog').on('shown.bs.modal', function(e) { }
2
And change the dimension of your div to something fixed. For this example, I’m setting 300px x 300px.
JavaScript
1
38
38
1
var map, GeoMarker;
2
3
function initialize() {
4
var mapOptions = {
5
zoom: 17,
6
center: new google.maps.LatLng(-34.397, 150.644),
7
mapTypeId: google.maps.MapTypeId.ROADMAP
8
};
9
map = new google.maps.Map(document.getElementById('map_canvas'),
10
mapOptions);
11
12
GeoMarker = new GeolocationMarker();
13
GeoMarker.setCircleOptions({
14
fillColor: '#808080'
15
});
16
17
google.maps.event.addListenerOnce(GeoMarker, 'position_changed', function() {
18
map.setCenter(this.getPosition());
19
map.fitBounds(this.getBounds());
20
});
21
22
google.maps.event.addListener(GeoMarker, 'geolocation_error', function(e) {
23
alert('There was an error obtaining your position. Message: ' + e.message);
24
});
25
26
GeoMarker.setMap(map);
27
}
28
29
google.maps.event.addDomListener(window, 'load', initialize);
30
31
if (!navigator.geolocation) {
32
alert('Your browser does not support geolocation');
33
}
34
35
36
$('#large-dialog').on('shown.bs.modal', function(e) {
37
initialize();
38
});
JavaScript
1
8
1
html,
2
body,
3
#map_canvas {
4
margin: 0;
5
padding: 0;
6
height: 300px;
7
width: 300px
8
}
JavaScript
1
27
27
1
<link href="https://propeller.in/assets/css/propeller.min.css" rel="stylesheet" />
2
<link href="https://propeller.in/assets/css/bootstrap.min.css" rel="stylesheet" />
3
4
<script src="https://propeller.in/assets/js/jquery-1.12.2.min.js"></script>
5
<script src="https://propeller.in/assets/js/bootstrap.min.js"></script>
6
<script src="https://propeller.in/assets/js/propeller.min.js"></script>
7
8
<script src="https://maps.googleapis.com/maps/api/js"></script>
9
<script src="https://chadkillingsworth.github.io/geolocation-marker/geolocation-marker.js"></script>
10
11
12
<button data-target="#large-dialog" data-toggle="modal" class="btn pmd-ripple-effect btn-primary pmd-z-depth" type="button">Large Modal</button>
13
<div tabindex="-1" class="modal fade" id="large-dialog" style="display: none;" aria-hidden="true">
14
<div class="modal-dialog modal-lg">
15
<div class="modal-content">
16
<div class="modal-header">
17
<button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
18
19
<div class="modal-body">
20
<div id="map_canvas"></div>
21
<!-- no working -->
22
</div>
23
24
</div>
25
</div>
26
</div>
27
</div>