Check if the user location is near of some points

2019-09-03 00:50发布

问题:

I have a question about GPS alarm.

I have some points (latitude and longitude). I need a way to check if the user location is near to some of these points (approximately are 1800 points).

If I remember correctly there is a best practice to do this: to be notified when GPS position is in a certain region. This allows for optimizations regarding the battery.

Can you give me any suggestions?

回答1:

1800 points as per 1800 different regions to be monitored?

It is not possible. See here:

For this reason, Core Location limits to 20 the number of regions that may be simultaneously monitored by a single app.

If you have more than 20 regions to be monitored, this is what you can do:-

  1. When the user launches your app, get the user's location and send the location to your server
  2. After the server gets the user location, gathers all the points within N Kilometres (KM) radius.
  3. If the points within N KM are less than 20, then send all the points to your user to be monitored.
  4. If the points within N KM are more than 20, your server has to reduce the points based on the smaller radius (eg: N-1 KM)

I have developed a region monitoring app myself and this is what we do.



回答2:

Define what you mean by near to some of these points.

You could use the haversine formula to calculate the distance between your current location and each of the set of test points. The calculation involves a fair number of trig functions so could work out computationally intensive but depending upon your application it may not be necessary to recalculate more than once a minute or so.