According to Wikipedia:
"King Mengrai founded the city of Chiang Mai (meaning "new city") in 1296, and it succeeded Chiang Rai as capital of the Lanna kingdom. The monarch was called Chao. To protect it against raids from Burma, the city was surrounded by a moat and a defensive wall. With the decline in power of the Lannathai kingdom, the city lost importance and often was occupied either by the Burmese or Thais from Ayutthaya. As a result of the Burmese wars that ended with the fall of Ayutthaya in April 1767, Chiang Mai was depopulated and its remaining inhabitants abandoned the city from 1776 to 1791. During that time, Lampang functioned as the capital of what remained of Lannathai. Chiang Mai formally became part of Siam in 1774 in an agreement with Kavila, when the Thai King Taksin helped recapture it from the Burmese. Chiang Mai rose in cultural, trading and economic importance to its current status as the unofficial capital of northern Thailand, second only in national importance to Bangkok."