【问题标题】:Two marker with two different image in Google map API谷歌地图API中有两个不同图像的两个标记
【发布时间】:2020-02-19 02:42:37
【问题描述】:

我有两个标记我想让每个标记都有自己的图像,这里显示两个标记用同一张图片我不希望它重复图片我想要两张不同的图片。 请帮助我,真诚地感谢您。

<html>

<head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>Google Maps Multiple Markers</title>
    <script src="http://maps.google.com/maps/api/js?key= AIzaSyCpuOArmqJRLE8nl9V_g-KH7M8zwhsnFf0&callback=intiMap" type="text/javascript"></script>

</head>

<body>

    <div id="map" style="width: 800px; height: 600px;"></div>

    <script type="text/javascript">
        var locations = [
            ['جامعة الملك سعود', 24.729004, 46.624154, 1],
            ['جامعة الإمام محمد بن سعود', 24.814796, 46.7127355, 2],

        ];

        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 10,
            center: new google.maps.LatLng(24.6742437, 46.7759905),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var infowindow = new google.maps.InfoWindow();

        var marker, i;

        for (i = 0; i < locations.length; i++) {
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                map: map,
                icon: {
                    url: "https://3.top4top.net/p_1390st3q12.png"
                },
                icon: {
                    url: "https://6.top4top.net/p_1390latqo1.png"
                }
            });

            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(locations[i][0]);
                    infowindow.open(map, marker);
                }
            })(marker, i));
        }
    </script>
</body>

</html> ```

【问题讨论】:

    标签: javascript html api google-maps google-maps-api-3


    【解决方案1】:

    您所要做的就是创建图标数组并在迭代中访问它。

    看看我的 sn-p,祝你有美好的一天 :)

       <html>
    
        <head>
    
            <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
            <title>Google Maps Multiple Markers</title>
            <script src="http://maps.google.com/maps/api/js?key= AIzaSyCpuOArmqJRLE8nl9V_g-KH7M8zwhsnFf0&callback=intiMap" type="text/javascript"></script>
    
        </head>
    
        <body>
    
            <div id="map" style="width: 800px; height: 600px;"></div>
    
            <script type="text/javascript">
                var locations = [
                    ['جامعة الملك سعود', 24.729004, 46.624154, 1],
                    ['جامعة الإمام محمد بن سعود', 24.814796, 46.7127355, 2],
    
                ];
    
                var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 10,
                    center: new google.maps.LatLng(24.6742437, 46.7759905),
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                });
    
                var infowindow = new google.maps.InfoWindow();
    
                var marker, i;
    
                // make new array for icons
                var icons = [
                  "https://3.top4top.net/p_1390st3q12.png",
                  "https://6.top4top.net/p_1390latqo1.png"
                ]
    
                for (i = 0; i < locations.length; i++) {
                    marker = new google.maps.Marker({
                        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                        map: map,
                        icon: {
                            // access icons by iteration
                            url: icons[i]
                        }
                    });
    
                    google.maps.event.addListener(marker, 'click', (function(marker, i) {
                        return function() {
                            infowindow.setContent(locations[i][0]);
                            infowindow.open(map, marker);
                        }
                    })(marker, i));
                }
            </script>
        </body>
    
        </html>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2020-07-02
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      相关资源
      最近更新 更多