Google Maps How to add Image into InfoWindow

2020-03-30 15:42发布

Hi I am trying to add an image into a google maps infoWindow, my code is like this a script

 var ContactUs = function () {

return {
    //main function to initiate the module
    init: function () {
        var map;
        $(document).ready(function(){
          map = new GMaps({
            div: '#map',
            lat: -13.004333,
            lng: -38.494333,
          });
           var marker = map.addMarker({
                lat: -13.004333,
                lng: -38.494333,
                title: 'Loop, Inc.',
                infoWindow: {
                    content: "<b>Loop, Inc.</b> 795 Park Ave, Suite 120<br>San Francisco, CA 94107"
                }
            });

           marker.infoWindow.open(map, marker);
        });
    }
};

 }();

Then in my HTML it gets called like this:

  <div class="row-fluid">
     <div id="map" class="gmaps margin-bottom-40" style="height:400px;"></div>
 </div>

I tried adding an image tag into the script file but it doesn't work, I understand I have to add some code in the html file instead but not sure as to what?

2条回答
霸刀☆藐视天下
2楼-- · 2020-03-30 16:18

You add your HTML to reference the image in the InfoWindow "content"

       var marker = map.addMarker({
            lat: -13.004333,
            lng: -38.494333,
            title: 'Loop, Inc.',
            infoWindow: {
                content: "<b>Loop, Inc.</b> 795 Park Ave, Suite 120<br>San Francisco, CA 94107<br><img src='myimage.jpg' alt='image in infowindow'>"
            }
        });

Above assumes the image "myimage.jpg" in the local directory. You can also use an absolute URL. Be careful of mixing " (double quotes) and ' (single quotes).

查看更多
在下西门庆
3楼-- · 2020-03-30 16:29

Use this, I tried this one in my app:

infoWindow.setContent(html);
infoWindow.open(map, marker);

Instead of:

infoWindow: {
    content: "<b>Loop, Inc.</b> 795 Park Ave, Suite 120<br>San Francisco, CA 94107"
}
查看更多
登录 后发表回答