Section 22. The route fixed under the two preceding sections may include such spurs, branches and connecting and terminal tracks in any city or town as may be necessary to enable the corporation conveniently to collect and deliver passengers and freight therein; but no such branches, spurs or connecting or terminal tracks shall be laid longitudinally within the limits of a public way without the consent of the board of aldermen or the selectmen, who, in giving such consent, may impose such conditions as to the location, construction and use thereof as may be agreed upon between themselves and the directors. A corporation which owns or operates any such tracks so laid longitudinally in a public way shall, in respect to the same, be liable to the city or town for all loss or damage caused to it by the construction and use of such tracks and by the negligence or default of the agents or workmen of such corporation on such way.