简体   繁体   English

带有InfoWindows的Google Maps Javascript

[英]Google Maps Javascript with InfoWindows

I have a javascript file like below that is init in contact us page. 我有一个如下所示的javascript文件,该文件位于“与我们联系”页面中。 I added almost everything that i wanted but could't figure out how to set working info windows for each marker. 我几乎添加了我想要的所有内容,但无法弄清楚如何为每个标记设置工作信息窗口。 In facts i understand how to set and use infoWindow but don't know where to put it in this code. 实际上,我了解如何设置和使用infoWindow,但不知道在此代码中的位置。

var ContactUs = function () {
    return {
        //main function to initiate the module
        init: function () {
            var neighborhoods = [
              { lat: 41.02688344, lng: 28.97104517, icon: '../Content/blue_MarkerSS.png', content: "a" },
              { lat: 41.07992535, lng: 29.02025431, icon: '../Content/blue_MarkerL.png', content: "b" },
              { lat: 41.059097, lng: 28.983857, icon: '../Content/blue_MarkerB.png', content: "c" },
              { lat: 41.08476948, lng: 28.97748649, icon: '../Content/blue_MarkerK.png', content: "d" },
              { lat: 41.05635465, lng: 28.95114452, icon: '../Content/red_MarkerS.png', content: "e" }
            ];

            var markers = [];
            var map;

            map = new google.maps.Map(document.getElementById("map"), {
                zoom: 12,
                center: { lat: 41.052244, lng: 28.985637 }
            });

            function addMarkerWithTimeout(position, timeout, icon, content) {
                window.setTimeout(function () {
                    markers.push(new google.maps.Marker({
                        position: position,
                        map: map,
                        animation: google.maps.Animation.DROP,
                        icon: icon
                    }));
                }, timeout);
            }

            for (var i = 0; i < neighborhoods.length; i++) {
                addMarkerWithTimeout(neighborhoods[i], i * 300, neighborhoods[i].icon, neighborhoods[i].content);
            }
        }
    };
}();

UPDATE: 更新:

I have a working script like that contains infoWindows. 我有一个包含infoWindows的工作脚本。 I want to add it addMarkerWithTimeout as in first question. 我想将其添加为addMarkerWithTimeout作为第一个问题。 Think about merge two scripts that will contain infoWindows and addMarkerWithTimeout in one. 考虑将两个脚本合并在一起,这两个脚本将包含infoWindows和addMarkerWithTimeout。 My problem is just this. 我的问题就是这样。

Original addMarkerWithTimeout Example is HERE (i don't want that button)! 原始的addMarkerWithTimeout示例在此处 (我不想要该按钮)!

var ContactUs = function () {
    return {
        init: function () {
            var locations = [
              ['a', 41.02688344, 28.97104517, 4, './Content/blue_MarkerSS.png'],
              ['b', 41.07992535, 29.02025431, 5, '../Content/blue_MarkerSS.png'],
              ['c', 41.059097, 28.983857, 3, '../Content/blue_MarkerSS.png'],
              ['d', 41.08476948, 28.97748649, 2, '../Content/blue_MarkerK.png'],
              ['e', 41.05635465, 28.95114452, 1, '../Content/red_MarkerS.png']
            ];

            var map = new google.maps.Map(document.getElementById('map'), {
                zoom: 12,
                center: new google.maps.LatLng(41.052244, 28.985637),
                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: locations[i][4]
                });

                google.maps.event.addListener(marker, 'click', (function (marker, i) {
                    return function () {
                        infowindow.setContent(locations[i][0]);
                        infowindow.open(map, marker);
                    }
                })(marker, i));
            }
        }
    };
}();
  • keep references to your markers. 保留对标记的引用。 Instead of: 代替:
markers.push(new google.maps.Marker({
                position: position,
                map: map,
                animation: google.maps.Animation.DROP,
                icon: icon
           }));

do: 做:

var marker = new google.maps.Marker({
                    position: position,
                    map: map,
                    animation: google.maps.Animation.DROP,
                    icon: icon
                });
markers.push(marker);
  • Then add the click listener to the marker (or you can add it to markers[markers.length-1] ...): 然后将点击侦听器添加到标记中(或者您可以将其添加到markers[markers.length-1] ...):
google.maps.event.addListener(marker,'click', function(e) {
  infowindow.setContent(content);
  infowindow.open(map,marker);
});

proof of concept fiddle 概念证明

code snippet: 代码段:

 var ContactUs = function() { return { //main function to initiate the module init: function() { var markers = []; var map; var infowindow = new google.maps.InfoWindow(); map = new google.maps.Map(document.getElementById("map"), { zoom: 12, center: { lat: 41.052244, lng: 28.985637 } }); function addMarkerWithTimeout(position, timeout, icon, content) { window.setTimeout(function() { var marker = new google.maps.Marker({ position: position, map: map, animation: google.maps.Animation.DROP, icon: icon }); google.maps.event.addListener(marker, 'click', function(e) { infowindow.setContent(content); infowindow.open(map, marker); }); markers.push(marker); }, timeout); } var neighborhoods = [{ lat: 41.02688344, lng: 28.97104517, icon: 'http://maps.google.com/mapfiles/ms/micons/blue.png', content: "a" }, { lat: 41.07992535, lng: 29.02025431, icon: 'http://maps.google.com/mapfiles/ms/micons/green.png', content: "b" }, { lat: 41.059097, lng: 28.983857, icon: 'http://maps.google.com/mapfiles/ms/micons/yellow.png', content: "c" }, { lat: 41.08476948, lng: 28.97748649, icon: 'http://maps.google.com/mapfiles/ms/micons/orange.png', content: "d" }, { lat: 41.05635465, lng: 28.95114452, icon: 'http://maps.google.com/mapfiles/ms/micons/red.png', content: "e" }]; for (var i = 0; i < neighborhoods.length; i++) { addMarkerWithTimeout(neighborhoods[i], i * 300, neighborhoods[i].icon, neighborhoods[i].content); } } }; }(); ContactUs.init(); 
 html, body, #map { height: 100%; width: 100%; margin: 0px; padding: 0px } 
 <script src="https://maps.googleapis.com/maps/api/js"></script> <div id="map" style="border: 2px solid #3872ac;"></div> 

The documentation on this is very good: docs 关于此的文档非常好: docs

According to the docs all you need to do is initialize the info window and then add an event handler or however you want to trigger it and call: 根据文档,您所需要做的就是初始化信息窗口,然后添加事件处理程序,或者您想触发它并调用:

infowindow.open(map, marker);

The IIFE you are using would suggest using an event handler would be best. 您正在使用的IIFE建议最好使用事件处理程序。 Otherwise you could add an additional closure that triggers the open method and call this whenever and wherever you want. 否则,您可以添加一个额外的闭包来触发open方法,并在任何时候,任何地方调用它。

(For your problem and future users) (针对您的问题和未来的用户)

You can refference yourself at this extremly good website which concerns your issue (google maps v3): 您可以在这个与您的问题有关的极好网站上推荐自己(google maps v3):

URL: http://wrightshq.com/playground/placing-multiple-markers-on-a-google-map-using-api-3/ 网址: http : //wrightshq.com/playground/placing-multiple-markers-on-a-google-map-using-api-3/

Also I suggest to visit a popular stackoverflow thread which again deals with your issue (you can also get a better insigth on closures if you don't have one already) 我也建议访问一个流行的stackoverflow线程,该线程再次处理您的问题(如果还没有闭包,您也可以更好地了解闭包)

URL: Adding multiple markers with infowindows (Google Maps API) 网址: 使用信息窗口添加多个标记(Google Maps API)

With this two links and the official google maps web on infowindows 有了这两个链接和infowindows上的官方google maps网站

URL: https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple 网址: https//developers.google.com/maps/documentation/javascript/examples/infowindow-simple

you should have no problem whatsoever to solve your problem. 您应该没有任何问题可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM