oneRain 2019-09-06 16:43:11 +08:00
parent f57fc6d884
commit b0cf1caa5e
2 changed files with 26 additions and 52 deletions

View File

@ -1,10 +1,8 @@
namespace LeanCloud namespace LeanCloud {
{
/// <summary> /// <summary>
/// Represents a distance between two AVGeoPoints. /// Represents a distance between two AVGeoPoints.
/// </summary> /// </summary>
public struct AVGeoDistance public struct AVGeoDistance {
{
private const double EarthMeanRadiusKilometers = 6371.0; private const double EarthMeanRadiusKilometers = 6371.0;
private const double EarthMeanRadiusMiles = 3958.8; private const double EarthMeanRadiusMiles = 3958.8;
@ -13,8 +11,7 @@
/// </summary> /// </summary>
/// <param name="radians">The distance in radians.</param> /// <param name="radians">The distance in radians.</param>
public AVGeoDistance(double radians) public AVGeoDistance(double radians)
: this() : this() {
{
Radians = radians; Radians = radians;
} }
@ -26,10 +23,8 @@
/// <summary> /// <summary>
/// Gets the distance in miles. /// Gets the distance in miles.
/// </summary> /// </summary>
public double Miles public double Miles {
{ get {
get
{
return Radians * EarthMeanRadiusMiles; return Radians * EarthMeanRadiusMiles;
} }
} }
@ -37,10 +32,8 @@
/// <summary> /// <summary>
/// Gets the distance in kilometers. /// Gets the distance in kilometers.
/// </summary> /// </summary>
public double Kilometers public double Kilometers {
{ get {
get
{
return Radians * EarthMeanRadiusKilometers; return Radians * EarthMeanRadiusKilometers;
} }
} }
@ -50,8 +43,7 @@
/// </summary> /// </summary>
/// <param name="miles">The number of miles.</param> /// <param name="miles">The number of miles.</param>
/// <returns>A AVGeoDistance for the given number of miles.</returns> /// <returns>A AVGeoDistance for the given number of miles.</returns>
public static AVGeoDistance FromMiles(double miles) public static AVGeoDistance FromMiles(double miles) {
{
return new AVGeoDistance(miles / EarthMeanRadiusMiles); return new AVGeoDistance(miles / EarthMeanRadiusMiles);
} }
@ -60,8 +52,7 @@
/// </summary> /// </summary>
/// <param name="kilometers">The number of kilometers.</param> /// <param name="kilometers">The number of kilometers.</param>
/// <returns>A AVGeoDistance for the given number of kilometers.</returns> /// <returns>A AVGeoDistance for the given number of kilometers.</returns>
public static AVGeoDistance FromKilometers(double kilometers) public static AVGeoDistance FromKilometers(double kilometers) {
{
return new AVGeoDistance(kilometers / EarthMeanRadiusKilometers); return new AVGeoDistance(kilometers / EarthMeanRadiusKilometers);
} }
@ -70,8 +61,7 @@
/// </summary> /// </summary>
/// <param name="radians">The number of radians.</param> /// <param name="radians">The number of radians.</param>
/// <returns>A AVGeoDistance for the given number of radians.</returns> /// <returns>A AVGeoDistance for the given number of radians.</returns>
public static AVGeoDistance FromRadians(double radians) public static AVGeoDistance FromRadians(double radians) {
{
return new AVGeoDistance(radians); return new AVGeoDistance(radians);
} }
} }

View File

@ -2,8 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using LeanCloud.Storage.Internal; using LeanCloud.Storage.Internal;
namespace LeanCloud namespace LeanCloud {
{
/// <summary> /// <summary>
/// AVGeoPoint represents a latitude / longitude point that may be associated /// AVGeoPoint represents a latitude / longitude point that may be associated
/// with a key in a AVObject or used as a reference point for geo queries. /// with a key in a AVObject or used as a reference point for geo queries.
@ -11,16 +10,14 @@ namespace LeanCloud
/// ///
/// Only one key in a class may contain a GeoPoint. /// Only one key in a class may contain a GeoPoint.
/// </summary> /// </summary>
public struct AVGeoPoint : IJsonConvertible public struct AVGeoPoint : IJsonConvertible {
{
/// <summary> /// <summary>
/// Constructs a AVGeoPoint with the specified latitude and longitude. /// Constructs a AVGeoPoint with the specified latitude and longitude.
/// </summary> /// </summary>
/// <param name="latitude">The point's latitude.</param> /// <param name="latitude">The point's latitude.</param>
/// <param name="longitude">The point's longitude.</param> /// <param name="longitude">The point's longitude.</param>
public AVGeoPoint(double latitude, double longitude) public AVGeoPoint(double latitude, double longitude)
: this() : this() {
{
Latitude = latitude; Latitude = latitude;
Longitude = longitude; Longitude = longitude;
} }
@ -30,18 +27,13 @@ namespace LeanCloud
/// Gets or sets the latitude of the GeoPoint. Valid range is [-90, 90]. /// Gets or sets the latitude of the GeoPoint. Valid range is [-90, 90].
/// Extremes should not be used. /// Extremes should not be used.
/// </summary> /// </summary>
public double Latitude public double Latitude {
{ get {
get
{
return latitude; return latitude;
} }
set set {
{ if (value > 90 || value < -90) {
if (value > 90 || value < -90) throw new ArgumentOutOfRangeException(nameof(value), "Latitude must be within the range [-90, 90]");
{
throw new ArgumentOutOfRangeException("value",
"Latitude must be within the range [-90, 90]");
} }
latitude = value; latitude = value;
} }
@ -52,18 +44,13 @@ namespace LeanCloud
/// Gets or sets the longitude. Valid range is [-180, 180]. /// Gets or sets the longitude. Valid range is [-180, 180].
/// Extremes should not be used. /// Extremes should not be used.
/// </summary> /// </summary>
public double Longitude public double Longitude {
{ get {
get
{
return longitude; return longitude;
} }
set set {
{ if (value > 180 || value < -180) {
if (value > 180 || value < -180) throw new ArgumentOutOfRangeException(nameof(value), "Longitude must be within the range [-180, 180]");
{
throw new ArgumentOutOfRangeException("value",
"Longitude must be within the range [-180, 180]");
} }
longitude = value; longitude = value;
} }
@ -75,8 +62,7 @@ namespace LeanCloud
/// </summary> /// </summary>
/// <param name="point">GeoPoint describing the other point being measured against.</param> /// <param name="point">GeoPoint describing the other point being measured against.</param>
/// <returns>The distance in between the two points.</returns> /// <returns>The distance in between the two points.</returns>
public AVGeoDistance DistanceTo(AVGeoPoint point) public AVGeoDistance DistanceTo(AVGeoPoint point) {
{
double d2r = Math.PI / 180; // radian conversion factor double d2r = Math.PI / 180; // radian conversion factor
double lat1rad = Latitude * d2r; double lat1rad = Latitude * d2r;
double long1rad = longitude * d2r; double long1rad = longitude * d2r;
@ -94,10 +80,8 @@ namespace LeanCloud
return new AVGeoDistance(2 * Math.Asin(Math.Sqrt(a))); return new AVGeoDistance(2 * Math.Asin(Math.Sqrt(a)));
} }
IDictionary<string, object> IJsonConvertible.ToJSON() IDictionary<string, object> IJsonConvertible.ToJSON() {
{ return new Dictionary<string, object> {
return new Dictionary<string, object>
{
{ "__type", "GeoPoint" }, { "__type", "GeoPoint" },
{ "latitude", Latitude }, { "latitude", Latitude },
{ "longitude", Longitude } { "longitude", Longitude }