Unity3d determines whether to arrive at the destination

Posted by InfiniteA on Tue, 14 Apr 2020 20:16:18 +0200

Because I don't know much about JAVA, I can't understand Golder's java api basically. At the beginning, I wanted to directly use Golder's web api to set up a Geographical fence , and then make a judgment, but the geofence list is always empty when the last request is made.

Bad return value.png

Geofence has been created, that is, there is no value when it is returned in the final judgment, and there is no problem.

If Gaud's geofencing will be used, the following sections can be ignored.

Since you can't get it directly, it's local.

The method of calculating the direct distance between two longitudes and latitudes:

private const double EARTH_RADIUS = 6378137;
/// <summary>
///Calculate the distance between two points and return the distance between two points in meters
///The formula is provided by GOOGLE with an error of less than 0.2m
/// </summary>
///< param name = "lng1" > first longitude < / param >
///< param name = "LAT1" > latitude of the first point < / param >        
///< param name = "lng2" > second longitude < / param >
///< param name = "lat2" > latitude of the second point < / param >
/// <returns></returns>
public static double GetDistance(double lng1, double lat1, double lng2, double lat2)
    double radLat1 = Rad(lat1);
    double radLng1 = Rad(lng1);
    double radLat2 = Rad(lat2);
    double radLng2 = Rad(lng2);
    double a = radLat1 - radLat2;
    double b = radLng1 - radLng2;
    double result = 2 * Math.Asin(Math.Sqrt(Math.Pow(Math.Sin(a / 2), 2) + Math.Cos(radLat1) * Math.Cos(radLat2) * Math.Pow(Math.Sin(b / 2), 2))) * EARTH_RADIUS;
    return result;

/// <summary>
///Convert latitude and longitude into radians
/// </summary>
/// <param name="d"></param>
/// <returns></returns>
private static double Rad(double d)
    return (double)d * Math.PI / 180d;

adopt The method of Unity access to Gaud map The current position of the device can be obtained in real time and displayed in latitude and longitude. Then the current location information can be obtained by the coordinate transformation of the reverse geocoding of the Gaud map web api.

stay Gaode open platform Query the destination coordinates, or get the longitude and latitude coordinates of the destination directly by geocoding.

Then calculate the distance between the two longitude and latitude and calculate the distance between the current position and the target position.

//Calculate the distance between the current longitude and latitude and the target longitude and latitude
double distance = PublicFunction.GetDistance(Current longitude, Current latitude, Longitude of target point, Target latitude);

//judge the distance
if (distance <= THE_TARGET_RADIUS)
    Debug.Log("Enter the target range!");
    Debug.Log("Distance target range:" + distance + "rice");

Topics: Java Google less Unity