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().
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
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.
I tried to add some GeoPoints on my map but I've problems with some “special” points.
The range of the latitude value is +90 to -90 and therefore I want to add a point like the this...
int latitude: 85000000;
int longitude = 45000000;
GeoPoint point = new GeoPoint(latitude, longitude);
...and after this I double check the latitude value with:
point.getLongitudeE6();
I always get a maximum value of 80000000 back. Can someone explain why google set this limit for a GeoPoint and what the reason for this limitation is?
Thanks!
Your issue is with getLatitudeE6(). GeoPoint doesn't work near the poles. You're probably going too far north or too far south.
This behavior is by design. From the docs:
[The latitude] will be clamped to between -80 degrees and +80 degrees
inclusive, in order to maintain accuracy in the Mercator projection.
Likewise, longitude is limited to -180 to +180.
I have a data set of different locations, and want to show the nearest locations (within 5 km).
How can I determine the minimum/maximum of latitude and longitude?
f.e.: I need to fill my car up, and am looking for all gas stations in my neighborhood so I can go to the nearest. How do I do this on an Android phone?
I'd like to avoid iterating through all of the locations as well, because I've got about 2500 locations and rising. Any suggestions on that?
Thank you guys in advance for the advice on this!
Update:
Thank you for the feedback you guys gave me, I solved my issue by iterating through all locations on the server and using the Google Distance Matrix API to calculate the distances: http://code.google.com/intl/nl/apis/maps/documentation/distancematrix/
Simplifying, latitude is the angle over/under the equator, longitude is angle right/left of greenwich meridian.
So to calculate (on average) how much for example 1º latitude is, you convert it to radians (multiply by PI/180), and then multiply by Earth's mean radius (6,371.0 km).
For your question, the process is the inverse one: take 5 km and convert it to degrees:
Divide it by Earth's radius
Multiply by 180/PI
This way you will get delta degrees, that is, how much degrees are 5 kms (on average, if you want exactitude, you will need the exact Earth radius differentials over those 5 kms) with which you can build a circle around the given location (just like a compass would).
All the calcuations give and methods are approximations but well within tolerances for what you require.
The earth circumference is approx 40076000 metres.
the distance traveled per degree of latitude is allways the same and is simply a proportion of the earth circ.
the distance travel per degree in longitude however changes depending upon your latitude ( this rings on the glode get smaller nearer the poles ).
so for a given distance m, the corresponing Latitude and Longitude values are
earthcirc = 40076000;
// at Lat and Lon for distance m (in meters)
LatDelta = (m * 360) / earthcirc;
LonDelta = (m * 360) / abs(eathcirc*cos(lat));
This gives you your square lat long deltas for a simple search of your data. but on fingin a candidate your should then do a full distance calc as the corner of the square is quite a bit more than 5 KM away.
distance between 2 lat/longs
distLat = (lat1-lat2) * earthcirc) / 360;
distLong = (long1-long2) * earthcirc * cos((lat1+lat2)/2) / 360;
dist = sqrt( sqr(distLat) + sqr(distLong) );
I know most compilers/languages use radians for cos/sin functions but its easire to explain in degrees.
as for searching your data the simplest way is to order in be either lat or long then you can do a binary search to find the possible location to check instead of a full scan. There are better ways to order the data ( quad trees ) but for 2500 ish entries i wouldnt bother
There are two issues here, 1) how to calculate the distance between two pairs of lat/lon and 2) how to find the point with shortest distance to a given point.
There are formulas on the net, some more accurate than others, for example http://www.movable-type.co.uk/scripts/latlong.html
This is a (geo) spatial indexing problem (http://en.wikipedia.org/wiki/Spatial_index#Spatial_Index ). You can use for example a quad tree with lat/lng as X/Y (I assume your points are not too close to the polars, which complicate things but still doable). The quad tree let you find in Log(N) time the neighborhood of your car without having to iterate over all points.
Not exact code but hopefully it will help.
hi friends in my gps application i did convert Geo point to pixels. i have two geo points so i convert this both of two points to pixels now i take different of this both pixels points and i want to convert this difference to kilometers
I wouldn't recommend using the view pixels to do distance calculation. If you have the geo points, you should use those. It all comes down to some geodetic calculation. And the accuracy depends on how you model the earth. What you want is to use geodetic great circle lines to perform distance calculations.
If you model the earth as a sphere (using law of cosines):
double earthAverageRadius = 6378137; //Average mean in meters when modeling the world as a sphere
double angle = Math.acos(Math.sin(point1.x) * Math.sin(point2.x)
+ Math.cos(point1.x) * Math.cos(point2.x) * Math.cos(point1.y- point2.y));
double distance = angle * pi * earthAverageRadius; // distance in metres
I would also recommend looking into the Haversine formula, which is numerically more stable. Using the haversine formula, the angle calculated in the previous code would be:
double a = Math.pow(Math.sin((point2.x-point1.x)/2.0), 2.0)
+ Math.cos(point1.x) * Math.cos(point2.x) * Math.pow(Math.sin((point2.y-point1.y)/2.0), 2.0);
double angle = 2 * Math.asin(Math.min(1.0, Math.sqrt(a)));
If you want increased accuracy (for large distances), you should consider modeling the earth as an ellipsoid, though the calculations for this are considerably harder.
EDIT: Also note that the above only holds if you give longitude and latitude in radians. So you'll have to make that conversion first too.