I have an Android app that when launched, inflates a MapView, gets the user's location and then makes a request to my server to fetch all points of interest (POI) as JSON within some radius of their location and draws those points on the map. I want the user to be able to pan around the map and see more points of interest load as they go outside of that initial data load, just like Google maps.
My initial thought is to handle the pan event and when panning stops, get the map center, and make another server request for POI within some radius of that location. This seems to me like this will quickly get to a point where it's sending back redundant data and making unnecessary server requests.
I'm looking for a caching strategy where I can make requests to fetch new data, but not have to make additional requests for the same data. My POI don't change very often either, so caching would be ideal to speed up subsequent launches of my app. Are there any best practices out there for such a thing? Or is it preferred to make a larger data request up front and just fetch new data as necessary?
What spontaneously comes to my mind is sectioning the POIs in square tiles for example. These tiles have a lastUpdate timestamp and both client and server communicate in terms of tiles, instead of geo location center point and radius. Your client would always send the lastUpdate timestamp of the cached tiles to the server and the server would only respond with updated data when that timestamp did change for the requested tiles. Another advantage is that your algorithm for retrieving POIs would be way faster compared to "point in circle" calculations.
Your client app can decide when to re-request tiles based on the timestamp. That depends on how often your server data changes. If it changes just once per day, then let your app re-request cached tiles only once per day. My suggestion is based on a fixed size tile (like 2 square miles for example). Otherwise you'll have a hard time with keeping track of the lastUpdate time stamps.
You could however create something like several different "levels of detail" for your zoom levels or your app needs to calculate the upper left visible tile and the lower right visible tile depending on the current zoom.
Related
In my app I have a Service which receives location updates and stores them to a database. I also have a Fragment which displays a MapView and a PolyLine of all recorded waypoints from the database.
During recording, the Service notifies the Fragment about new waypoints so the Fragment can update the PolyLine. The problem is that when the user navigates away from the app the app the Service keeps recording waypoints to the database, but now the Fragment doesn't get updated since the Fragment is paused. So in onResume I create a new PolyLine, read all the waypoints in the database and add them to the database.
This is all working fine, but it doesn't really feel like it's optimal from a performance perspective to create a new PolyLine and re-add all the waypoints (there could be thousands!). I guess I could just re-add any new waypoints that are not already in the PolyLine, but I wanted to see if anyone here has an alternate solution? Is there any way to keep the Fragment "alive" and updating its PolyLine even when the app is in the background (as long as the service is running)? Or is there a better way to do this?
Recreating the polyline is probably inevitable, but here are some things you should think about doing if performance becomes an issue:
Put a time limit. When recreating the polyline, only fetch the data in the last hour or day (test and you'll be able to determine the best value here), and offer an option to extend that period. This will make it more understood by the user that the data needs some time to load, and that the app will use more resources.
Aggregate the data while saving them. This should reduce the disk space used to save the points in the database (very important for low end devices) and improve on the performance when rebuilding the activity.. These are some tips for what you can do to reduce the number:
Check 2 points behind and see if they are a straight line. If so, delete the middle one. That should remove a lot of data recorded when the user is in a car or walking a long distance
Check if the last set of points (5 or more) are in the same area, that way you can get rid of a lot of data. So if a user is just waling in his home or workplace, you can just save one point for that without loosing too much data, which shouldn't really be a problem in most (99%) of the applications.
I did some search but could not find a suitable answer.
My App should compare with multiple locations for proximity. This means I will not be able to save all the locations into my app to confirm the proximity using locationManager. I want the proximity confirmation to be done in the server
What would be the best way to implement this?
Would it be sensible if the app asks for proximity confirmation every time the devices moves around?
I would try a different approach, since location updates from GPS are made once per second,
and I don't think it's a good idea to ask the server each second for proximity if you have a large amount of devices.
Think of this idea -
Get the device's initial location and send it to the server.
Decide on a sensible radius that the device will stay within it for the next 5-10 minutes.
Also make sure that you don't have "too many" points in that radius, or you can narrow the radius in that case.
It's up to you to decide the radius and the number of points, depending on your usage,
number of points etc.
Send form the server all the locations within that radius to the device.
Let the device calculate the proximity by itself.
When the device moves out of the initial radius - update the server and get the new
relevant locations.
This can be done easily - call the radius r. Save the initial location of the device, and calculate the distance
between the current and initail location. When it is "close enough" to r - update the server.
In your case, simply, you can send the received locations to your server and then make required calculations on server. But don't forget that you will be dealing with those questions
How many devices send location to server ?
How frequently each device send location to server ?
Also the responsibility of detecting a device has entered an area on the server
I think you can reduce the complexity of the all things by using geofencing api, link
No need to send each location to server.
Each device individually detects itself has entered or exited an
area.
EDIT
Otherwise you will be doing entered/exited calculations on server for unlimited count of device, whenever each device's location has changed.
Before we were doing similar thing in my previous company, calculating enter/exit time and enter durations. but via real gps devices on buses
We have almost 100 points(geofence) in city. So you can think that those points are on a few routes
Each gps device on bus is sending location to server periodically.
When the bus has finished it's route, server reviews device's all received locations on the route.
Compares each geofence with each location of bus.
This is the real scenario. You can call it "server based geofencing".
You could do a simple k-d tree implementation on the server side to store the coordinates.
Send the coordinates of the device over, which can be determined at whatever interval you need. If it's every 5 seconds, or 10 seconds it doesn't really matter. This will mainly be decided by the minimum distance between each of the coordinates/radius. If they're closer, you may need to update it more frequently.
Using the k-d tree finding the nearest neighbor would be O(log(n)). However, you make a slight modification where you can start adding the nodes to a list as long as they are within the certain radius of your device coordinates. In fact if you store it locally as a k-d tree as well then you can pick the furthest nodes in O(log(n))
Now on the second update when the device location is sent again, you can quickly update it since you have the existing locations. Say you move in the x direction by 5. You can drop the points that are now outside of the radius at x - 5. The new proximity, you do the same nearest neighbor search, adding in nodes as they're within the radius, but this time starting with the cached nodes closest to the direction you are moving in.
Combining that with an interval tree for radiuses. So say 0 to 1, 1 to 2, 2 to 3, as your intervals. You can pick out everything within a certain radius in O(log(n)) time as well. Those should be pointers to nodes in the k-d tree. That would simplify the radius calculations and finding the location if you're willing to sacrifice some memory for efficiency.
For a "fast" way to implement it on the server side you can use the mondodb $near geospatial query.
https://docs.mongodb.org/manual/reference/operator/query/near/
While on the mobile side you can use the minDistance property for the location updates. You can set it to a reasonable distance 20m/50m depending on the average distance between your locations.
http://developer.android.com/reference/android/location/LocationManager.html#requestLocationUpdates(java.lang.String,%20long,%20float,%20android.location.LocationListener)
There is a free service for this purpose -> Radar
You can register unlimited circle or polygon geofence, and register your user in app for tracking that user. When user entered in one geofence Radar send a notification to your server and send below data to you:
User ID, Geofence ID that user entered or exit, Confidence (low, medium, high) used for geofence that has overlap.
You can use this SDK in only 10 minutes.
I'm building a location based application.
Lets say that i have user A and i have a latitude and longitude values of his current location.
I got user B,C,D and thier locations as well..
For the example - user B + D are in a radius of 5km from user A and user C is in 12km radius from user A and I want to know how can i make a function that will tell me that whose near to me by 4/5/6/7km and etc..
If i user A wants users that are 8.5km away from him i will have as a result user B + D and thier distance from user A.
Now.. i know that i can use the Location class and use the distance function to calculate the distance between two users.
But the problem is that if i want to calculate that for the radius distance i need to fetch the entire users list from my database and send it to the client to start calculating distances between him and those i fetched from the server.
Now i dont want to do that off course if there is a better and more effecient way..
Firstly, I thought off using http request or some mathematical functions to calculate the distance between my users on the server side but the client (Android) offers very good tools to do so , so because of that I am lost of knowing to is the best thing to do.
Thanks head up :)
Hey don't you think that always sending locations of n-1 users to one requesting from server is inefficient and consumes unnecessary bandwidth than sending only few in the radius vicinity? The server can easily do this computation.
Think of a scenario where your app user base grows and grows? Then what?
Such a computation is always performed on the server.
Also nearly all of the times, server has much more computing power than the client. So even though android tools look lucrative, don't end up using those in a scenario like this.
In terms of tools, there are similar on the server side too ex. the haversine function. Also some databases like mongo also have inbuilt location filters. So this is really worth checking out.
I am in the process of planning an app that includes a feature that requires it to record and store the exact route a user takes while driving. Is there any location api that supports this out of the box? Or supports detecting a turn? I was initially hoping for an event triggered by the user turning onto a different road, but so far, no such luck.
I think you may try this way:
1 Request for GPS location update every 30s ,you can do this with Timer and TimerTask ,and LocationManager;
2 You can get the road name through Google Map API with the GPS location you get;
3 So you will get to know whether the user is turning onto a different road every 30 seconds.
I believe I have found a solution that makes use of the other answer here. I will post it as a separate answer though as it adds more to the other answer and is a full solution. As Ai Hao said, I could have my app request an updated location every 30 seconds to determine whether the user is on a new road, and then place a waypoint signalling the turn. However, this method is flawed if the mapping algorithm used (and I do not have the expertise to write my own) detects multiple possible routes. My solution to this issue is to store all location responses until a turn is made. This means that when a turn is detected, the oldest location data will be the last turn, while the newest will correspond to the turn being made. The app will request alternate routes, and if any are found, will calculate a route between the second oldest and second most recent coordinates, until no alternate routes are found. This should result in the exact route that the user took.
I have a list of coordinates in the database identified as POI. For a city could be >100 records.
I would like to get notified when the phone gets in 150 meters range of one of the location. The location coordinates too has an error/radius, usually 10 to 100meters. Since I don't find it good to add each location(could be hundreds) for a trigger, how can I optimize the wake-up code?
Also do I have options to remove a previously setup notification from the queue?
You could store your POIs in some sort of intelligent Hash-Table using the coordinates to compute a unique hash. Each time a location update arrives you make a lookup in your hash-table to see if there are POIs near the current location. This lookup should only take O(1), since it is a hash-lookup.
The desired range should be taken into account when computing the hashes and storing the POIs.
Just an idea!
Kind regards,
mefiX
There's an app named Locale, that can toggle various events based on your GPS location OR available Wifi network OR cell-station id, etc
It also has a plugins interface. It could be useful for you to examine that app and, maybe, write a plugin for it.
This problem reminds me of graphics in video games. There's no need to load the points that are well outside your range of movement. I'd break down the map into a grid, set triggers for the 8 adjecent grid blocks and then for each of the POI within the current grid block. When a new grid block is reached the triggers are updated. It'd probably be smart to overlap the grid blocks considering the range of error.