38

See this illustration:

enter image description here

What I would like to know is:

  1. How to create an area (circle) when given a latitude and longitude and the distance (10 kilometers)
  2. 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

Peter Warbo
  • 11,136
  • 14
  • 98
  • 193
  • 1
    Well, as far as the second question goes, circle is just collection of dots on the same distance from one point, the center. If it is inside the circle, it means that it is closer (less distance) to the center, if it is outside then the distance is larger than the radius. Radius in your example is I suppose 10 kilometers. – dkasipovic Feb 27 '14 at 09:15

8 Answers8

46

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

Eszter
  • 331
  • 1
  • 3
  • 19
flx
  • 14,146
  • 11
  • 55
  • 70
  • Hi, I want to implement same functionality with java What I would like to know is: 1) How to create an area (circle) when given a latitude and longitude and the distance (in meters) 2)How to check (calculate) if a latitude and longitude is either inside or outside the area – Mangesh Mandavgane Feb 12 '16 at 14:55
  • @MangeshMandavgane you just have to copy the two functions called "computeDistanceAndBearing" and "distanceBetween" https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/location/java/android/location/Location.java – Flo354 Jul 06 '16 at 15:36
  • @Flo354, Thanks for reply I checked your suggested URL code. It is very helpful code, But I have done implementation 4 month ago, I refer this http://stackoverflow.com/questions/120283/how-can-i-measure-distance-and-create-a-bounding-box-based-on-two-latitudelongi – Mangesh Mandavgane Jul 07 '16 at 10:34
  • Hi all! which jar should I include in my java application in order to use "Location.distanceBetween()" method – srihari Jul 27 '16 at 09:00
  • 2
    I am having the question. Is it the distance including the pathway to a location or just distance between two points. – Ajinkya Jul 25 '17 at 09:07
2

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];
}
  • Hi sir i am new to this google maps. i have multiple circles. Is there any way to find my current location is pointed in which circle? – Aravindhan Gs May 17 '19 at 18:20
2

Kotlin version

Using instance method distanceTo, something like this in a form of an extension function:

fun Location.checkIsInBound(radius: Double,center:Location):Boolean
        =  this.distanceTo(center)<radius

Usage example:

fusedLocationClient.lastLocation
        .addOnSuccessListener { location : Location? ->
        val isWithin2km = location?.checkIsInBound(2000.0,center) }

Besides using Location API's distanceTo or distanceBetween as stated in the above answers, You could also use Google Map Utility Library's SphericalUtil's computeDistanceBetween as well, however it takes LatLng as input.

Kalai
  • 503
  • 1
  • 3
  • 12
1

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.

Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
1

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);
Android Newbie
  • 711
  • 3
  • 9
  • ??? by definition every point on the circle is 10Km from the center. the distance between area of interest and current position is the line between the center of the circle and the current position – Android Newbie Feb 27 '14 at 09:26
1

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

Ruan
  • 3,969
  • 10
  • 60
  • 87
1

Just in case if anyone is using GoogleMap and tried to apply location range by long&lat. You may try google.maps.Circle()

i.e. (mkr is your marker)

  let yourcircle = new google.maps.Circle({
    strokeColor: "#0079C3",
    strokeOpacity: .8,
    strokeWeight: 2,
    fillColor: "#0079C3",
    fillOpacity: 0.2,
    map: this.map,
    center:  {'lat':your latitude, 'lng':your longitude},
    radius: Math.sqrt(your distance range) * 1000,
  });
JaMondo
  • 278
  • 2
  • 8
0

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.

Community
  • 1
  • 1
user2808624
  • 2,502
  • 14
  • 28