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.
Related
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
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.
Does anyone know where the separate latitude and longitude distances can be found between two google markers? I can get the total distance using the haversine method such as this:
Find distance between two points on map using Google Map API V2
How can I get each value please?
Thanks
Frank
The Marker class has a Position property, so
double latitudeDifferenceInDegrees = marker1.getPosition().latitude - marker2.getPosition().latitude;
double longitudeDifferenceInDegrees = marker1.getPosition().longitude - marker2.getPosition().longitude;
You can convert the latitude difference into a distance in meters - each degree is equal to 110.574 kilometers:
double latitudeDifferenceInMeters = latitudeDifferenceInDegrees * 110574;
You can't do the same for longitude. On the equator, a longitude degree is about 111 km, but near the poles, it is much less. If you are only using this for short distances, I'd go for
double longitudeDifferenceInMeters = longitudeDifferenceInDegrees * 111320 * Math.cos((marker1.getPosition().latitude + marker2.getPosition().latitude) / 2);
Note that both can give negative values if the first marker lies south or west of the second marker. If you're interested in positive values only, apply Math.abs().
I am developing an application that uses google map to navigate the user to a given location. I want to orient the map so the destination point always be at the top of the map and the current location marker to be at the bottom and both markers to be vertically aligned and centered in the map (e.g the vertical line between them to be perpendicular to the screen)
My approach is to find the mid point of the markers and than to calculate the bearing to rotate the map so both markers end up vertically aligned and centered. Centering the mid point will center the markers but I can't calculate the correct value for the bearing.
Any help will be appreciated.
EDIT:
I've tried Location.bearingTo and Location.distanceBetween. For the same input they return different values and the one returned from Location.distanceBetween is what i am looking for.
EDIT2 (Code example):
public static void positionMap(GoogleMap map, LatLng start, LatLng end) {
// zoom the map so both locations are visible
LatLngBounds bounds = LatLngBounds.builder().include(start).include(end).build();
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 200));
// find the bearing
float[] results = new float[3];
Location.distanceBetween(
start.latitude,
start.longitude,
end.latitude,
end.longitude,
results);
float bearing = results[2];
// position the map so the two markers are vertically aligned
CameraPosition position = map.getCameraPosition();
CameraPosition cameraPosition = new CameraPosition.Builder(position)
.target(Utils.median(start, end))
.bearing(bearing)
.build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
Take a look the the distanceBetween method of a location object.
Here is the doc. The initial bearing is the bearing you need to use from the starting point, the final bearing is the bearing you will be on when you reach the destination. I think you would be interested in the initial bearing.
public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Added in API level 1
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].
Parameters
startLatitude the starting latitude
startLongitude the starting longitude
endLatitude the ending latitude
endLongitude the ending longitude
results an array of floats to hold the results
Throws
IllegalArgumentException if results is null or has length < 1
You could try using Location.bearingTo().
Unfortunately this requires converting from LatLng to Location, but that should be simple enough.
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).