Search code examples
javascriptjquerygoogle-mapsgoogle-street-view

Need to place the pegman at the center of the map


I have a demo here http://jsfiddle.net/coderslay/vQVTq/1/

My Js file

var fenway = new google.maps.LatLng(42.345573,-71.098326);
var panoramaOptions = {
    enableCloseButton : true,
    visible: false
};
var panorama = new  google.maps.StreetViewPanorama(document.getElementById("pano"), panoramaOptions);
var mapOptions = {
  center: fenway,
  zoom: 14,
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  streetView : panorama
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

google.maps.event.addListener(panorama, "visible_changed", function() {
    if (panorama.getVisible() && $("#pano").is(':visible')){
        //moving the pegman around the map
    }else if(panorama.getVisible() && $("#pano").is(':hidden')){
        $("#pano").show();
        $("#map_canvas").removeClass('bigmap');
        $("#map_canvas").addClass('minimap');
            latLngBounds = new google.maps.LatLngBounds();
            latLngBounds.extend( new google.maps.LatLng(panorama.getPosition().lat(), panorama.getPosition().lng()));
    map.panToBounds(latLngBounds);
    map.fitBounds(latLngBounds);
    }
    google.maps.event.addListener(panorama, "closeclick", function() {
        $("#pano").hide();  
        $("#map_canvas").removeClass('minimap');
        $("#map_canvas").addClass('bigmap');         
    });
    });        

My css file

#container {
width:500px;
height: 500px ;
position: relative;
}

#map_canvas, 
#pano {
    position: absolute;
    top: 0;
    left: 0;
}

#map_canvas {
    z-index: 10;
}

.bigmap{
    width:100%;
    height:100%;
}

.minimap{
    width:50%;
    height:100%;
}​ 

Now what is happening is when i do map.panToBounds(latLngBounds); map.fitBounds(latLngBounds); then the pegman is coming at the center of the entire map, regardless of the Street view and normal view. I want the Pegman to be shown at the center of the normal map. How to do it?


Solution

  • It seems to me as if, even though you've changed the width of the map to 50%, Google still thinks it's at 100%, and doesn't know to dynamically adjust it. You could try removing then adding a new map at the new width instead.

    Alternatively, try the Map panBy() function to pan left 250 pixels.

    PS: why is the pano set to 100% width and not 50%?