开发者

Google Map v3 not centering

Does anyone know if there is a way to specify the size of a google map independent of it's container div? The reason i'm asking is that my map doesn't centre exactly on the lat lng point. I'm loading the map into my page in an iframe that has a width and height (400 x 200). in the frame that I am pulling in, the google map is within a div of size (400 x 200) also. But it's slightly off centre every time.

my code is as follows

function initializeMap(latVal, lngVal, titleStr, merchantName) {
var latlng = new google.maps.LatLng(latVal, lngVal);
var myOptions = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
};  

var contentString = '<span style="font-size:12pt;">' + merchantName + '</span><span style="font-size:10pt;">' + titleStr + '</span>';
var infowindow = new google.maps.InfoWindow(开发者_运维百科{
    content: contentString
});
var marker = new google.maps.Marker({
    position: latlng,
    title:titleStr
});
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
});
// To add the marker to the map, call setMap();
marker.setMap(map);
}

I'm using v3 of the google maps api and was just looking for some pointers on how I might be able to centre my map properly. As mentioned, the above script (and map_canvas div) is loaded into my main page in an iframe.

Many thanks for your help


If I read your script correctly, you're initializing the map, adding a marker then opening the marker's info window. Note the documentation for InfoWindowOptions:

By default, the info window will pan the map so that it is fully visible when it opens.

Set the disableAutoPan property to false when creating the InfoWindow to prevent it repositioning the map:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜