Prior research has shown that environmental hazards, such as limited green space, air pollution, and harmful weather, have the strong adverse impact on older adults' cognitive function; however, most of the studies were conducted in developed countries and limited to cross-sectional analyses. China has the largest aging population in the world so the research evidence from it can offer an insight to the study in other developing countries facing similar issues and inform future public health policy and disease control. This study examined the long-term impact of environmental factors, namely, green space coverage, air pollution, and weather conditions on cognitive function using a nationally representative sample consisting of adults aged 45 years and older selected from the China Health and Retirement Longitudinal Study (CHARLS 2011–2018), the China City Statistical Yearbook, and other sources. Multilevel growth curve models were utilized for analysis and the mediator effects of physical activity and social engagement on the relationship between environmental factors and cognitive function were examined. Findings of this study showed that after controlling for sociodemographic characteristics, annual precipitation of 80 cm or more, living in areas with July temperature of 28°C or higher, urban community, and green space coverage were positively associated with cognition score at the baseline and lower precipitation, urban community, and greater green space coverage were associated with slower cognitive decline over a 7-year period. The impact of gross domestic product (GDP) seemed to take into effect more and more over time. These effects did not substantially change after weekly total hours of physical activities and levels of social engagement were added. More research on the mechanisms of the effect of environmental factors on cognition is needed such as the subgroup analyses and/or with more aspects of environmental measures.