How to get location data within a radius around a point

Let’s see how to get location data within a radius around a point.

  • The point is defined by latitude and longitude.
  • We can retrieve location data in terms of pairs of coordinates within a specific radius around the defined point.
  • Geolocation ArcQL query is used to retrieve the location data.
  • The location data is defined by pairs of latitude and longitude.
  • Frame the query like this
geo(latitude, longitude, radius, latitudeFieldName, longitudeFieldName)

•latitude: defined in radians by latitude * Pi / 180
•longitude: defined in radians by longitude * Pi / 180
•radius: defined in kilometers, for example, 0.5 stands for 500 meters
•latitudeFieldName: The name of GraphQL type latitude field
•longitudeFieldName: The name of GraphQL type longitude field

We will define GPSCoordinates data type with latitude and longitude fields.

type GPSCoordinates {
 latitude: Float
 longitude: Float
}

Insert the latitude and longitude data in the GPSCoordinates objects. Please note the data is in radians and not in degrees.

mutation {
  upsert(
    values: {
      GPSCoordinates: [
        { latitude: 0.55835374214, longitude: 0.6084901767 }
        { latitude: 0.55835374214, longitude: 0.6084901767 }
        { latitude: -0.41167494186, longitude: -0.81812175344 }
        { latitude: -0.41786913283, longitude: -0.80969561972 }
        { latitude: -0.41776046165, longitude: -0.81033767224 }
      ]
    }
  ) {
    id
  }
}

#result
{
  "data": {
    "upsert": [
      {
        "id": "01FK3W4Q89M2HRH65H9WNVREGZ"
      },
      {
        "id": "01FK3W4Q8E02A84ABZYM0NMQXB"
      },
      {
        "id": "01FK3W4Q8FAFGEDK7MKXANBG6E"
      },
      {
        "id": "01FK3W4Q8F8K8AG7X7QAKTP2JE"
      },
      {
        "id": "01FK3W4Q8FSDNKBQ26MZGSM0D7"
      }
    ]
  }
}

Sample Query

Let’s search the pairs of coordinates from GPSCoordinates objects that come within 100 Km of a location specified by a point (-0.41176913283,-0.81863767224)

{
  find(type: GPSCoordinates, arcql: "geo(-0.41176913283,-0.81863767224,100,'latitude','longitude')") {
    edges {
      node {
        ... on GPSCoordinates {
          latitude
          longitude
        }
      }
      cursor
    }
  }
}
#result
{
  "data": {
    "find": {
      "edges": [
        {
          "node": {
            "latitude": -0.41786913283,
            "longitude": -0.80969561972
          },
          "cursor": "01FK3W4Q8F8K8AG7X7QAKTP2JE"
        },
        {
          "node": {
            "latitude": -0.41167494186,
            "longitude": -0.81812175344
          },
          "cursor": "01FK3W4Q8FAFGEDK7MKXANBG6E"
        },
        {
          "node": {
            "latitude": -0.41776046165,
            "longitude": -0.81033767224
          },
          "cursor": "01FK3W4Q8FSDNKBQ26MZGSM0D7"
        }
      ]
    }

  }
}

Check the POSTMAN collection for the geolocation query in different programming languages! Click </>and choose the programming language of your choice.
Don’t forget to insert your own Authorization key and Hypi Domain under Headers to test the results!
Run in Postman