javascript - Google V3 添加点击监听器

标签 javascript google-maps-api-3

我是 JavaScript 新手,我想从 Google V2 迁移到 Google v3,为此我只是稍微更改了 V2 中使用的方法,所有方法都可以正常工作,但是当我尝试更改 addListener从V2到V3我面临着像v2一样的问题,使用相同的监听器将标记放在 map 上,但是当涉及到V3时,我无法在 map 上放置标记。这里我发布v2代码和V3代码。请帮我解决这个问题。 V2 是:

GEvent.addListener(map, "click", function(marker, point) {
                console.debug('after Click map is '+map+' marker is '+marker+' point is '+point);
                if (marker) {
                        if(PolygonMarkers.length == 1){ //Only one marker in the array
                            map.removeOverlay(PolygonMarkers[0]);
                            map.removeOverlay(PolygonMarkers[0]);
                            PolygonMarkers = [];
                            if(Polygon){map.removeOverlay(Polygon)};
                        } else{                     /*More then one marker*/
                            var RemoveIndex = -1;
                            var Remove;
                            //Search for clicked Marker in PolygonMarkers Array
                            for(var m=0; m<PolygonMarkers.length; m++)
                            {
                                if(PolygonMarkers[m].getPoint().equals(marker.getPoint()))
                                {
                                    RemoveIndex = m; Remove = PolygonMarkers[m];
                                    break;
                                }
                            }
                            //Shift Array elements to left
                            for(var n=RemoveIndex; n<PolygonMarkers.length-1; n++)
                            {
                                PolygonMarkers[n] = PolygonMarkers[n+1];
                            }
                            PolygonMarkers.length = PolygonMarkers.length-1 //Decrease Array length by 1
                            map.removeOverlay(Remove); //Remove Marker
                            geofencedetails.drawPolygon(); //Redraw Polygon
                          }  

                        allMarkers = PolygonMarkers;

                  } else {
                    // Adds a new Polygon boundary marker 
                        var markerOptions = { icon: icon, draggable: true };
                        var marker = new GMarker(point, markerOptions);
                        PolygonMarkers.push(marker); //Add marker to PolygonMarkers array
                        map.addOverlay(marker); //Add marker on the map
                        GEvent.addListener(marker,'dragstart',function(){ //Add drag start event
                            marker.setImage(icon.image);
                            polygon_resizing = true;
                        });
                        GEvent.addListener(marker,'drag',function(){ 
                            geofencedetails.drawPolygon(); 
                            }); //Add drag event
                        GEvent.addListener(marker,'dragend',function(){   //Add drag end event
                            marker.setImage(icon.image);
                            polygon_resizing = false;
                            geofencedetails.drawPolygon();
                        });
                        geofencedetails.drawPolygon();

                        allMarkers = PolygonMarkers;
                 }
                });`

V3 是

        google.maps.event.addListener(map, "click", function(marker, point) {
            if (marker) {
                //console.debug('marker '+marker.toSource());
                if(PolygonMarkers.length == 1){ //Only one marker in the array
                    map.removeOverlay(PolygonMarkers[0]);
                map.removeOverlay(PolygonMarkers[0]);
                PolygonMarkers = [];
                if(Polygon){
                    map.removeOverlay(Polygon)
                };
                }else {                     /*More then one marker*/
                    //console.debug('PolygonMarkers.length is '+PolygonMarkers.length);
                    var RemoveIndex = -1;
                    var Remove;
                    //Search for clicked Marker in PolygonMarkers Array
                    for(var m=0; m<PolygonMarkers.length; m++){
                        //console.debug('PolygonMarkers['+m+'] '+PolygonMarkers[m]);
                        if(PolygonMarkers[m].getPosition().equals(marker.getPosition())){
                            //console.debug('Both are equal ');
                            RemoveIndex = m;
                            Remove = PolygonMarkers[m];
                            break;
                        }
                    }
                    //Shift Array elements to left
                    for(var n=RemoveIndex; n<PolygonMarkers.length-1; n++){
                        PolygonMarkers[n] = PolygonMarkers[n+1];
                    }
                    PolygonMarkers.length = PolygonMarkers.length-1 //Decrease Array length by 1
                    /*map.removeOverlay(Remove); //Remove Marker*/
                    for (var i = 0; i < allMarkers.length; i++ ) {
                        allMarkers[i].setMap(null);
                    }
                    geofencedetails.drawPolygon(); //Redraw Polygon
                }  

                allMarkers = PolygonMarkers;

            } else {
                //console.debug('In else block');
                // Adds a new Polygon boundary marker 
                var markerOptions = { icon: icon, draggable: true };
                //console.debug('1');
                var marker = new GMarker(point, markerOptions);
                //console.debug('2');
                PolygonMarkers.push(marker); //Add marker to PolygonMarkers array
                //console.debug('3');
                map.addOverlay(marker); //Add marker on the map
                //console.debug('4');
                GEvent.addListener(marker,'dragstart',function(){ //Add drag start event
                    //console.debug('5');
                marker.setImage(icon.image);
                //console.debug('6');
                polygon_resizing = true;
                });
                GEvent.addListener(marker,'drag',function(){ 
                    geofencedetails.drawPolygon(); 
                }); //Add drag event
                GEvent.addListener(marker,'dragend',function(){   //Add drag end event
                    marker.setImage(icon.image);
                polygon_resizing = false;
                geofencedetails.drawPolygon();
                });
                geofencedetails.drawPolygon();

                allMarkers = PolygonMarkers;
            }
        });

最佳答案

您需要将点击事件绑定(bind)到标记而不是 map 。

google.maps.event.addListener(marker, "click", function(event) { .... } );

UI events within the Google Maps API V3 typically pass an event argument, which can be accessed by the event listener, noting the UI state when the event occurred. For example, a UI 'click' event typically passes a MouseEvent containing a latLng property denoting the clicked location on the map. Note that this behavior is unique to UI events; MVC state changes do not pass arguments in their events.

关于javascript - Google V3 添加点击监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13838062/

相关文章:

javascript - 是否可以在 Canvas 内拖动图像并在同一 Canvas 上绘图?

javascript - Google map 地理编码无法正常工作(无标记)

javascript - 在弹出的谷歌地图 API 信息窗口中,何时获取图像?

javascript - 如何检查用户是否属于共享点广告组或仅使用 REST/Javascript?

javascript - 使用 Google Maps API 添加标记功能

javascript - InfoWindow 打开缓慢或根本不打开

javascript - 在谷歌地图上绘制区域

javascript - HTML5 Konva 类重定位

javascript - (Javascript) 如何根据下拉列表中的选定选项更改变量值?

javascript - 这两个代码块之间的差异