How to calculate bearing between two latitude and longitude, I need to use this in google map for icon rotation.
import 'package:geolocator/geolocator.dart';
This geolocator has the provision to calculate bearing between two location.
double calculatedRotation = Geolocator.bearingBetween(
start.latitude,
start.longitude,
end.latitude,
end.longitude);
Related
I have two points as below. I need to get the distance between them in meters.
POINT (80.99456 7.86795)
POINT (80.97454 7.872174)
How can this be done via GeoPandas?
Your points are in a lon, lat coordinate system (EPSG:4326 or WGS 84). To calculate a distance in meters, you would need to either use the Great-circle distance or project them in a local coordinate system to approximate the distance with a good precision.
For Sri Lanka, you can use EPSG:5234 and in GeoPandas, you can use the distance function between two GeoDataFrames.
from shapely.geometry import Point
import geopandas as gpd
pnt1 = Point(80.99456, 7.86795)
pnt2 = Point(80.97454, 7.872174)
points_df = gpd.GeoDataFrame({'geometry': [pnt1, pnt2]}, crs='EPSG:4326')
points_df = points_df.to_crs('EPSG:5234')
points_df2 = points_df.shift() #We shift the dataframe by 1 to align pnt1 with pnt2
points_df.distance(points_df2)
The result should be 2261.92843 m
Is GeoPy a wrapper of another implementation? Where can I find a Scala or Java equivalent solution to measure distances between two coordinates? Geotools does not work the way I want. For example, I have spotted a couple of cases in which the coordinates do not converge and return an error.
The GeographicLib-Java package (I wrote this) will do what you want.
It does all sorts of geodesic calculations on an ellipsoid. The solution
for the distance between two points always converges.
have also a look at Geocalc. I wrote it for one of my website
//Kew, London
Coordinate lat = new DegreeCoordinate(51.4843774);
Coordinate lng = new DegreeCoordinate(-0.2912044);
Point kew = new Point(lat, lng);
//Richmond, London
lat = new DegreeCoordinate(51.4613418);
lng = new DegreeCoordinate(-0.3035466);
Point richmond = new Point(lat, lng);
double distance = EarthCalc.getDistance(richmond, kew); //in meters
double distance = EarthCalc.getHarvesineDistance(richmond, kew); //in meters
double distance = EarthCalc.getVincentyDistance(richmond, kew); //in meters
In the past I've worked with JTS, it's a very rich library for processing georeferenced data. If you want to use it in Scala it's not very idiomatic but I know there's a binding library that may fit your needs.
Say I have a square which consists of four CLLocationCoordinate2D points, which are in lat, lon, and I want to find the area of the square in meters. I convert the CLLocationCoordinate2D points into MKMapPoints, and I find the area in X-Y space. However, the area I find is in the units of MKMapPoint, which don't directly translate to meters. How can I translate this area in MKMapPoint-space back into meters?
The MapKit function MKMetersBetweenMapPoints makes this easier.
For example, if you wanted to get the area of the currently displayed region:
MKMapPoint mpTopLeft = mapView.visibleMapRect.origin;
MKMapPoint mpTopRight = MKMapPointMake(
mapView.visibleMapRect.origin.x + mapView.visibleMapRect.size.width,
mapView.visibleMapRect.origin.y);
MKMapPoint mpBottomRight = MKMapPointMake(
mapView.visibleMapRect.origin.x + mapView.visibleMapRect.size.width,
mapView.visibleMapRect.origin.y + mapView.visibleMapRect.size.height);
CLLocationDistance hDist = MKMetersBetweenMapPoints(mpTopLeft, mpTopRight);
CLLocationDistance vDist = MKMetersBetweenMapPoints(mpTopRight, mpBottomRight);
double vmrArea = hDist * vDist;
The documentation states that the function takes "into account the curvature of the Earth."
You can use the Haversine formula to calculate it, assuming that the earth is a perfect sphere.
To understand how lat/lon vs meters works in the context of the earth, you may find it interesting to read about Nautical miles.
You can find some more resources and some sample code by googling objective-c Haversine formula.
Enjoy!
I want the exact position on mkmapkit that is x y cordinate from lat lon values .
I used the followind code
CLLocationCoordinate2D neCoord;
neCoord.latitude = 72.2234;
neCoord.longitude = 23.340876;
//Here i have passed hardcoded lat lon
nePoint = [map_view convertCoordinate:neCoord toPointToView:map_view];
But i get the cordinates to be negative values.
So any idea or suggestions for the same are accepted.
Maybe you're getting negative numbers because the MKMapView isn't showing the region where the coordinates are? If they're both negative, then the location is NW of the current displayed map region.
as title how to? i have tried the code from google earth, but seem like the result is different with the google map calculation result. below provided the code i did
-(double)GetDistance:(double)lat1 long1:(double)lng1 la2:(double)lat2 long2:(double)lng2 {
//NSLog(#"latitude 1:%.7f,longitude1:%.7f,latitude2:%.7f,longtitude2:%.7f",lat1,lng1,lat2,lng2);
double radLat1 = [self rad:lat1];
double radLat2 = [self rad:lat2];
double a = radLat1 - radLat2;
double b = [self rad:lng1] -[self rad:lng2];
double s = 2 * asin(sqrt(pow(sin(a/2),2) + cos(radLat1)*cos(radLat2)*pow(sin(b/2),2)));
s = s * EARTH_RADIUS;
s = round(s * 10000) / 10000;
return s;
}
-(double)rad:(double)d
{
return d *3.14159265 / 180.0;
}
the EARTH_RADIUS value is 6378.138
by using this function by provided two coordinates the result come out is 4.5kM
but when i use google map get direction between two same coordinates, it show me the distance is about 8km
can anyone help to point out the problem of my code?
Since this is tagged iPhone, why not use the built-in distance function rather than rolling your own? location1 and location2 are CLLocation objects.
CLLocationDistance distance = [location1 getDistanceFrom:location2];
Here is a simple code (supposing you just have latitude and longitude of the two points)
CLLocation *startLocation = [[CLLocation alloc] initWithLatitude:startLatitude longitude:startLongitude];
CLLocation *endLocation = [[CLLocation alloc] initWithLatitude:endLatitude longitude:endLongitude];
CLLocationDistance distance = [startLocation distanceFromLocation:endLocation]; // aka double
Don't forget to add MapKit Framework to your project, and import MapKit in your file :
#import <MapKit/MapKit.h>
Google Maps is likely to be giving you the driving distance, whereas the great circle equation you have listed is going to be the straight line surface distance. If there was a straight line surface road directly from point A to point B, Google Maps would likely give you the same distance as the equation you have there.
Since
getDistanceFrom:
isDeprecated
Try use the
[newLocation distanceFromLocation:oldLocation
You should be able to use the google API directly to calculate either great circle distance or driving distance depending on your application needs.
See GLatLong::distanceFrom and GDirections::getDistance.