0

I want to develop a location based app.

Requirement: user inserts data(lat-long) and it is stored in SQLite. When user reach at that location he/she will get an alert. I want to define some area(e.g. 1 km) near to this location. If user is in 1 km circle of this location then he/she get an alert.

I know how to use location listener but do not have idea to fix this area of circle.

I looked at this but no idea how to use it.

Anybody can suggest me ? Thanks

Community
  • 1
  • 1
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45

2 Answers2

0

What is so difficult?

To know whether the current point is inside any of the 1km radius?

A point is inside a circle if the distance to the center is smaller than the radius!

So just use the distanceTo method of CLLocation, and check to be < 1000. Do that for all points.

for (int i = 0; i < numpoints; i++) {
  dist = currentLocation.distanceTo(point[i].latitude, point[i].longitude);
  if (dist < 1000) {
     // found: signal point reached
  }
}

Unless you have hundred thsousands of points this approach works, Load all points to main memeroy into one big array.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
0
No need to calculate distance , you can compare the longitude and latitudes within radius of 1km

        double lat1 = Double.parseDouble(lat);
        double lon1 = Double.parseDouble(lon);

        double incrlng = 0.85;
        double incrlat = (radius in km)/ (Math.cos(lat1/57.3)*69.1);
         //here below lat1 is your latitude and lon1 is your longitude               
         north = lat1 + incrlat;
         west = lon1 - incrlng;
         south = lat1 - incrlat;
         east  = lon1 + incrlng; 

check your latitude and longitude with the above co-ordinates,like below
if(latitude >= south && latitude  <= north && longitude <= east && longitude >= west)// check the user latitude and longitude satisfies the if condition, if so he is within the 1 KM radius. 
ari
  • 11
  • 5