angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

What is the correct way to add events to new polygons added to the polygon directive?

Open aplus01 opened this issue 11 years ago • 4 comments

Right now I'm attaching map event listeners to new polygons when they are added to the polygon scope object. Is this the correct way to do this? This isn't something that the polygon directive will do is it?

I'm assuming it is correct since if I do not add click event listeners to new polygons the event specified in the polygon directive's click event does not fire

Markup:

            <ui-gmap-google-map center='map.center' zoom='map.zoom' options="map.options" control="googleMapCtrl" events="map.events">
                <ui-gmap-polygon ng-repeat="p in polygons track by p.id" fill="p.fill"
                path="p.path" stroke="p.stroke" clickable="true" visible="p.visible" draggable="true" events="polyEvents"/>
            </ui-gmap-google-map>

Javascript code responsible for adding new polygons:

            var loadField = function(fieldId) {
                mapViewDataService.getFieldBoundaryById(fieldId).then(function(boundaryData) {
                    var fieldBoundary = JSON.parse(boundaryData);
                    var newPoly = new google.maps.Polygon(
                        {
                            id: fieldId,
                            selected:false,
                            paths: google.maps.geometry.encoding.decodePath(fieldBoundary[0].googlePolyLine),
                            strokeColor: '#59A7DE',
                            strokeOpacity: 1,
                            strokeWeight: 3,
                            fillColor: '#22616F',
                            fillOpacity: .8,
                            clickable: true,
                            visible: true
                        });

                    google.maps.event.addListener(newPoly, "click", function() {
                        console.debug('clicked on new poly');
                    });
                    google.maps.event.addListener(newPoly, "rightclick", function() {
                        console.debug('right clicked on new poly');
                    });

                    newPoly.setMap(getMapObject());
                    $scope.polygons.push(newPoly);

                });
            };

aplus01 avatar Dec 18 '14 18:12 aplus01

No please see other examples with markers and etc. it is the same concept. Your doing a very function approach using a lot of the google sdk. The markup you use with ui-gmap is so you can avoid the js you have .

nmccready avatar Dec 18 '14 18:12 nmccready

Can you please point me to a specific example?

The colleciton of polygons I'm attaching to the ui-gmap-polygon is empty on page load.

I add to that collection of polygons as a result of user interaction (selecting geographical areas to load polygons on). This is why I assumed I needed to rely on the google sdk so much. I am not sure how to add polygons at runtime without use of the google maps sdk.

aplus01 avatar Dec 18 '14 19:12 aplus01

https://github.com/angular-ui/angular-google-maps/blob/master/example/assets/scripts/controllers/issue-663-marker-events-once.js#L9-L25

nmccready avatar Dec 18 '14 21:12 nmccready

https://github.com/angular-ui/angular-google-maps/blob/master/example/assets/scripts/controllers/issue-663-marker-events-once.js#L79-L89

nmccready avatar Dec 18 '14 21:12 nmccready