code: def closest_waypoint(self, latlon):
          '''find closest waypoint to a position'''
          (lat, lon) = latlon
          best_distance = -1
          closest = -1
          for i in range(self.module('wp').wploader.count()):
              w = self.module('wp').wploader.wp(i)
              distance = mp_util.gps_distance(lat, lon, w.x, w.y)
              if best_distance == -1 or distance < best_distance:
                  best_distance = distance
                  closest = i
          if best_distance < 20:
              return closest
          else:
              return -f closest_waypoint(self, latlon):
          '''find closest waypoint to a position'''
          (lat, lon) = latlon
          best_distance = -1
          closest = -1
          for i in range(self.module('wp').wploader.count()):
              w = self.module('wp').wploader.wp(i)
              distance = mp_util.gps_distance(lat, lon, w.x, w.y)
              if best_distance == -1 or distance < best_distance:
                  best_distance = distance
                  closest = i
          if best_distance < 20:
              return closest
          else:
              return -1
docs: find closest waypoint to a position