Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a LatLng generic abstract class #2873

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions CodenameOne/src/com/codename1/location/LatLng.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.codename1.location;

public interface LatLng {

public void setLatitude(double latitude);

public void setLongitude(double longitude);

public double getLatitude();

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()));
// }
}
66 changes: 58 additions & 8 deletions CodenameOne/src/com/codename1/location/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* <p>The sample below demonstrates the usage of the background geofencing API:</p>
* <script src="https://gist.github.com/codenameone/3de90e0ff4886ec145e8.js"></script>
*/
public class Location {
public class Location implements LatLng {

private int status;

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -255,10 +255,10 @@ public String toString() {
* location.
* @return
*/
public Comparator<Location> createDistanceCompartor() {
return new Comparator<Location>() {
public Comparator<LatLng> createDistanceCompartor() {
return new Comparator<LatLng>() {

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;
Expand All @@ -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 l != null && l.getLatitude() == latitude && l.getLongitude() == 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;
}




}
4 changes: 3 additions & 1 deletion CodenameOne/src/com/codename1/maps/BoundingBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import java.util.Vector;

import com.codename1.location.LatLng;

/**
* This class declares a bounding box of coordinates on the map.
*
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion CodenameOne/src/com/codename1/maps/Coord.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
*/
package com.codename1.maps;

import com.codename1.location.LatLng;
import com.codename1.ui.geom.Dimension;

/**
* This class declares a coordinate point on a map.
*
* @author Roman Kamyk <roman.kamyk@itiner.pl>
*/
public class Coord {
public class Coord implements LatLng {

private double longitude;
private double latitude;
Expand Down