【问题标题】:GoogleMaps api: How to place images in the content of Multiple InfoWindows?Google Maps api:如何将图像放置在多个 InfoWindows 的内容中?
【发布时间】:2013-05-15 19:42:37
【问题描述】:

简而言之,我想把这两个主题结合起来:

How to add an image to InfoWindow of marker in google maps v3?

Google Maps JS API v3 - Simple Multiple Marker Example

我将我的位置数据存储在数据库中,并将其作为数组检索。我想为数据库中的 x 个位置中的每一个放置一个标记和信息窗口。其中一个字段包含一个图像,我想将其包含在 infoWindow 内容中。

这是我目前所处的位置:

PHP: // 创建连接 $con=mysqli_connect("*******","*******","*******","tests");

// Check connection
if (mysqli_connect_errno($con))
{
 echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT Name, Latitude, Longitude, Image FROM locate_coords");


while($row = mysqli_fetch_array($result))
{
$users[]= $row;

}

Javascriptfunction initialize():

<?php
$js_array = json_encode($users);
echo "var locations = ". $js_array . ";\n";
?>

var styles = [
   {
     stylers: [
       { hue: "#00ffe6" },
       { saturation: -20 },
     ]
   },{
     featureType: "road",
     elementType: "geometry",
     stylers: [
       { lightness: 100 },
       { visibility: "simplified" },
     ]
   },{
     featureType: "road",
     elementType: "labels",
     stylers: [
       { visibility: "off" }
     ]
   }
 ];

var styledMap = new google.maps.StyledMapType(styles,
   {name: "Styled Map"});

var mapProp = {
   zoom: 12,
   center: new google.maps.LatLng(51.508742,-0.120850),
   mapTypeControlOptions: {
     mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'map_style']
   }
 };

 var map = new google.maps.Map(document.getElementById('map-canvas'),
   mapProp);


   var infowindow = new google.maps.InfoWindow();
   var marker, i;

   for (i = 0; i < locations.length; i++) {  

    //var container = document.getElementById("photo");

    //var img = new Image();
    //img.src = '
    //container.appendChild(img);

     marker = new google.maps.Marker({
       position: new google.maps.LatLng(locations[i][1], locations[i][2]),
       map: map
     });

     google.maps.event.addListener(marker, 'click', (function(marker, i) {
       return function() {
        //infowindow.setContent('<img id=="photo">' + locations[i][0]);
        infowindow.setContent('<img src=/uploads/'. locations[i][3]'>' + locations[i][0]);
        infowindow.open(map, marker);
       }
     })(marker, i));

   }

map.mapTypes.set('map_style', styledMap);
map.setMapTypeId('map_style');
}

google.maps.event.addDomListener(window, 'load', initialize);

放入一个普通的图片 url 效果很好,但是一旦我尝试放入一个数组值,地图就不想加载了!在这一点上,我真的可以用理智的头脑告诉我该怎么做:-(..

【问题讨论】:

    标签: javascript arrays image google-maps infowindow


    【解决方案1】:

    这行有错别字

    infowindow.setContent('<img src=/uploads/'. locations[i][3]'>' + locations[i][0]);
    

    。不是连接运算符,在locations[i][3]'&gt;' 之间根本没有连接运算符。正确的代码是:

    infowindow.setContent('<img src=/uploads/' + locations[i][3] + '>' + locations[i][0]);
    

    【讨论】:

    • 天哪!我知道这将是微不足道的事情!非常感谢!!那让我非常高兴/松了一口气。我希望我能投票给你,但显然我必须有 15 个代表。
    猜你喜欢
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 2023-03-30
    • 2011-06-21
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多