最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Maps How to add Image into InfoWindow - Stack Overflow

programmeradmin7浏览0评论

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?

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?

Share Improve this question asked Aug 20, 2013 at 11:19 user2527785user2527785 1171 gold badge2 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

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).

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"
}
发布评论

评论列表(0)

  1. 暂无评论