Previous studies have paid little attention to the spatial heterogeneity of residents' marginal willingness to pay (MWTP) for clean air at a city level. To fill this gap, this study adopts a geographically weighted regression (GWR) model to quantify the spatial heterogeneity of residents' MWTP for clean air in Shanghai. First, Shanghai was divided into 218 census tracts and each tract was the smallest research unit. Then, the impacts of air pollutants and other built environment variables on housing prices were chosen to reflect residents' MWTP and a GWR model was used to analyze the spatial heterogeneity of the MWTP. Finally, the total losses caused by air pollutants in Shanghai were estimated from the perspective of housing market value. Empirical results show that air pollutants have a negative impact on housing prices. Using the marginal rate of transformation between housing prices and air pollutants, the results show Shanghai residents, on average, are willing to pay 50 and 99 Yuan/m2 to reduce the mean concentration of PM2.5 and NO2 by 1 μg/m3, respectively. Moreover, residents' MWTP for clean air is higher in the suburbs and lower in the city center. This study can help city policymakers formulate regional air management policies and provide support for the green and sustainable development of the real estate market in China.