Reputation: 21178
I'm using Twitter Bootstrap and have implemented Google Maps in a modal, which dynamically shows the position of a producer. Everything works fine, except for the fact that the marker won't center. Instead it's always located in the upper-left corner (seen only if scrolled).
I have tried many things here but can't get it to work. Can someone please help me out here?
html:
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel">Test</h3>
</div>
<div id="modal-left" class="modal-body left">
</div>
<div class="modal-body right">
<div id="map"></div>
</div>
</div>
</div>
<div class="row span12">
<div id="allProducers"></div>
</div>
relevant css:
#map {
height: 300px;
width: 300px;
}
.hide {
display: none;
}
js:
function largeMap(latitude, longitude){
var Latlng = new google.maps.LatLng(latitude, longitude);
var Options = {
zoom: 10,
center: Latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var Map = new google.maps.Map(document.getElementById("map"), Options);
var Marker = new google.maps.Marker({
position: Latlng,
map:Map,
title:"The Map"
});
Marker.setMap(Map);
}
$('.modal-btn').click(function(){
var producerId = $(this).attr('id');
GetProducer(producerId, function(data) {
var titel = data.name;
$('#myModalLabel').html(titel);
$('#myModal').on('shown', function () {
google.maps.event.trigger(map, 'resize');
map.setCenter(new google.maps.LatLng(data.longitude, data.latitude));
})
});
});
Note: I would love to create a jsFiddle, but because I'm using a modal (included in Twitter Bootstrap), which probably is a part of the problem that won't work.
Upvotes: 2
Views: 3848
Reputation: 54618
Basically you need access to the Map
object (and highly recommend just reusing your LatLng
object), you could do it like this:
var Map;
var Markerlatlng
function largeMap(latitude, longitude){
Markerlatlng= new google.maps.LatLng(latitude, longitude);
var Options = {
zoom: 10,
center: Markerlatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
Map = new google.maps.Map(document.getElementById("map"), Options);
var Marker = new google.maps.Marker({
position: Markerlatlng,
map:Map,
title:"The Map"
});
Marker.setMap(Map);
}
$('.modal-btn').click(function(){
var producerId = $(this).attr('id');
GetProducer(producerId, function(data) {
var titel = data.name;
$('#myModalLabel').html(titel);
$('#myModal').on('shown', function () {
google.maps.event.trigger(Map, 'resize');
Map.setCenter(Markerlatlng);
})
});
});
Upvotes: 4
Reputation: 16938
I think you mixed the cases:
var Map = new google.maps.Map(document.getElementById("map"), Options);
and
map.setCenter(new google.maps.LatLng(data.longitude, data.latitude));
Try this instead
map = new google.maps.Map(document.getElementById("map"), Options);
Aren't there any issues in your firebug/chrome console?
Upvotes: 1