I am making an application which needs to be able to find people nearby, who are users of my app.
I looked at many answers of precedent similar questions, and it seems I have no choice but to keep uploading a user's current location to the server, and get nearby users' list when necessary.
Then my question is,
1. To get the nearby list, there should be some algorithm or function which calculates the distance. Then doesn't that mean I have to get all distances between my location and the rest of the app users? So the server returns certain number of users who have the least distance results. If I'm correct, wouldn't there be memory or time issues?
2. This might sound weird, but how about this.
I'll probably send latitude and longitude information or address information to the server. Can't I compare those strings with all users' address list from the first numbers or letters using string searching algorithms or something?
For example, if my last updated address is 'abcde' on the server, the algorithm will look for addresses that start with 'a', if finished searching, then look for addresses that has 'b' after 'a', in other words 'ab'.
This might not be a right solution, but I thought it might work because the address will be saved in same forms.
To find nearby users efficiently, you need a spatial index. See: Hierarchical Triangular Mesh.
You also can use one of the databases that support spatial queries.
I'll probably send latitude and longitude information or address information to the server. Can't I compare those strings with all users' address list from the first numbers or letters using string searching algorithms or something?
That won't work with latitude and longitude because that way you can only search for proximity in one dimension. For example, 30°N 30°E will appear closer to 30°N 90°E than to 31°N 30°E.
It may work with addresses, but only if they are reliably connected with coordinates (i.e. not typed in by users), and only if you don't mind that users 200 meters apart but on different sides of some administrative border will not count as close to each other.
You can use the REST Api from Server Side using PHP which takes the Current LAT LONG of all user for your app From User's Android Phone at certain time Interval Lets Say 5 min & it returns the Nearest Location,Address Distance.You need to call the Api # certain time Interval & in response From Server You will get all the Details What You Want Like Nearest Location of Your App user, Distance and Other Thing.
Why i am suggesting this way because to do Computation from Android Side Will Affect the App Performance Having Battery Issues So its better to Have All computation from Server side instead of Android(User) Side
hope this will helps you.
I am a bit late here but for anyone who comes across this question, assuming you are using Firebase for your database, your best option would be GeoFire using queries around a specific point in coordinates and a radius in km. Here is a link to the github repo https://github.com/firebase/geofire-android which explains everything in detail.
Related
I am new to mobile development and I undertook some freelance work. Now I am required to display a page with top N nearest places (businesses registered with us).
I am wondering what the best way of going about this is, from my experience it would seem that I would want to do this calculation in my back-end server.
I have a NodeJS server, however, it seems the server will be concurrently doing a lot of other work just verifying JWT tokens and what not, I have seen that android provides a method to easily do these calculations (Location). I have also read that there are some google API's one could use.
The idea I have is that I can pull my places with their lat, long from my database then the user sends lat,long and my server calculates top N results and sends to user.
What would you recommend and why?
Thanks!
The short answer is: Server.
But why?
The job of the server to make it so no one can use the app who is not authorized to do so. Thats one of many jobs anyway. However, you should send your location to the server, the server makes the API call, and returns the N closest locations.
You take that response, parse it, and fill in a view as you would like. With the server doing the heavy lifting, you can then also keeps track of something better, like how many calls, which calls, location of calls, etc etc.
i am developing an app in android that must recieve the distance between two points from the user after traveling between them, and that input must be validated, for that i'm thinking of using an API to get the information and make the validations later. I know that it's not the op to ask the user to enter that kind of input, but that's not on me.
For example, if the distance between point A and point B is 13 km according Google Maps Distance Matrix API, i got from the waze api like 18 km. The approach of the app is one about transport so i would prefer to use waze information because it's more accuarate in terms of road traffic.
So, there's any way to take the distance between two points from waze?
more exactly, this one:
Any help would be appreciated
PD: sorry for bad english
I'm not certain whether you're still looking for an answer, but in theory you could call the Waze routing server in the same way as the Waze Live Map calls it.
The following URL is the request sent to the Waze routing server when I calculate the route between Antwerp and Brussels in Belgium.
https://www.waze.com/row-RoutingManager/routingRequest?from=x:4.4024643+y:51.2194475&to=x:4.3517103+y:50.8503396&at=0&returnJSON=true&returnGeometries=false&returnInstructions=false&timeout=60000&nPaths=1&clientVersion=4.0.0&options=AVOID_TRAILS:t,ALLOW_UTURNS:t
Note: make sure to properly encode this URL when using a script. Those colons and commas might cause issues otherwise.
To calculate the length of a route, you'll need to go through all the traversed road segments and make a sum of the length fields (expressed in metres, if I'm not mistaken).
Do note that the results will differ from time to time as Waze includes historical traffic data in its results and will decide to make detours, which may not be wanted for your use case.
I would also strongly suggest to look into the legal status of doing this, though I suspect it won't be a big issue if you're not using this data to make a competing service or 'forget' to mention you're using Waze data to calculate things.
I am working on an android application which can find users nearby.
I need help in design that application.
I am thinking:
1. When I start my application, I use API to find my location.
2. I send that location to my server.
3. server returns a list of other users nearby.
My question is how can the server keeps track of a list of users who are nearby?
Do I create a background service on phone who will report users location periodically?
And the server maintains a list of all users' location?
Is that approach feasible?
You're on the right track. If you are able to geocode or retrieve your current location lat/lon coordinates, there might be a library that can help you out.
My first thought was the Geocoder (https://github.com/alexreisner/geocoder) library for ruby, it offers a slick API that allows you to easily find other nearby objects:
if obj.geocoded?
obj.nearbys(30) # other objects within 30 miles
obj.distance_from([40.714,-100.234]) # distance from arbitrary point to object
obj.bearing_to("Paris, France") # direction from object to arbitrary point
end
There are other options that are easy enough too, if you're using PostgreSQL with PostGIS, you have some utility functions that I'm sure you could use to query 'nearby' users. (Found an example, "What is the best way to find all objects within a radius of another object?" http://postgis.net/docs/manual-1.3/ch03.html#id434832)
So the overall workflow would be:
Request lat/lon from device API
Send lat/lon to server/web service
Use library/database to find other users within a specified radius
Send list of users back down to device
I suspect the most difficult aspect would probably be keeping the lat/lon of other users up to date, but that's a different problem to solve.
Edit:
Just to clarify, you'll definitely want to store all the logged in users' lat/lon inside of some sort of database on the server. It's a very feasible approach, but you'll have to keep it up to date and be able to determine somehow if the data is stale (if that's important to your application). Whether you use a background service to keep that information up to date is kind of up to you and the constraints of the problem you're trying to solve.
I am trying to develop an android program that finds the location of the nearest hospital WITHOUT using the internet. Instead, I want it to use a stored map on the android device. Is this feasible? If so, Can anyone refer me to the code?
I successfully developed a similar program that uses google API, meaning it will contact the server and use the internet. I reused the source code in this link.
But in some cases, my user might not have an access to the internet and it is urgent to find the nearest hospital. How can I solve this issue?
Another much simpler solution is to store the coordinates of each hospital in a file.
At App start your read the file in.
Possible file format:
Hospital Name, latitude, longitude, Adress Optional
Then simple calculate the line of sight distance to all hospitals using CLLocation distanceTo(). Then sort by distance.
Take the shortest.
Although this is not road distance, it will work as long as there is no river or rail road inbetween
It's definitely possible.
You'll need to use offline map data - you can get map data from the OpenStreetMap project. I'd recommend letting the user choose which areas to download after initial app installation, and then the data will always be available.
Later on, usage of the location API does not require internet connection. You'll get co-ordinates, with which you can calculate the distance to other co-ordinates (of hospitals..).
Obviously, your challenge will be to create a hard-coded list of hospital locations (albeit update-able, make sure the app can download a new list when internet is present, for example download a XML file).
I have an application that generates some number of tokens around the current location of the user using a certain distance radius. The user will then have to run to some of those locations. The problem is that some tokens can be created in a lake,forest,ocean, or some other physically unreachable location. As a quick fix I just generate extra tokens and increase the proximity distance that determines if a user reached a certain location. I now want to improve this so that each token is located at a reachable location.
The only solution I have been able to come up with is using the Google Directions API to determine a path from the user to the token and use the last coordinate in the polyline as the new reachable location of the token. My problem with this is that I potentially have to post up to 30 requests to the Directions service simultaneously and I am worried that I might hit the query rate limit. I have not found anything definite about query rate limit.
So my question is whether anyone knows of a better solution or can give any input on the Directions query rate limit? Waiting 1 second between each request and forcing the user to wait up to 30 seconds is not a reasonable solution. Thanks.
UPDATE
Using the solution that I described in the question does produces an OVER_QUERY_LIMIT, even if I wait 1 second between each request. Other then that the logic was sound and tokens that got a request thru were appearing in walk reachable locations.
You can calculate the distance between two lat/lon with Location.distanceBetween(). This is a static convenience API call. There's no limit on number of calulations.
Usage limits
Use of the Google Directions API is subject to a query limit of 2,500 directions requests per day. Individual directions requests may contain up to 8 intermediate waypoints in the request.
Google Maps Premier customers may query up to 100,000 directions requests per day, with up to 23 waypoints allowed in each request.
You might want to take a look into the Maps premier customer so you don't hit the limit too fast. If your app becomes popular I bet you can get an higher limit.
One way would be to see if you can see the elevation to see if it's a cliff or not. However just seeing if it's a lake or not seems to be quite hard. Might be some kind of gps lookup service out there except Google Maps.
If you just want to know the distance between locations just use Location.distanceTo() or static distanceBetween()
Getting the info if it's a road or not is another question.
After some 8 hours I finally got something working. So using the fact that each request can have up to 8 waypoints I can technically ask directions to 9 locations in one request. Here's what I am doing now:
Generate 9 random locations at a time. Pass the locations to my DirectionsComputer which returns the polyline path that goes through all 9 coordinates as a list of coordinates. Then I pick 9 location from the path and set them as the locations of my tokens. Now all my tokens are semi-randomly generated, always appear on a road, and I only need to do 4 consecutive requests to generate 36 tokens.
There are some cases where a few tokens are bunched together. For example, the coordinates which are originally located in the ocean get moved to the same beach. But for the most part, all my tests showed the tokens spread apart and I could tweak the rest.