Şu satırı dahil ederiz
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed;
Örnek
Şöyle yaparız
import org.springframework.data.mongodb.core.index.GeoSpatialIndexed; import org.springframework.data.mongodb.core.mapping.Document; @Document public class User { private String id; private String name; @GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE) private Location location; private List<String> friendIds; // getters and setters omitted for brevity } @Document public class Place { private String id; private String name; @GeoSpatialIndexed(type = GeoSpatialIndexType.GEO_2DSPHERE) private Location location; // getters and setters omitted for brevity } public class Location { private double x; private double y; // getters and setters omitted for brevity }
Şöyle yaparız
public interface UserRepository extends MongoRepository<User, String> {} public interface PlaceRepository extends MongoRepository<Place, String> {} @Service public class UserService { @Autowired private UserRepository userRepository; public List<User> findNearbyFriends(String userId, Distance distance) { User user = userRepository.findById(userId) .orElseThrow(() -> new RuntimeException("User not found")); Point userLocation = new Point(user.getLocation().getX(), user.getLocation().getY()); return userRepository.findByLocationNear(userLocation, distance); } } @Service public class PlaceService { @Autowired private PlaceRepository placeRepository; public List<Place> findNearbyPlaces(String userId, Distance distance) { User user = userRepository.findById(userId) .orElseThrow(() -> new RuntimeException("User not found")); Point userLocation = new Point(user.getLocation().getX(), user.getLocation().getY()); return placeRepository.findByLocationNear(userLocation, distance); } }
Hiç yorum yok:
Yorum Gönder