简体   繁体   中英

Google maps javascript API how to center map on infobox

If I have an infobox on a google maps how do I make the infobox appear centered on the map? I specify this to center the map on the marker. But I have an richmarker box of variable width and height next to the map marker. I want that infobox to appear centered on the map.

    var lat_lon = new google.maps.LatLng(latitude, longitude);
    var myOptions = {
        zoom: 4,
        center: lat_lon,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
    };

    map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

    var marker = new google.maps.Marker({
        position: lat_lon,
        map: map,
        title: place,
        icon: 'googlemaps/church3.png',
        clickable: true,
    });

    var mappics_dir = "mappics/";
    var picture_img = "";
    if ( picture != undefined && picture != "" ) {
        picture_img = '<div><img src="'+mappics_dir+picture+'" style="width: auto; height: 100px; min-width: 90px;"/></div>'; 
    }

    marker_info = new RichMarker({
        position: lat_lon,
        map: map,
        draggable: true,
        content: '<div class="channel_marker">' + picture_img + '<div style="width: auto;">'+place+'</div></div>',
        flat: true,
        anchor: RichMarkerPosition.TOP_LEFT,
    });

The richmarker is anchored on the map marker. I'd like to tell the Google maps api to show the richmarker at the center of the map. Thanks.

map.setCenter(lat_lon) 

...will center on the location. If you want to center on the RichMarker, you'll have to do some math, by getting the ContainerPixel or DivPixel center of the RichMarker and then using...

MapCanvasProjection.fromContainerPixelToLatLong(pixel:Point) or MapCanvasProjection.fromDivPixelToLatLng(pixel:Point) to center on that using map.setCenter(MapCanvasProjection.fromDivPixelToLatLng(pixel:Point)), for example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM