Android find bearing on a fixed location - android

I am very new to Android and learning through it. So i want to create a arrow like pointer to show where is the fixed location is fixed, as the users move their device, the arrow should automatically updated(like compass).
I have googled for resources but i couldn't manage to find it. Anyone here knows how to do it? any examples or tutorials would be much appreciated.
Example:
fixed location: 20 latitude, 50 longitude
my location: 5 latitude 25, longitude
So how do i find the arrow pointed to fixed location?

If curLat and curLon are the current location's latitude and longitude and if refLat and refLon are the reference location latitude and longitude you may get the bearing from the current location to the reference location like this:
final float[] results= new float[3];
// The computed distance in meters is stored in results[0].
// If results has length 2 or greater, the initial bearing is stored in results[1].
// If results has length 3 or greater, the final bearing is stored in results[2].
Location.distanceBetween(curLat, curLon, refLat, refLong, results);
final float bearing = results[1];
If the distance grows result[2] differ more and more from results[1] unless the the course from the current location to the reference location follows a rhumb line (or loxodrome, see here: http://en.wikipedia.org/wiki/Rhumb_line).

Related

How to find the distance between two location on map i

How to find the distance between two locations on android ecillipse project &php .the project is based on online good transport system .The fare of the carrier needed to be found out so there is a need of finding distance between source & destination .Harversine formula was implemented for finding the shortest vehicle in the region
float[] results = new float[1];
Location.distanceBetween(source.latitude, source.longitude,
destination.latitude, destination.longitude,
results);
Computes the approximate distance in meters between two
locations, and optionally the initial and final bearings of the
shortest path between them. Distance and bearing are defined using the
WGS84 ellipsoid. The computed distance is stored in results[0]. If results has length
2 or greater, the initial bearing is stored in results[1]. If results has
length 3 or greater, the final bearing is stored in results[2].
#param startLatitude the starting latitude
#param startLongitude the starting longitude
#param endLatitude the ending latitude
#param endLongitude the ending longitude
#param results an array of floats to hold the results
#throws IllegalArgumentException if results is null or has length < 1
Try this code
double _distance = Location.distanceBetween(
_firstLatitude,
_firstLongitude,
_secondLatitude,
_secondLongitude,
_results);

is it possible to get distance between current location and a street, avenue or area?

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 check if locations are within my circle region?

I can use GPS to get my current location, I'd like to retrieve all the POIs which within my circle region.
The center point of circle is my current location, radius of circle region is x.
POI item is stored in database with its location and other information.
Which solution is the best way to retrieve all the POIs which within my circle.
Any suggestion?
Thanks,
Considering that you've retrieved all of your location points, retrieved your location using LocationManager and wish to check which of those lie inside of your circle area, this may be one way to achieve your goal:
int RADIUS;
ArrayList<Point> points;
Location myLocation;
for(Point point : points){
Location pointLocation = Location("");
pointLocation.setLatitude(point.x); // assume x values are latitudes
pointLocation.setLongitude(point.y); // assume y values are longitudes
if(myLocation.distanceTo(Location pointLocation) < RADIUS){
// This point lies inside
}
}
Another, less Android-ish way would be to check if your data points satisfy the equation:
(myLocation.getLatitude() - pointLocation.getLatitude()) ^ 2 + (myLocation.getLongitude()) - pointLocation.getLongitude()) ^ 2 <= RADIUS ^ 2
Since this equation presupposes that the Earth surface is flat, the result would contain error, magnitude of which would grow with the RADIUS value.

Checking current location with location's which are stored in Sqlite Database

I have to generate Notification Alert whenever my current location get match with the location stored in the Database.
So I used google places API for user type the location and then it get stored in the database so i have to write one service for getting current location from LocationListener and then how can I check location stored in database?
Can any one suggest me that how to check ?
with the help of location text with the current text or getting latitude and longitude ?
There are many approaches that could be used here but in each of those the comparison should be depending on latitude and longitude (at least that what I use in similar situations).
For sure you can't compare the exact value of latitude and longitude with another stored latitude and longitude; it is logically wrong and almost impossible to happen.
a better approach would be:
1-create a virtual circle around the stored location (lat,lng).
here the circle is just for demonstration purposes what you need in your code is:
Location.distanceBetween() method.
lets say you need to compare within 500 meters the code would look like this:
float[] results = new float[1];
Location.distanceBetween(storedLatitude, storedLongitude, currentLatitude, currentLongitude, results);
float distanceInMeters = results[0];
boolean within500m = distanceInMeters < 500;
2-get returned boolean
if(true) -> //result(your code when stored location = current)
if(false) ->// result(your code when stored location != current).

how to get points in distance from point

first what I want to achive, than I think I can do it
I want to find streets on the edge of area where center point is point where I am standing.
Now for example I'm selecting area with 2km radius. I want to find 360 points (1 point for each degree) and check if point with this coordinates is street or not. Now 3 questions
How to get each point in distance from me
How to get latitude and longitude of this point
How to check if this point is street or not
How to get each point in distance from me
Having the Lat and Lng of source and destination points, you can use Location.distanceBetween method.
float[] results = new float[3];
Location.distanceBetween(srcLat, srcLng, destLat, destLng, results);
The distance between the two points is in index 0 : results[0]
How to get latitude and longitude of this point
Here is a link
How to check if this point is street or not
Here is another link.
Hope this help.

Categories

Resources