Skip to content

Commit

Permalink
Merge pull request #42 from SAKPaaS/feature/#32_resultlist_begrenzen
Browse files Browse the repository at this point in the history
Feature/#32 resultlist begrenzen
  • Loading branch information
r-franzke authored Mar 24, 2020
2 parents e983167 + de32c1f commit 126a1a0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/main/java/de/sakpaas/backend/model/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,4 @@ public class Location {

@Column(name = "COUNTRY")
private String country;


}
21 changes: 18 additions & 3 deletions src/main/java/de/sakpaas/backend/service/LocationService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package de.sakpaas.backend.service;

import de.sakpaas.backend.model.Location;
import javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class LocationService {
Expand All @@ -24,8 +24,23 @@ public Optional<Location> getById(long id) {
return locationRepository.findById(id);
}

public static double distanceInKm(double lat1, double lon1, double lat2, double lon2) {
//https://www.daniel-braun.com/technik/distanz-zwischen-zwei-gps-koordinaten-in-java-berchenen/
int radius = 6371;
double lat = Math.toRadians(lat2 - lat1);
double lon = Math.toRadians(lon2 - lon1);
double a = Math.sin(lat / 2) * Math.sin(lat / 2) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(lon / 2) * Math.sin(lon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = radius * c;
return Math.abs(d);
}

public List<Location> findByCoordinates(Double lat, Double lon) {
return locationRepository.findByLatitudeBetweenAndLongitudeBetween(lat - 0.1, lat + 0.1, lon - 0.1, lon + 0.1);
List<Location> list = locationRepository.findByLatitudeBetweenAndLongitudeBetween(lat - 0.1, lat + 0.1, lon - 0.1, lon + 0.1);
return list.stream()
.sorted(Comparator.comparingDouble(l -> distanceInKm(l.getLatitude(), l.getLongitude(), lat, lon)))
.limit(100)
.collect(Collectors.toList());
}

public Location save(Location location) {
Expand Down

0 comments on commit 126a1a0

Please sign in to comment.