在Google Maps的infowindow中添加图片?

时间:2015-11-18 19:36:39

标签: javascript api google-maps

我想在infowindow的地址旁边添加一个浮动:左侧标识,我该怎么做? 这是我的代码:

<script>
  function initialize() {
    var mapCanvas = document.getElementById('map');
    var mapOptions = {
      center: new google.maps.LatLng(18.5231,-72.2929),
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      styles: 
      [{"featureType":"landscape","stylers":[{"hue":"#FFBB00"},{"saturation":63.400000000000006},{"lightness":37.599999999999994},{"gamma":1}]},{"featureType":"road.highway","stylers":[{"hue":"#FFC200"},{"saturation":-61.8},{"lightness":45.599999999999994},{"gamma":1}]},{"featureType":"road.arterial","stylers":[{"hue":"#FF0300"},{"saturation":-100},{"lightness":51.19999999999999},{"gamma":1}]},{"featureType":"road.local","stylers":[{"hue":"#FF0300"},{"saturation":-100},{"lightness":52},{"gamma":1}]},{"featureType":"water","stylers":[{"hue":"#0078FF"},{"saturation":-13.200000000000003},{"lightness":2.4000000000000057},{"gamma":1}]},{"featureType":"poi","stylers":[{"hue":"#008349"},{"saturation":-1.0989010989011234},{"lightness":11.200000000000017},{"gamma":1}]}]
    }
    var map = new google.maps.Map(mapCanvas, mapOptions)
    marker = new google.maps.Marker({
        map: map, 
        position: new google.maps.LatLng(18.5231,-72.2929),
        icon: "img/mapmarker.png"
        });
    infowindow = new google.maps.InfoWindow({
        content:"<b>Title</b><br/>123 Address<br/> City,Country" });
        google.maps.event.addListener(marker, "click", function(){infowindow.open(map,marker);});infowindow.open(map,marker);
  }
    google.maps.event.addDomListener(window, 'load', initialize);
</script>

2 个答案:

答案 0 :(得分:4)

最简单的方法。在infowindow中添加d​​iv float:left

infowindow = new google.maps.InfoWindow({
    content: "<div style='float:left'><img src='http://i.stack.imgur.com/g672i.png'></div><div style='float:right; padding: 10px;'><b>Title</b><br/>123 Address<br/> City,Country</div>"
});

proof of concept fiddle

代码段

&#13;
&#13;
var geocoder;
var map;

function initialize() {
  var mapCanvas = document.getElementById('map');
  var mapOptions = {
    center: new google.maps.LatLng(18.5231, -72.2929),
    zoom: 14,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    styles: styles
  }
  var map = new google.maps.Map(mapCanvas, mapOptions)
  marker = new google.maps.Marker({
    map: map,
    position: new google.maps.LatLng(18.5231, -72.2929),
    icon: "img/mapmarker.png"
  });
  infowindow = new google.maps.InfoWindow({
    content: "<div style='float:left'><img src='http://i.stack.imgur.com/g672i.png'></div><div style='float:right; padding: 10px;'><b>Title</b><br/>123 Address<br/> City,Country</div>"
  });
  google.maps.event.addListener(marker, "click", function() {
    infowindow.open(map, marker);
  });
  infowindow.open(map, marker);
}
google.maps.event.addDomListener(window, 'load', initialize);
var styles = [{
  "featureType": "landscape",
  "stylers": [{
    "hue": "#FFBB00"
  }, {
    "saturation": 63.400000000000006
  }, {
    "lightness": 37.599999999999994
  }, {
    "gamma": 1
  }]
}, {
  "featureType": "road.highway",
  "stylers": [{
    "hue": "#FFC200"
  }, {
    "saturation": -61.8
  }, {
    "lightness": 45.599999999999994
  }, {
    "gamma": 1
  }]
}, {
  "featureType": "road.arterial",
  "stylers": [{
    "hue": "#FF0300"
  }, {
    "saturation": -100
  }, {
    "lightness": 51.19999999999999
  }, {
    "gamma": 1
  }]
}, {
  "featureType": "road.local",
  "stylers": [{
    "hue": "#FF0300"
  }, {
    "saturation": -100
  }, {
    "lightness": 52
  }, {
    "gamma": 1
  }]
}, {
  "featureType": "water",
  "stylers": [{
    "hue": "#0078FF"
  }, {
    "saturation": -13.200000000000003
  }, {
    "lightness": 2.4000000000000057
  }, {
    "gamma": 1
  }]
}, {
  "featureType": "poi",
  "stylers": [{
    "hue": "#008349"
  }, {
    "saturation": -1.0989010989011234
  }, {
    "lightness": 11.200000000000017
  }, {
    "gamma": 1
  }]
}];
&#13;
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js?"></script>
<div id="map"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

只需将<img ...添加到您的内容

即可
infowindow = new google.maps.InfoWindow({
    content:"<img src='yourpath/yourlogo'><b>Title</b><br/>123 Address<br/> City,Country" });
    google.maps.event.addListener(marker, "click", function(){infowindow.open(map,marker);});infowindow.open(map,marker);