Check if a latitude and longitude is within a circle

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

See this illustration:

\"enter

What I would like to know is:

  1. How
6条回答
  •  Happy的楠姐
    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

提交回复
热议问题