1. Convert S2Cell Id to Latitude and Longitude
To get the Latitude and Longitude of given S2cell Id, below is the code,
public List < String > getLatLong(String s2Id) {
List < String > latlong = new ArrayList < > ();
if (s2Id == null) {
Assert.fail("Invalid S2Id provided");
}
S2CellId cell = new S2CellId(Long.parseLong(s2Id));
double latitude = cell.toLatLng().lat().degrees();
double longitude = cell.toLatLng().lng().degrees();
latlong.add(String.valueOf(latitude));
latlong.add(String.valueOf(longitude));
return latlong;
}
2. Convert Latitude and Longitude to S2Cell Id
To get the S2cell Id of given Latitude and Longitude below is the code,
public String getS2Id(String latitude, String longitude) {
if ((latitude == null || latitude.isEmpty()) || (longitude == null || longitude.isEmpty())) {
Assert.fail("Invalid latitude/longitude provided");
}
S2LatLng s2LatLng = S2LatLng.fromDegrees(Double.parseDouble(latitude), Double.parseDouble(longitude));
return String.valueOf(S2CellId.fromLatLng(s2LatLng).id());
}