Check if a latitude and longitude is within a circle

前端 未结 6 1698
一整个雨季
一整个雨季 2020-12-04 13:47

See this illustration:

\"enter

What I would like to know is:

  1. How
相关标签:
6条回答
  • 2020-12-04 14:08

    Have you gone through the new GeoFencing API. It should help you. Normal implementation takes a lot of time. This should help you implementing it easily.

    0 讨论(0)
  • 2020-12-04 14:09

    What you basically need, is the distance between two points on the map:

    float[] results = new float[1];
    Location.distanceBetween(centerLatitude, centerLongitude, testLatitude, testLongitude, results);
    float distanceInMeters = results[0];
    boolean isWithin10km = distanceInMeters < 10000;
    

    If you have already Location objects:

    Location center;
    Location test;
    float distanceInMeters = center.distanceTo(test);
    boolean isWithin10km = distanceInMeters < 10000;
    

    Here is the interesting part of the API used: https://developer.android.com/reference/android/location/Location.html

    0 讨论(0)
  • 2020-12-04 14:14

    see https://developer.android.com/reference/android/location/Location.html

    Location areaOfIinterest = new Location;
    Location currentPosition = new Location;
    
    areaOfIinterest.setLatitude(aoiLat);
    areaOfIinterest.setLongitude(aoiLong);
    
    currentPosition.setLatitude(myLat);
    currentPosition.setLongitude(myLong);
    
    float dist = areaOfIinterest.distanceTo(currentPosition);
    
    return (dist < 10000);
    
    0 讨论(0)
  • 2020-12-04 14:14

    Check this:

     private boolean isMarkerOutsideCircle(LatLng centerLatLng, LatLng draggedLatLng, double radius) {
        float[] distances = new float[1];
        Location.distanceBetween(centerLatLng.latitude,
                centerLatLng.longitude,
                draggedLatLng.latitude,
                draggedLatLng.longitude, distances);
        return radius < distances[0];
    }
    
    0 讨论(0)
  • 2020-12-04 14:18

    Location didn't work for me, here's what I did.

    import 'dart:math' show cos, sqrt, asin;
    
    
    double calculateDistanceBetweenTwoLatLongsInKm(
        double lat1, double lon1, double lat2, double lon2) {
      var p = 0.017453292519943295;
      var c = cos;
      var a = 0.5 -
          c((lat2 - lat1) * p) / 2 +
          c(lat1 * p) * c(lat2 * p) * (1 - c((lon2 - lon1) * p)) / 2;
      return 12742 * asin(sqrt(a));
    }
    

    Then just check if that distance is more (outside) or less (inside) than your radius in KM

    0 讨论(0)
  • 2020-12-04 14:25

    If you mean by "How to create an area", that you want to draw the area on the map, you will find an example right in the map V2 reference doc for the class Circle.

    For checking whether the distance between the center of the circle and your point is greater than 10 km I would suggest to use the static method Location.distanceBetween(...) as it avoids unnecessary object creations.

    See also here (at the very end of the answer) for a code example in case the area is a polygon rather than a circle.

    0 讨论(0)
提交回复
热议问题