I wanted to make an android app which provides the distance between two areas in a city.
The user will have two input fields where he must enter the two area names he would like to find the distance.
After he enters the area name. My app should display the distance.
How should I implement this?
First of all you need to find latitude and longitude of that area name.
Convert both address in Location object, refer this.
Calculate distance by using distanceTo()
location1.distanceTo(location2);
Returns the approximate distance in meters between this location and
the given location.
refer this for distanceTo() detail.
First get latitude and longitude of your start point and end point and using
distanceBetween method of Location class calculate the distance here is link
http://developer.android.com/reference/android/location/Location.html#distanceBetween(double,%20double,%20double,%20double,%20float[])
Use the Google Maps Directions API. You'll need to request the directions over HTTP. You can do this directly from Android, or via your own server.
For example, directions from Montreal to Toronto:
GET http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false
You'll end up with some JSON. In routes[].legs[].distance, you'll get an object like this:
"legs" : [
{
"distance" : {
"text" : "542 km",
"value" : 542389
},
You can also get the polyline information directly from the response object.
for more detail visit here. Get the distance between two locations in android?
Related
I have a table with the following fields:
Name Longitude Latitude
The user can search for near places depending on the radius he chooses, what is the algorithm used to do this?
Example: I retrieve the user Longitude/Latitude, and I ask him about the radius in KM he wants to apply to his search, then these 3 parameters should be sent to the method that will retrieve the location.
I found the answer.
In other words what we need is the distance between two points that we have their coordinates, so using math :
Distance between two points
all solutions about calculating distance are between two points.
my question is:
Is there any way to calculate the distance to a certain street?
suppose I want to calculate a distance from my location to a specific area or avenue, which have many streets to get there, in this case how to know the nearest street to me to get there?
here I have only one point (my location) while the destination point is variant and depends on my current location. what I know about destination is in a certain street or area, so how to calculate it?
another case for this issue, suppose I am away 10 meters from a certain street which is 1 km long.
I should be able to calculate that my distance is 10 meters from the beginning of that street, after this distance when enter the street, and for each meter I waked for 1 km it should be 0 meter distance from my location to this street, and when I exit it from the other side the distance should be calculating again from the end point of that street not from the beginning point, so when I get away from it about 5 meters it should calculate it as 5 meters not as 1005 meters.
Here is the requirement by example on google maps:
first let's see Wall Street in NY city on map.
So the target destination is Wall Street.
Now if your current location in the cross of John Street with Water Street, my task is to lead you to the nearest point in Wall Street and the path will be as this one (walk mode).
however if you are in the cross of Broad Street with Beaver Street then the destination to nearest point in Wall Street will be like this one (walk mode):
this is my issue
is it possible? Do new APIs offer a solution for this?
any solutions or ideas?
hey yes you get latitude and longitude of searched location here i'll example how you can get.
You can use google autocomplete for searching any place.Then get placeid from autocomplete api then pass it to details api.
https://maps.googleapis.com/maps/api/place/autocomplete/json
https://maps.googleapis.com/maps/api/place/details/json
See pass the parameters
//Google Autocomplete
#GET(ApiConstants.GET_AUTOCOMPLETE)
Observable<GoogleAutocompleteSearchResponse> getAutoCompleteSearch(#Query("input") String input, #Query("key") String key, #Query("components") String components);
// Google Autocomplete places
#GET(ApiConstants.GET_AUTOCOMPLETE_PLACE)
Observable<GoogleAutoCompletePlacesResponse> getAutoCompletePlaces(#Query("placeid") String input, #Query("key") String key);
You can get latitude longitude in second one api.
Hope this will help you.
First get the longitude, latitude of destination location
public Location getCoordinatesFromAddress(Context context,String strAddress) {
Geocoder coder = new Geocoder(context);
List<Address> address;
Location destinationLoc = null;
try {
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
return null;
}
Address location = address.get(0);
destinationLoc = new Location("Destination");
destinationLoc.setLatitude(location.getLatitude());
destinationLoc.setLongitude(location.getLongitude());
} catch (Exception ex) {
ex.printStackTrace();
}
return destinationLoc;
}
As you mentioned you have latitude and longitude of your present location. Use that to create your current location.
Location locationA = new Location("Present location ");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Next after getting latitudes and longitudes of your destination and source(present) locations you can easily calculate the distance between them
e.g
float distance = locationA.distanceTo(locationB);
It will give you the distance b/w two geo locations in meters.
Take a look at this response. Also take a look over this API from google.
I guess what you want looks like this:
- get targeted street coordinates [lat, long]
- get nearby places from google,
- search the nearest place.
In this case, if you can get address of both destination and origin, then by following method you can get distance between address.
For which you need to pass either address (origin/destination) or latitude / longitude (origin/destination)
1) Here the origin parameter can be like following , if address is provided
origins=Bobcaygeon+ON|24+Sussex+Drive+Ottawa+ON
2) Where as , if latitude / longitude is provided , then following will be the format.
origins=41.43206,-81.38992
3) If you are able to get place ID , then this will be the format
origins=place_id:ChIJ3S-JXmauEmsRUcIaWtf4MzE
The following example uses latitude/longitude coordinates to specify the destination coordinates
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.6905615%2C-73.9976592%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626%7C40.659569%2C-73.933783%7C40.729029%2C-73.851524%7C40.6860072%2C-73.6334271%7C40.598566%2C-73.7527626&key=YOUR_API_KEY
Same request for encoded polyline will be
https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=40.6655101,-73.89188969999998&destinations=enc:_kjwFjtsbMt%60EgnKcqLcaOzkGari%40naPxhVg%7CJjjb%40cqLcaOzkGari%40naPxhV:&key=YOUR_API_KEY
As explained by other's the general method to find shortest distance using API is
float distance = firstLocation.distanceTo(secondLocation);
For more information go through this link
Just use Haversine formula to calculate distance between two points if you know the latitude and longitude for each of them.
Node Package: Haversine Node
Gradle: Gradle Haversine
How to find distance from polyline in google map in android using map apiV2 ? I want to find distance when user start moving from one location to another .
I used the Calculate distance in meters when you know longitude and latitude in java but it takes distance between two lat-long not as per user
moves in map. Please help me if anyone knows how to find distance using polyline.
For finding distance from your polyline you can use PolyUtil class which contains one method distanceToLine .
implementation'com.google.maps.android:android-maps-utils:0.5+'
add this library in your build.gradle.
For more you can use Google map utility class
https://developers.google.com/maps/documentation/android-api/utility/
For this you need to use thread. In that at regular interval you should find the distance between current coordinates and last saved coordinate.
Raw code:
Coordinate current, last;
Distance D = 0;
while (true) {
wait(10000);
last = current;
current = getNewCordinate;
d = D + distFrom(current, last);
}
This logic will help.
Based on the answer given by Gevaria Purva I found a more appropriate method on PolyUtil named isLocationOnPath.
For the third parameter most likely you want to pass true.
Hello I want to search nearest location using google place api....
from https://developers.google.com/places/documentation/supported_types i have used types in my code for searching nearest places....
and the code is below
try {
double radius = 5000; // 10000 meters
// get nearest places
nearPlaces = googlePlaces
.search(gps.getLatitude(), gps.getLongitude(), radius,
"sublocality|sublocality_level_4|sublocality_level_5|sublocality_level_3|sublocality_level_2|sublocality_level_1|neighborhood|locality|sublocality");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
but could not find nearest places
like if i am in "San Diego, CA, USA" then i want to get result like
Gaslamp/Downtown,
Pacific Beach,
Ocean Beach,
Uptown,
Hillcrest,
Mission Valley,
Fashion Valley,
North Park,
but instead of this i am getting only two value like
Mission Valley East,
Serra Mesa
so can any one help me what type i have to pass so i can get result like above instead of below result
My previous answer was deleted by a moderator for unknown reasons, I am re-posting with some more context as it is the currently the correct answer:
It appears you are trying return nearby geographical locations (anything from the second list in the supported place types). Unfortunately the Places API is not designed for this, it is designed to return a list of nearby establishments (anything from the first list in the supported place types) and up to two locality or political type results to to help identify the area you are performing a Place Search request for.
This is stated in the documentation here:
https://developers.google.com/places/documentation/search#PlaceSearchResponses
"results" contains an array of Places, with information about each. See Place Search Results for information about these results. The Places API returns up to 20 establishment results per query. Additionally, political results may be returned which serve to identify the area of the request.
If you type filter your Place Search request by a geographic location type like locality or political, you will filter out the establishment results and only be left with the area identity results.
A Places API - Feature Request for returning nearby localities has been filed here:
http://code.google.com/p/gmaps-api-issues/issues/detail?id=4434
If you believe this would be a useful feature please make sure your star the request to let us know you are interested in seeing it added.
double radius = 5000 is 5 Kilometers just over 3 miles.
If you increase the radius you should return more places
You should consider the distance to maximum 50Km and there are different types as follows:
establishment,lodging,spa,bar,church,place_of_worship etc.
I am developing an application that shows the distance between the user and multiple locations (such as foursquare in the image below) on android, I am using eclipse and would like to know how to calculate the distance between various points. Thank you!
Image:
http://i.stack.imgur.com/rsWmO.jpg
There are probably many ways to get this done, here's an option.
You could use the distanceTo() method. If you want more than one distance, simply use a loop to repeat it until you've calculated the distances between all the Locations you have at hand.
If you're using Google Maps, you can use a Distance Matrix
https://developers.google.com/maps/documentation/distancematrix/
https://developers.google.com/maps/documentation/javascript/distancematrix
Here I am providing you some sample code for Distance calculation. I have done like this in my project. Here distanceTo() method will return you the distance in double.
private Location currentLocation, distanceLocation;
double distance = 0;
//set your current location
currentLocation.setLatitude(currentLat);
currentLocation.setLongitude(currentLong);
//set your destination location
distanceLocation = new Location("");
distanceLocation.setLatitude(destinatioLat);
distanceLocation.setLongitude(destinationLong);
distance = currentLocation.distanceTo(distanceLocation)/1000;
Same for more then one location you can use Array for storing distance. Its on you how you want to use it as per your requirement.
Hope this will help you.