Check if a latitude and longitude is within a circle

See this illustration:

在这里输入图像描述

What I would like to know is:

  • How to create an area (circle) when given a latitude and longitude and the distance (10 kilometers)
  • How to check (calculate) if a latitude and longitude is either inside or outside the area
  • I would prefer if you can give me code example in Java or specifically for Android with Google Maps API V2


    What you basically need, is the distance between to 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 that API: https://developer.android.com/reference/android/location/Location.html


    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.


    请参阅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);
    
    链接地址: http://www.djcxy.com/p/84906.html

    上一篇: 降低复杂性。 找到最接近的一对纬度和经度

    下一篇: 检查经度和纬度是否在一个圆圈内