ANDROID Show closest 10 locations on mapview - android

I am developing a POI locator app. I am parsing a JSON file with over 1600 locations. How would effiecntly compare this list to the current users location and get the closest 10?
EDIT
The app is using no server, as I want to use local JSON files.

I fixed it by using a for loop and measuring the distance between the location and the users location.
distance = locationA.distanceTo(locationB);
if (distance <= 1000 * 10) //finding all within 10km Radius.{
ADD GEOPOINT HERE
}

It's better if you can store the 1600 locations in a database(like mongodb) which supports geo spatial queries. you can directly query over the database to determine the closest N no.of locations to the given latitude and longitude of the user.

If you are going to find distance by a birds-view approach you can just query your locations and check distance with formula located here. But if you are going to take roads (graphs) into accounts, I'm afraid you'll need to use some kind of pathfinding service like google maps or roll your own. (As far as I know there is no built-in mechanism in Android.)

We were writing this kind of app as well, and performed some performance tests on MySQL and MongoDB. Because MongoDB is document based and stores its data in json format, location based queries(like closest restaurants to the user) completes and returns incredibly fast. As far as I can remember, it was approximately 10 times faster than MySQL. So, even if your application doesn' t currently use a no-sql database, I strongly suggest you to use MongoDB even just for location calculating part of your project.

Related

Get exact GEO location like lat and long in Android

I have created simple android application which take current Latitude and Longitude from google api. then it will store that data to server.
My problem is that every time when i am try to get the Location it show me different value. same place and same phone then also it fetches different data.
this problem is very critical because i am doing some functionality based on the difference of the Latitude and Longitude.
can you tell me how can i get the same lat and long in each devices ?
Thanks in advance
Does it display huge differences or just ~50-100m differences? Because GPS isn't exact (plus it depends on connections, ...).

How to query OSM from the server

Is there any way to connect to the web server of OpenStreetMap?
I want to receive the speed limit information given a geoLoc "lat and Lon". I thought of solving this issue by creating a huge database encompases
the node's lat and lon and the speed limit and then query the speed limit given a specific geoLoc.
but now i am wondering is it possible to connect directly the OSM server and query the speed limit given a specific geoLoc?! whic approach is more
efficient?
You can use Overpass API for querying (almost) any information. The language guide even has a very similar problem to yours: querying for name and rough location. Just replace the name tag with the maxspeed tag and you are almost done.
Overpass API has a really powerful language which can be quite a bit confusing at first. It might help you running your queries via the overpass turbo frontend which has a nice visualization of the results.

How does mobile application find users nearby

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.

How do I find nearby app users in android?

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.

Finding nearest Hospital using a stored map

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).

Categories

Resources