/** * Calculates the span of bounds and returns it in a String. A span is two values which are * latitude and longitude length between each parts of bounds. Example : -SW.lat = 1 -SW.lng = 10 * -NE.lat = 5 -NE.lng = 50 -> lat span = NE.lat - SW.lat = 5 - 1 = 4 -> lng span = NE.lng - * SW.lng = 50 - 10 = 40 * * @return span formated in a string */ public String toSpan() { return new StringBuffer("") .append(mNe.getLat() - mSw.getLat()) .append(",") .append(mNe.getLng() - mSw.getLng()) .toString(); }
@Test public void equals() { double lat = 90, lng = 180; LatLng point = LatLng.newInstance(lat, lng); Map<LatLng, String> map = new HashMap<LatLng, String>(); map.put(point, "point"); Assert.assertEquals("point", map.get(LatLng.newInstance(lat, lng))); }
/** * Creates new SW and NE to create union with another entire LatLngBounds. * * @param other Other bounds to make union */ public void union(LatLngBounds other) { mSw = new LatLng( Math.min(mSw.getLat(), other.getSw().getLat()), Math.min(mSw.getLng(), other.getSw().getLng())); mNe = new LatLng( Math.max(mNe.getLat(), other.getNe().getLat()), Math.max(mNe.getLng(), other.getNe().getLng())); calculateCenter(); }
private static boolean throwsException(double lat, double lng) { try { LatLng.newInstance(lat, lng); return false; } catch (Exception e) { return true; } }
@Override public boolean equals(Object o) { // Return true if the objects are identical. if (this == o) { // PLog.i("equals","Same object"); return true; } // Return false if the other object has the wrong type. if (!(o instanceof LatLngBounds)) { return false; } // Cast to the appropriate type LatLngBounds lhs = (LatLngBounds) o; // Check each field. Primitive fields, reference fields, and nullable reference // fields are all treated differently. return mSw.equals(lhs.mSw) && mNe.equals(lhs.mNe) && mCenter.equals(lhs.mCenter); }
/** * Extends bounds to the LatLng passed in parameter, if this one is not contained in bounds. * * @param point LatLng to include in bounds * @return LatLng */ public void extend(LatLng point) { if (!contains(point)) { mSw = new LatLng( Math.min(mSw.getLat(), point.getLat()), Math.min(mSw.getLng(), point.getLng())); mNe = new LatLng( Math.max(mNe.getLat(), point.getLat()), Math.max(mNe.getLng(), point.getLng())); calculateCenter(); } }
/** * Returns if yes or not the given LatLng is contained in bounds. * * @param latlng LatLng to test * @return true if LatLng is contained, false otherwise */ public boolean contains(LatLng latlng) { double lat = latlng.getLat(); double lng = latlng.getLng(); return (lat >= mSw.getLat() && lng >= mSw.getLng() && lat <= mNe.getLat() && lng <= mNe.getLng()); }
@Test public void compareTo() { LatLng p1 = LatLng.newInstance(90, -180); LatLng p2 = LatLng.newInstance(-90, 180); LatLng p3 = LatLng.newInstance(90, -180); LatLng p4 = LatLng.newInstance(-90, -180); Assert.assertEquals(1, p1.compareTo(p2)); Assert.assertEquals(-1, p2.compareTo(p1)); Assert.assertEquals(0, p1.compareTo(p3)); Assert.assertEquals(0, p3.compareTo(p1)); Assert.assertEquals(1, p1.compareTo(p2)); Assert.assertEquals(1, p2.compareTo(p4)); Assert.assertEquals(1, p1.compareTo(p4)); }
public FloatLatLng(LatLng ll) { this.lat = ll.getLat(); this.lng = ll.getLng(); }
@Override public LatLng calculateMidpoint(LatLng other) { return new FloatLatLng((lat + other.getLat()) / 2.0, (lng + other.getLng()) / 2.0); }
/** * Calculates LatLng SW and NE when center and latSpanE6 and lngSpanE6 are known. * * @param latSpan Latitude span between SW lat and NE lat * @param latSpan Latitude span between SW lng and NE lng */ private void calculateBounds(double latSpan, double lngSpan) { mSw = new LatLng(mCenter.getLat() - latSpan, mCenter.getLng() - lngSpan); mNe = new LatLng(mCenter.getLat() + latSpan, mCenter.getLng() + lngSpan); }
/** Calculates LatLng center when SW and NE are known. */ private void calculateCenter() { double latCenter = (mSw.getLat() + mNe.getLat()) / 2; double lngCenter = (mSw.getLng() + mNe.getLng()) / 2; ; mCenter = new LatLng(latCenter, lngCenter); }
public int getLngSpanE6() { return (int) (Math.abs(mNe.getLng() - mSw.getLng()) * 1000000); }
/** * Returns center of theses bounds. * * @return Center LatLng */ public LatLng getCenter() { return mCenter.clone(); }
/** * Returns North East of theses bounds. * * @return North East LatLng */ public LatLng getNe() { return mNe.clone(); }
/** * Returns South West of theses bounds. * * @return South West LatLng */ public LatLng getSw() { return mSw.clone(); }