The mayor and aldermen or board of common council of any city, and the trustees of any incorporated town, through which the lines of any telephone or telegraph company are to pass, may, by ordinance or otherwise, specify where the posts, piers or abutments shall be located, the kind of posts that shall be used, the height at which the wires shall be run; and such company shall be governed by the regulations thus prescribed; and after the erection of said telephone or telegraph lines, the said mayor and aldermen, or board of common council, and the trustees of any incorporated town, shall have power to direct any alteration in the location or erection of said posts, piers or abutments, and also in the height at which the wires shall run, having first given such company or its agents opportunity to be heard in regard to such alteration.