javascript - set_at called multiple times in Google Maps V3 API -
poly = new google.maps.polyline(polyoptions); poly.setmap(map); google.maps.event.addlistener(map, 'click', addlatlng); function addlatlng(event) { var path = poly.getpath(); // because path mvcarray, can append new coordinate // , automatically appear path.push(event.latlng); // add new marker @ new plotted point on polyline. var marker = new google.maps.marker({ position: event.latlng, title: '#' + path.getlength(), map: map, dragable: false, clickable: true, name: name, raiseondrag: false, }); //this event fired when setat() called. event passes index passed setat() , element in array @ index. google.maps.event.addlistener(path, 'set_at', function(index,oldwp) { //called when editing path //we need remove (current) marker , add new 1 @ position }); google.maps.event.addlistener(path, 'insert_at', function(index) { //when insert happens not @ end of path somewhere in middle //we need rerender makers }); }
google documentation: this event fired when setat() called. event passes index passed setat() , element in array @ index.
however event called x times (where x # elem of path).
anybody know why , if can prevented (asside keeping counter in memory).
what doing here adding new listeners path each time path changes. want add listeners once. think should trick:
poly = new google.maps.polyline(polyoptions); poly.setmap(map); var path = poly.getpath(); google.maps.event.addlistener(map, 'click', addlatlng); function addlatlng(event) { // because path mvcarray, can append new coordinate // , automatically appear path.push(event.latlng); // add new marker @ new plotted point on polyline. var marker = new google.maps.marker({ position: event.latlng, title: '#' + path.getlength(), map: map, dragable: false, clickable: true, name: name, raiseondrag: false, }); } //this event fired when setat() called. event passes index passed setat() , element in array @ index. google.maps.event.addlistener(path, 'set_at', function(index,oldwp) { //called when editing path //we need remove (current) marker , add new 1 @ position }); google.maps.event.addlistener(path, 'insert_at', function(index) { //when insert happens not @ end of path somewhere in middle //we need rerender makers });
Comments
Post a Comment