A method to determine distances to molecular clouds using near-IR photometry