I am developing one application in which I am having database of the places with me as a addresses in form of String. Using Geocoders I am getting the Lat/Long of the same. I also done with my current location. Now from that current location I wish to get near by 4-5 places from my database. Another thing about this is that first i wish to display the nearest 4-5 location's addresses in ListView and if you clicked on any of it then it will display the map with distance of your current location to the destination and path of it.
I got the idea of this application from already developed application famously known as "Places" in android market.
Please kindly help me out in this I am very close to my application but stuck with some small things.
Thank You Keyur
Use Location.distanceTo() to get a list of distances from the current location and all the locations in the database.
Sort the result and choose the top 5 with the least distance from the current position. Get the addresses and pass this list in an String array in extras to a new list activity
Because what i wish to do is that 5best nearest location which i get from distance.To()
Once you get these locations, You can send it in a String of the format
String topFive = "lat1,lon1 lat2,lon2 lat3,lon3 ..."
Intent intent = new Intent(getBaseContext(), YourListActivity.class);
intent.putExtra("TOP_FIVE_LOCATIONS", topFive );
startActivity(intent)
In the onCreate()
of YourListActivity.java
String topFive = getIntent().getStringExtra("TOP_FIVE_LOCATIONS");
Now split this string using String.split(" ")
and you will have an String [] of locations. Split again with "," and parse the lat, lon using Double.parseDouble()
You need to learn about ListView
s next, I'll direct you to a tutorial for this.
Once you have created your ListView
, to set the lat, lon data in an adapter, the ListView
will render the data in this adapter.
method that will be displayed in well formed in list and if you click on the list
Use the setOnItemClickListener()
to get item click events
You launch the next activity with the String coordinates, like before. This is a MapView.
then it will display the map of that selected item and distance from your current location to that selected plus the path.
See this to learn how to draw a line on the MapView.
Take baby steps since you are quite new to this.