From 9b7d14c554fc32fc1257e961012a3801aa385878 Mon Sep 17 00:00:00 2001 From: ramsestom Date: Tue, 30 Jul 2019 19:25:01 +0200 Subject: [PATCH 1/3] Add LatLng abstract class to CN1 core to avoid having to convert back and forth from different objects representing a LatLng position (like Location, Coord, geojson Position...) --- .../src/com/codename1/location/Location.java | 66 ++++++++++++++++--- .../src/com/codename1/maps/BoundingBox.java | 4 +- CodenameOne/src/com/codename1/maps/Coord.java | 3 +- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/CodenameOne/src/com/codename1/location/Location.java b/CodenameOne/src/com/codename1/location/Location.java index e4067497c5..b58c342003 100644 --- a/CodenameOne/src/com/codename1/location/Location.java +++ b/CodenameOne/src/com/codename1/location/Location.java @@ -44,7 +44,7 @@ *

The sample below demonstrates the usage of the background geofencing API:

* */ -public class Location { +public class Location extends LatLng { private int status; @@ -222,7 +222,7 @@ public void setVelocity(float velocity) { * @param l2 The location to measure distance to. * @return The number of metres between the current location and {@literal l2}. */ - public double getDistanceTo(Location l2) { + public double getDistanceTo(LatLng l2) { return haversine(getLatitude(), getLongitude(), l2.getLatitude(), l2.getLongitude()) * 1000; } @@ -255,10 +255,10 @@ public String toString() { * location. * @return */ - public Comparator createDistanceCompartor() { - return new Comparator() { + public Comparator createDistanceCompartor() { + return new Comparator() { - public int compare(Location o1, Location o2) { + public int compare(LatLng o1, LatLng o2) { double d1 = Location.this.getDistanceTo(o1); double d2 = Location.this.getDistanceTo(o2); return d1 < d2 ? -1 : d2 < d1 ? 1 : 0; @@ -274,9 +274,59 @@ public int compare(Location o1, Location o2) { * @param l * @return True if l has same latitude and longitude as this location. */ - boolean equalsLatLng(Location l) { + boolean equalsLatLng(LatLng l) { + return super.equals(l); //return l != null && l.latitude == latitude && l.longitude == longitude; + } - return l != null && l.latitude == latitude && l.longitude == longitude; + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + Float.floatToIntBits(accuracy); + long temp; + temp = Double.doubleToLongBits(altitude); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + Float.floatToIntBits(direction); + temp = Double.doubleToLongBits(latitude); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(longitude); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + status; + result = prime * result + (int) (timeStamp ^ (timeStamp >>> 32)); + result = prime * result + Float.floatToIntBits(velocity); + return result; + } - } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Location other = (Location) obj; + if (Float.floatToIntBits(accuracy) != Float.floatToIntBits(other.accuracy)) + return false; + if (Double.doubleToLongBits(altitude) != Double.doubleToLongBits(other.altitude)) + return false; + if (Float.floatToIntBits(direction) != Float.floatToIntBits(other.direction)) + return false; + if (Double.doubleToLongBits(latitude) != Double.doubleToLongBits(other.latitude)) + return false; + if (Double.doubleToLongBits(longitude) != Double.doubleToLongBits(other.longitude)) + return false; + if (status != other.status) + return false; + if (timeStamp != other.timeStamp) + return false; + if (Float.floatToIntBits(velocity) != Float.floatToIntBits(other.velocity)) + return false; + return true; + } + + + + } diff --git a/CodenameOne/src/com/codename1/maps/BoundingBox.java b/CodenameOne/src/com/codename1/maps/BoundingBox.java index c5239cd411..22597e4ddc 100644 --- a/CodenameOne/src/com/codename1/maps/BoundingBox.java +++ b/CodenameOne/src/com/codename1/maps/BoundingBox.java @@ -21,6 +21,8 @@ import java.util.Vector; +import com.codename1.location.LatLng; + /** * This class declares a bounding box of coordinates on the map. * @@ -95,7 +97,7 @@ public double longitudeDifference() { * @param cur coordinate to check * @return true if the given coordinate is contained in the bounding box */ - public boolean contains(Coord cur) { + public boolean contains(LatLng cur) { double latitude = cur.getLatitude(); if (latitude > getNorthEast().getLatitude() || latitude < getSouthWest().getLatitude()) { return false; diff --git a/CodenameOne/src/com/codename1/maps/Coord.java b/CodenameOne/src/com/codename1/maps/Coord.java index c7f0524b6e..1690c4c3b7 100644 --- a/CodenameOne/src/com/codename1/maps/Coord.java +++ b/CodenameOne/src/com/codename1/maps/Coord.java @@ -19,6 +19,7 @@ */ package com.codename1.maps; +import com.codename1.location.LatLng; import com.codename1.ui.geom.Dimension; /** @@ -26,7 +27,7 @@ * * @author Roman Kamyk */ -public class Coord { +public class Coord extends LatLng { private double longitude; private double latitude; From c485db30c29abf6ca29fdc397ab900cb9ef04b20 Mon Sep 17 00:00:00 2001 From: ramsestom Date: Tue, 30 Jul 2019 23:08:15 +0200 Subject: [PATCH 2/3] add LatLng that was missed from previous commit --- .../src/com/codename1/location/LatLng.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 CodenameOne/src/com/codename1/location/LatLng.java diff --git a/CodenameOne/src/com/codename1/location/LatLng.java b/CodenameOne/src/com/codename1/location/LatLng.java new file mode 100644 index 0000000000..58fdad2149 --- /dev/null +++ b/CodenameOne/src/com/codename1/location/LatLng.java @@ -0,0 +1,37 @@ +package com.codename1.location; + +public abstract class LatLng { + + public abstract void setLatitude(double latitude); + + public abstract void setLongitude(double longitude); + + public abstract double getLatitude(); + + public abstract double getLongitude(); + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(this.getLatitude()); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(this.getLongitude()); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object object) { + if (this == object) + return true; + if (object == null) + return false; + if (!(object instanceof LatLng)) + return false; + LatLng aPosition = (LatLng) object; + return (aPosition.getLatitude()==this.getLatitude() && aPosition.getLongitude()==this.getLongitude()); + //return (Double.doubleToLongBits(aPosition.getLatitude())==Double.doubleToLongBits(this.getLatitude()) && Double.doubleToLongBits(aPosition.getLongitude())==Double.doubleToLongBits(this.getLongitude())); + } +} From 7a47be43d03ca4aa972d5596bde3bd707426bb30 Mon Sep 17 00:00:00 2001 From: ramsestom Date: Thu, 1 Aug 2019 21:16:13 +0200 Subject: [PATCH 3/3] changed LatLng from abstract class to interface --- .../src/com/codename1/location/LatLng.java | 58 +++++++++---------- .../src/com/codename1/location/Location.java | 4 +- CodenameOne/src/com/codename1/maps/Coord.java | 2 +- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/CodenameOne/src/com/codename1/location/LatLng.java b/CodenameOne/src/com/codename1/location/LatLng.java index 58fdad2149..c67c193199 100644 --- a/CodenameOne/src/com/codename1/location/LatLng.java +++ b/CodenameOne/src/com/codename1/location/LatLng.java @@ -1,37 +1,37 @@ package com.codename1.location; -public abstract class LatLng { +public interface LatLng { - public abstract void setLatitude(double latitude); + public void setLatitude(double latitude); - public abstract void setLongitude(double longitude); + public void setLongitude(double longitude); - public abstract double getLatitude(); + public double getLatitude(); - public abstract double getLongitude(); + public double getLongitude(); - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - long temp; - temp = Double.doubleToLongBits(this.getLatitude()); - result = prime * result + (int) (temp ^ (temp >>> 32)); - temp = Double.doubleToLongBits(this.getLongitude()); - result = prime * result + (int) (temp ^ (temp >>> 32)); - return result; - } - - @Override - public boolean equals(Object object) { - if (this == object) - return true; - if (object == null) - return false; - if (!(object instanceof LatLng)) - return false; - LatLng aPosition = (LatLng) object; - return (aPosition.getLatitude()==this.getLatitude() && aPosition.getLongitude()==this.getLongitude()); - //return (Double.doubleToLongBits(aPosition.getLatitude())==Double.doubleToLongBits(this.getLatitude()) && Double.doubleToLongBits(aPosition.getLongitude())==Double.doubleToLongBits(this.getLongitude())); - } +// @Override +// public int hashCode() { +// final int prime = 31; +// int result = 1; +// long temp; +// temp = Double.doubleToLongBits(this.getLatitude()); +// result = prime * result + (int) (temp ^ (temp >>> 32)); +// temp = Double.doubleToLongBits(this.getLongitude()); +// result = prime * result + (int) (temp ^ (temp >>> 32)); +// return result; +// } +// +// @Override +// public boolean equals(Object object) { +// if (this == object) +// return true; +// if (object == null) +// return false; +// if (!(object instanceof LatLng)) +// return false; +// LatLng aPosition = (LatLng) object; +// return (aPosition.getLatitude()==this.getLatitude() && aPosition.getLongitude()==this.getLongitude()); +// //return (Double.doubleToLongBits(aPosition.getLatitude())==Double.doubleToLongBits(this.getLatitude()) && Double.doubleToLongBits(aPosition.getLongitude())==Double.doubleToLongBits(this.getLongitude())); +// } } diff --git a/CodenameOne/src/com/codename1/location/Location.java b/CodenameOne/src/com/codename1/location/Location.java index b58c342003..66bac0c45a 100644 --- a/CodenameOne/src/com/codename1/location/Location.java +++ b/CodenameOne/src/com/codename1/location/Location.java @@ -44,7 +44,7 @@ *

The sample below demonstrates the usage of the background geofencing API:

* */ -public class Location extends LatLng { +public class Location implements LatLng { private int status; @@ -275,7 +275,7 @@ public int compare(LatLng o1, LatLng o2) { * @return True if l has same latitude and longitude as this location. */ boolean equalsLatLng(LatLng l) { - return super.equals(l); //return l != null && l.latitude == latitude && l.longitude == longitude; + return l != null && l.getLatitude() == latitude && l.getLongitude() == longitude; } diff --git a/CodenameOne/src/com/codename1/maps/Coord.java b/CodenameOne/src/com/codename1/maps/Coord.java index 1690c4c3b7..173db6cc77 100644 --- a/CodenameOne/src/com/codename1/maps/Coord.java +++ b/CodenameOne/src/com/codename1/maps/Coord.java @@ -27,7 +27,7 @@ * * @author Roman Kamyk */ -public class Coord extends LatLng { +public class Coord implements LatLng { private double longitude; private double latitude;