/** * Creates a copy of {@code point}; * * @param point The point to copy. */ public ParseGeoPoint(ParseGeoPoint point) { this(point.getLatitude(), point.getLongitude()); }
@Override public boolean equals(Object obj) { if (obj == null || !(obj instanceof ParseGeoPoint)) { return false; } if (obj == this) { return true; } return ((ParseGeoPoint) obj).getLatitude() == latitude && ((ParseGeoPoint) obj).getLongitude() == longitude; }
/** * Get converts coordinate to JSONArray. */ protected JSONArray coordinatesToJSONArray() throws JSONException { JSONArray points = new JSONArray(); for (ParseGeoPoint coordinate : coordinates) { JSONArray point = new JSONArray(); point.put(coordinate.getLatitude()); point.put(coordinate.getLongitude()); points.put(point); } return points; }
/** * Get distance in radians between this point and another {@code ParseGeoPoint}. This is the * smallest angular distance between the two points. * * @param point {@code ParseGeoPoint} describing the other point being measured against. */ public double distanceInRadiansTo(ParseGeoPoint point) { double d2r = Math.PI / 180.0; // radian conversion factor double lat1rad = latitude * d2r; double long1rad = longitude * d2r; double lat2rad = point.getLatitude() * d2r; double long2rad = point.getLongitude() * d2r; double deltaLat = lat1rad - lat2rad; double deltaLong = long1rad - long2rad; double sinDeltaLatDiv2 = Math.sin(deltaLat / 2.); double sinDeltaLongDiv2 = Math.sin(deltaLong / 2.); // Square of half the straight line chord distance between both points. // [0.0, 1.0] double a = sinDeltaLatDiv2 * sinDeltaLatDiv2 + Math.cos(lat1rad) * Math.cos(lat2rad) * sinDeltaLongDiv2 * sinDeltaLongDiv2; a = Math.min(1.0, a); return 2. * Math.asin(Math.sqrt(a)); }
double minX = coordinates.get(0).getLatitude(); double maxX = coordinates.get(0).getLatitude(); double minY = coordinates.get(0).getLongitude(); double maxY = coordinates.get(0).getLongitude(); minX = Math.min(geoPoint.getLatitude(), minX); maxX = Math.max(geoPoint.getLatitude(), maxX); minY = Math.min(geoPoint.getLongitude(), minY); maxY = Math.max(geoPoint.getLongitude(), maxY); boolean outside = point.getLatitude() < minX || point.getLatitude() > maxX || point.getLongitude() < minY || point.getLongitude() > maxY; if (outside) { return false; for (int i = 0, j = coordinates.size() - 1; i < coordinates.size(); j = i++) { double startX = coordinates.get(i).getLatitude(); double startY = coordinates.get(i).getLongitude(); double endX = coordinates.get(j).getLatitude(); double endY = coordinates.get(j).getLongitude(); boolean intersect = ((startY > point.getLongitude()) != (endY > point.getLongitude()) && point.getLatitude() < (endX - startX) * (point.getLongitude() - startY) / (endY - startY) + startX);
ParseGeoPoint target = (ParseGeoPoint) value; if (northeast.getLongitude() < southwest.getLongitude()) { throw new ParseException(ParseException.INVALID_QUERY, "whereWithinGeoBox queries cannot cross the International Date Line."); "The southwest corner of a geo box must be south of the northeast corner."); if (northeast.getLongitude() - southwest.getLongitude() > 180) { throw new ParseException(ParseException.INVALID_QUERY, "Geo box queries larger than 180 degrees in longitude are not supported. " && target.getLongitude() >= southwest.getLongitude() && target.getLongitude() <= northeast.getLongitude());
@Override public boolean equals(Object obj) { if (obj == null || !(obj instanceof ParsePolygon)) { return false; } if (obj == this) { return true; } ParsePolygon other = (ParsePolygon) obj; if (coordinates.size() != other.getCoordinates().size()) { return false; } boolean isEqual = true; for (int i = 0; i < coordinates.size(); i += 1) { if (coordinates.get(i).getLatitude() != other.getCoordinates().get(i).getLatitude() || coordinates.get(i).getLongitude() != other.getCoordinates().get(i).getLongitude()) { isEqual = false; break; } } return isEqual; }
json.put("__type", "GeoPoint"); json.put("latitude", point.getLatitude()); json.put("longitude", point.getLongitude()); return json;
ParseGeoPoint rhs = (ParseGeoPoint) value; return lhs.getLatitude() == rhs.getLatitude() && lhs.getLongitude() == rhs.getLongitude();
@Test public void testGeoPoint() throws JSONException { JSONObject json = new JSONObject(); json.put("__type", "GeoPoint"); json.put("longitude", -20); json.put("latitude", 30); ParseGeoPoint parseGeoPoint = (ParseGeoPoint) ParseDecoder.get().decode(json); assertNotNull(parseGeoPoint); final double DELTA = 0.00001; assertEquals(-20, parseGeoPoint.getLongitude(), DELTA); assertEquals(30, parseGeoPoint.getLatitude(), DELTA); }
@Test public void testGetParseGeoPointKeyExist() { final ParseGeoPoint geoPoint = new ParseGeoPoint(44.484, 26.029); ParseGeoPoint geoPointAgain = new ParseGeoPoint(45.484, 27.029); final Map<String, Object> params = new HashMap<>(); params.put("key", geoPoint); ParseConfig config = new ParseConfig(params); ParseGeoPoint geoPointConfig = config.getParseGeoPoint("key"); assertEquals(geoPoint.getLongitude(), geoPointConfig.getLongitude(), 0.0001); assertEquals(geoPoint.getLatitude(), geoPointConfig.getLatitude(), 0.0001); assertSame(geoPointConfig, config.getParseGeoPoint("key", geoPointAgain)); }
@Test public void testConstructors() { ParseGeoPoint point = new ParseGeoPoint(); assertEquals(0, point.getLatitude(), 0); assertEquals(0, point.getLongitude(), 0); double lat = 1.0; double lng = 2.0; point = new ParseGeoPoint(lat, lng); assertEquals(lat, point.getLatitude(), 0); assertEquals(lng, point.getLongitude(), 0); ParseGeoPoint copy = new ParseGeoPoint(point); assertEquals(lat, copy.getLatitude(), 0); assertEquals(lng, copy.getLongitude(), 0); }
@Test public void testParcelable() { ParseGeoPoint point = new ParseGeoPoint(30d, 50d); Parcel parcel = Parcel.obtain(); point.writeToParcel(parcel, 0); parcel.setDataPosition(0); point = ParseGeoPoint.CREATOR.createFromParcel(parcel); assertEquals(point.getLatitude(), 30d, 0); assertEquals(point.getLongitude(), 50d, 0); } }
ParseGeoPoint geoPointAgain = (ParseGeoPoint) paramsAgain.get("geoPoint"); assertEquals(geoPoint.getLatitude(), geoPointAgain.getLatitude(), 0.0000001); assertEquals(geoPoint.getLongitude(), geoPointAgain.getLongitude(), 0.0000001); List<Object> listAgain = (List<Object>) paramsAgain.get("array"); assertArrayEquals(list.toArray(), listAgain.toArray());
ParseGeoPoint geoPointAgain = (ParseGeoPoint) decodedDiskConfigParameters.get("geoPoint"); assertEquals(geoPoint.getLatitude(), geoPointAgain.getLatitude(), 0.0000001); assertEquals(geoPoint.getLongitude(), geoPointAgain.getLongitude(), 0.0000001); List<Object> listAgain = (List<Object>) decodedDiskConfigParameters.get("array"); assertArrayEquals(list.toArray(), listAgain.toArray());
ParseGeoPoint geoPointAgain = (ParseGeoPoint) paramsAgain.get("geoPoint"); assertEquals(geoPoint.getLatitude(), geoPointAgain.getLatitude(), 0.0000001); assertEquals(geoPoint.getLongitude(), geoPointAgain.getLongitude(), 0.0000001); List<Object> listAgain = (List<Object>) paramsAgain.get("array"); assertArrayEquals(list.toArray(), listAgain.toArray());
@Override public void done(List<ParseObject> objects, ParseException e) { if ( e == null){ if(objects.size() > 0){ listViewContent.clear(); usernames.clear(); latitudes.clear(); longitudes.clear(); for (ParseObject object : objects){ Double distanceInMiles = userLocation.distanceInMilesTo((ParseGeoPoint) object.get("requesterLocation")); Double distaneOneDP = (double) Math.round(distanceInMiles *10) / 10; listViewContent.add(String.valueOf(distaneOneDP)+ " miles"); usernames.add(object.getString("requesterUsername")); latitudes.add(object.getParseGeoPoint("requesterLocation").getLatitude()); longitudes.add(object.getParseGeoPoint("requesterLocation").getLongitude()); } arrayAdapter.notifyDataSetChanged(); } } } });