I'm trying to come up with function that could fill in gps coordinates between two points every second. There are few posts about this here, but I couldn't find something complete. The closest answer I found was:
Interpolate between 2 GPS locations based on walking speed
I modified one of the answer using the bearing. However, it still doesn't seem to work. Especially I think the distance calculation is wrong. Could someone look at the code below and change?
Thank you!
import java.util.ArrayList;
public class Test {
double radius = 6371;
public Test() {
Location start = new Location(lat, lon);
Location end = new Location(lat, lon);
double speed = 1.39;
double distance = CalculateDistanceBetweenLocations(start, end);
double duration = distance / speed;
System.out.println(distance + ", " + speed + ", " + duration);
ArrayList<Location> locations = new ArrayList<Location>();
for (double i = 0; i < duration; i += 1.0) {
double bearing = CalculateBearing(start, end);
double distanceInKm = speed / 1000;
Location intermediaryLocation = CalculateDestinationLocation(start, bearing, distanceInKm);
locations.add(intermediaryLocation);
System.out.println(intermediaryLocation.latitude + ", " + intermediaryLocation.longitude);
start = intermediaryLocation;
}
}
double DegToRad(double deg) {
return (deg * Math.PI / 180);
}
double RadToDeg(double rad) {
return (rad * 180 / Math.PI);
}
double CalculateBearing(Location startPoint, Location endPoint) {
double lat1 = DegToRad(startPoint.latitude);
double lat2 = DegToRad(endPoint.latitude);
double deltaLon = DegToRad(endPoint.longitude - startPoint.longitude);
double y = Math.sin(deltaLon) * Math.cos(lat2);
double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(deltaLon);
double bearing = Math.atan2(y, x);
return (RadToDeg(bearing) + 360) % 360;
}
Location CalculateDestinationLocation(Location point, double bearing, double distance) {
distance = distance / radius;
bearing = DegToRad(bearing);
double lat1 = DegToRad(point.latitude);
double lon1 = DegToRad(point.longitude);
double lat2 = Math
.asin(Math.sin(lat1) * Math.cos(distance) + Math.cos(lat1) * Math.sin(distance) * Math.cos(bearing));
double lon2 = lon1 + Math.atan2(Math.sin(bearing) * Math.sin(distance) * Math.cos(lat1),
Math.cos(distance) - Math.sin(lat1) * Math.sin(lat2));
lon2 = (lon2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI;
return new Location(RadToDeg(lat2), RadToDeg(lon2));
}
double CalculateDistanceBetweenLocations(Location startPoint, Location endPoint) {
double lat1 = DegToRad(startPoint.latitude);
double lon1 = DegToRad(startPoint.longitude);
double lat2 = DegToRad(endPoint.latitude);
double lon2 = DegToRad(endPoint.longitude);
double deltaLat = lat2 - lat1;
double deltaLon = lon2 - lon1;
double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2)
+ Math.cos(lat1) * Math.cos(lat2) * Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - 1));
return (radius * c);
}
public static void main(String[] args) {
new Test();
}
class Location {
public double latitude, longitude;
public Location(double lat, double lon) {
latitude = lat;
longitude = lon;
}
}
}
You have a typing error in line
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - 1));
It should be
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
Your method CalculateDistanceBetweenLocations contains an this line:
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - 1));
which is equivalent to
double c = 2 * Math.atan2(Math.sqrt(a), 0.0);
which means that the result of Math.atan2 is always pi, independent of the value of a as long as a is positive.
Therefore CalculateDistanceBetweenLocations always returns 20015.086796020572 independent of the input coordinates.
Related
Using GPS points I am calculating by holding previous and current points with
location1.distanceTo(location2) by adding each time to a variable on some time diff to get traveled distance. Is it good approach to get vehicle movement distance? Is any better approach to get accurate travel distance during moving vehicle?
Use the following code. I hope it will help.
public double CalculationByDistance(LatLng StartP, LatLng EndP) {
int Radius = 6371;// radius of earth in Km
double lat1 = StartP.latitude;
double lat2 = EndP.latitude;
double lon1 = StartP.longitude;
double lon2 = EndP.longitude;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
int kmInDec = Integer.valueOf(newFormat.format(km));
double meter = valueResult % 1000;
int meterInDec = Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value", "" + valueResult + " KM " + kmInDec
+ " Meter " + meterInDec);
return Radius * c;
}
float[] results = new float[1];
Location.distanceBetween(oldPosition.latitude, oldPosition.longitude,
newPosition.latitude, newPosition.longitude, results);
i have used Haversine for calculating distance between two location.
public static class Haversine {
static int Radius = 6371;
public static double haversine(double lat1, double lon1, double lat2,
double lon2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
int kmInDec = Integer.valueOf(newFormat.format(km));
double meter = valueResult % 1000;
int meterInDec = Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value", "" + valueResult + " KM " + kmInDec
+ " Meter " + meterInDec);
return Radius * c;
/*double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1)* Math.cos(lat2);
double c = 2 * Math.asin(Math.sqrt(a));
return R * 2 * Math.asin(Math.sqrt(a));*/
}
}
From the above code i am not able to get exact distance between 2 location.
When i run the above mehtod then it shows 4.32 km from my two places but when i checked on the google map then it shows the 5.3 km .
i have also used Location.distanceBetween method still it shows the 4.32 km .
How can i get exact distance between location?
You can see this link.
Haversine and Location.distanceBetween method are both the origin to the point at which a line.
So, you can use http://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&sensor=false to get the real distance.
From Google official repository link
SphericalUtil
MathUtil
Usage
double distance = SphericalUtil.computeDistanceBetween(new LatLng(9.000,10.00), new LatLng(9.000,11.00));
The above method will returns the distance between two LatLngs, in meters. Or try this
private String getDistance(LatLng my_latlong,LatLng frnd_latlong){
Location l1=new Location("One");
l1.setLatitude(my_latlong.latitude);
l1.setLongitude(my_latlong.longitude);
Location l2=new Location("Two");
l2.setLatitude(frnd_latlong.latitude);
l2.setLongitude(frnd_latlong.longitude);
float distance=l1.distanceTo(l2);
String dist=distance+" M";
if(distance>1000.0f)
{
distance=distance/1000.0f;
dist=distance+" KM";
}
return dist;
}
or you can give a look at link
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am creating a project in which i'm using Google direction API to calculate distance between multiple latitude and longitude. But that is showing incorrect. I want to use Java for calculating the distance between multiple latitude and longitude.
Here is my java code
private static double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private static double rad2deg(double rad) {
return (rad * 180 / Math.PI);
}
It is not exact, and Dutch, but I don't think it is hard to understand:
package nl.abz.testjaap;
public class Locatie {
private static final double DOORSNEDE_AARDE = 40000.0;
public final double latitude;
public final double longitude;
public Locatie(double latitude, double longitude) throws IllegalArgumentException {
if (latitude > 90 || latitude < -90) {
throw new IllegalArgumentException("latitude moet tussen - 90 en 90 liggen, was "
+ latitude);
}
if (longitude < -360 || longitude > 360) {
throw new IllegalArgumentException("Longitude moet tussen -360 en 360 liggen was "
+ longitude);
}
this.latitude = latitude;
this.longitude = longitude;
}
/**
* Calculate the distance in KM
*
* #param l2
* #return
*/
public double afstandTot(Locatie l2) {
double lon1 = longitude / 180 * Math.PI;
double lon2 = l2.longitude / 180 * Math.PI;
double lat1 = latitude / 180 * Math.PI;
double lat2 = l2.latitude / 180 * Math.PI;
double x1 = Math.cos(lon1) * Math.cos(lat1);
double y1 = Math.sin(lon1) * Math.cos(lat1);
double z1 = Math.sin(lat1);
double x2 = Math.cos(lon2) * Math.cos(lat2);
double y2 = Math.sin(lon2) * Math.cos(lat2);
double z2 = Math.sin(lat2);
double psi = Math.acos(x1 * x2 + y1 * y2 + z1 * z2);
double afstand = DOORSNEDE_AARDE / (2 * Math.PI) * psi;
return afstand;
}
}
To calculate distance between 2 points you need to use Haversine formula.
In java:
public class Haversine {
public static final double R = 6372.8; // In kilometers
// calculate distance with haversine formula
public static double haversine(double lat1, double lon1, double lat2, double lon2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.asin(Math.sqrt(a));
return R * c;
}
// example how to use
public static void main(String[] args) {
System.out.println(haversine(36.12, -86.67, 33.94, -118.40));
}
}
Please try this code:
private static double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double rad2deg(double rad) {
return (rad * 180 / Math.PI);
}
I still have issues in calculating the lat/lng for 2nd point provided and lat/lng for 1nd point and distance.
i found one solution here in Javascript, I tried to convert to Java. but the result was not accurate, it seems I've done something wrong.
How to calculate the latlng of a point a certain distance away from another?
public class Misc {
double EARTH_RADIUS_METERS= 6378.1 *1024;
private double toRad(double value) {
return value* (Math.PI/ 180);
}
private double toDeg (double value) {
return value* (180 / Math.PI);
}
/*-------------------------------------------------------------------------
* Given a starting lat/lon point on earth, distance (in meters)
* and bearing, calculates destination coordinates lat2/lon2.
*
* all params in radians
*-------------------------------------------------------------------------*/
GPoint destCoordsInRadians(double lat1, double lon1,
double distanceMeters, double bearing
/*,double* lat2, double* lon2*/)
{
//-------------------------------------------------------------------------
// Algorithm from http://www.geomidpoint.com/destination/calculation.html
// Algorithm also at http://www.movable-type.co.uk/scripts/latlong.html
//
// Spherical Earth Model
// 1. Let radiusEarth = 6372.7976 km or radiusEarth=3959.8728 miles
// 2. Convert distance to the distance in radians.
// dist = dist/radiusEarth
// 3. Calculate the destination coordinates.
// lat2 = asin(sin(lat1)*cos(dist) + cos(lat1)*sin(dist)*cos(brg))
// lon2 = lon1 + atan2(sin(brg)*sin(dist)*cos(lat1), cos(dist)-sin(lat1)*sin(lat2))
//-------------------------------------------------------------------------
double distRadians = distanceMeters / EARTH_RADIUS_METERS;
double lat2 = Math.asin( Math.sin(lat1) * Math.cos(distRadians) + Math.cos(lat1) * Math.sin(distRadians) * Math.cos(bearing));
double lon2 = lon1 + Math.atan2( Math.sin(bearing) * Math.sin(distRadians) * Math.cos(lat1),
Math.cos(distRadians) - Math.sin(lat1) * Math.sin(lat2) );
return new GPoint( lat2 , lon2 );
}
/*-------------------------------------------------------------------------
* Given a starting lat/lon point on earth, distance (in meters)
* and bearing, calculates destination coordinates lat2/lon2.
*
* all params in degrees
*-------------------------------------------------------------------------*/
GPoint destCoordsInDegrees(double lat1, double lon1,
double distanceMeters, double bearing/*,
double* lat2, double* lon2*/)
{
GPoint gPoint2=destCoordsInRadians(/*Deg_to_*/toRad(lat1), /*Deg_to_*/toRad(lon1),
distanceMeters, /*Deg_to_*/toRad(bearing)/*,
lat2, lon2*/);
gPoint2.lat = /*Rad_to_*/toDeg( gPoint2.lat );
gPoint2.lon = normalize180( /*Rad_to_*/toDeg( gPoint2.lon )) ;
return gPoint2;
}
/*-------------------------------------------------------------------------
* Given two lat/lon points on earth, calculates the heading
* from lat1/lon1 to lat2/lon2.
*
* lat/lon params in radians
* result in radians
*-------------------------------------------------------------------------*/
double headingInRadians(double lat1, double lon1, double lat2, double lon2)
{
//-------------------------------------------------------------------------
// Algorithm found at http://www.movable-type.co.uk/scripts/latlong.html
//
// Spherical Law of Cosines
//
// Formula: ? = atan2( sin(?lon) * cos(lat2),
// cos(lat1) * sin(lat2) ? sin(lat1) * cos(lat2) * cos(?lon) )
// JavaScript:
//
// var y = Math.sin(dLon) * Math.cos(lat2);
// var x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
// var brng = Math.atan2(y, x).toDeg();
//-------------------------------------------------------------------------
double dLon = lon2 - lon1;
double y = Math.sin(dLon) * Math.cos(lat2);
double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) * Math.cos(lat2) * Math.cos(dLon);
return Math.atan2(y, x);
}
/*-------------------------------------------------------------------------
* Given two lat/lon points on earth, calculates the heading
* from lat1/lon1 to lat2/lon2.
*
* lat/lon params in degrees
* result in degrees
*-------------------------------------------------------------------------*/
double headingInDegrees(double lat1, double lon1, double lat2, double lon2)
{
return /*Rad_to_*/toDeg( headingInRadians(/*Deg_to_*/toRad(lat1),
/*Deg_to_*/toRad(lon1),
/*Deg_to_*/toRad(lat2),
/*Deg_to_*/toRad(lon2)) );
}
// Normalize a heading in degrees to be within -179.999999° to 180.00000°
double normalize180(double heading)
{
while (true) {
if (heading <= -180) {
heading += 360;
} else if (heading > 180) {
heading -= 360;
} else {
return heading;
}
}
}
// Normalize a heading in degrees to be within -179.999999° to 180.00000°
float normalize180f(float heading)
{
while (true) {
if (heading <= -180) {
heading += 360;
} else if (heading > 180) {
heading -= 360;
} else {
return heading;
}
}
}
// Normalize a heading in degrees to be within 0° to 359.999999°
double normalize360(double heading)
{
while (true) {
if (heading < 0) {
heading += 360;
} else if (heading >= 360) {
heading -= 360;
} else {
return heading;
}
}
}
// Normalize a heading in degrees to be within 0° to 359.999999°
float normalize360f(float heading)
{
while (true) {
if (heading < 0) {
heading += 360;
} else if (heading >= 360) {
heading -= 360;
} else {
return heading;
}
}
}
}
You need one more parameter, namely "bearing". Then:
var d = radius/6378800; // 6378800 is Earth radius in meters
var lat1 = (PI/180)* centerLat;
var lng1 = (PI/180)* centerLng;
// Go around a circle from 0 to 360 degrees, every 10 degrees or set a to your desired bearing, in degrees.
for (var a = 0 ; a < 361 ; a+=10 ) {
var tc = (PI/180)*a;
var y = asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc));
var dlng = atan2(sin(tc)*sin(d)*cos(lat1),cos(d)-sin(lat1)*sin(y));
var x = ((lng1-dlng+PI) % (2*PI)) - PI ;
var lat = y*(180/PI);
var lon = x*(180/PI);
// Convert the lat and lon to pixel, if needed. (x,y)
}
Originally from this other SO thread:
How to change 1 meter to pixel distance?
here is great answer for this question:
you can use android-maps-utils with method:
SphericalUtil.computeOffset(latLng, dist, brng)
in case you want to do it by your own:
private LatLng getDestinationPoint(LatLng source, double brng, double dist) {
dist = dist / 6371;
brng = Math.toRadians(brng);
double lat1 = Math.toRadians(source.latitude), lon1 = Math.toRadians(source.longitude);
double lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) +
Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
double lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) *
Math.cos(lat1),
Math.cos(dist) - Math.sin(lat1) *
Math.sin(lat2));
if (Double.isNaN(lat2) || Double.isNaN(lon2)) {
return null;
}
return new LatLng(Math.toDegrees(lat2), Math.toDegrees(lon2));
}
I want to add direction arrows to my application to make it looks like this. Is there any solutions or advices?
Here is my solution:
import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
import com.jettaxi.R;
import java.util.ArrayList;
public class ArrowDirectionsOverlay extends Overlay {
private Context context;
private ArrayList<GeoPoint> directions;
private Bitmap arrowBitmap;
private int maximalPositionX;
private int maximalPositionY;
public ArrowDirectionsOverlay(Context context) {
this.context = context;
directions = new ArrayList<GeoPoint>();
arrowBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.direction);
}
public void addDirectionPoint(GeoPoint destination) {
directions.add(destination);
}
public void clear() {
directions.clear();
}
private double getAngle(GeoPoint center, GeoPoint destination) {
double lat1 = center.getLatitudeE6() / 1000000.;
double lon1 = center.getLongitudeE6() / 1000000.;
double lat2 = destination.getLatitudeE6() / 1000000.;
double lon2 = destination.getLongitudeE6() / 1000000.;
int MAXITERS = 20;
// Convert lat/long to radians
lat1 *= Math.PI / 180.0;
lat2 *= Math.PI / 180.0;
lon1 *= Math.PI / 180.0;
lon2 *= Math.PI / 180.0;
double a = 6378137.0; // WGS84 major axis
double b = 6356752.3142; // WGS84 semi-major axis
double f = (a - b) / a;
double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b);
double L = lon2 - lon1;
double A = 0.0;
double U1 = Math.atan((1.0 - f) * Math.tan(lat1));
double U2 = Math.atan((1.0 - f) * Math.tan(lat2));
double cosU1 = Math.cos(U1);
double cosU2 = Math.cos(U2);
double sinU1 = Math.sin(U1);
double sinU2 = Math.sin(U2);
double cosU1cosU2 = cosU1 * cosU2;
double sinU1sinU2 = sinU1 * sinU2;
double sigma = 0.0;
double deltaSigma = 0.0;
double cosSqAlpha = 0.0;
double cos2SM = 0.0;
double cosSigma = 0.0;
double sinSigma = 0.0;
double cosLambda = 0.0;
double sinLambda = 0.0;
double lambda = L; // initial guess
for (int iter = 0; iter < MAXITERS; iter++) {
double lambdaOrig = lambda;
cosLambda = Math.cos(lambda);
sinLambda = Math.sin(lambda);
double t1 = cosU2 * sinLambda;
double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda;
double sinSqSigma = t1 * t1 + t2 * t2; // (14)
sinSigma = Math.sqrt(sinSqSigma);
cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda; // (15)
sigma = Math.atan2(sinSigma, cosSigma); // (16)
double sinAlpha = (sinSigma == 0) ? 0.0 :
cosU1cosU2 * sinLambda / sinSigma; // (17)
cosSqAlpha = 1.0 - sinAlpha * sinAlpha;
cos2SM = (cosSqAlpha == 0) ? 0.0 :
cosSigma - 2.0 * sinU1sinU2 / cosSqAlpha; // (18)
double uSquared = cosSqAlpha * aSqMinusBSqOverBSq; // defn
A = 1 + (uSquared / 16384.0) * // (3)
(4096.0 + uSquared *
(-768 + uSquared * (320.0 - 175.0 * uSquared)));
double B = (uSquared / 1024.0) * // (4)
(256.0 + uSquared *
(-128.0 + uSquared * (74.0 - 47.0 * uSquared)));
double C = (f / 16.0) *
cosSqAlpha *
(4.0 + f * (4.0 - 3.0 * cosSqAlpha)); // (10)
double cos2SMSq = cos2SM * cos2SM;
deltaSigma = B * sinSigma * // (6)
(cos2SM + (B / 4.0) *
(cosSigma * (-1.0 + 2.0 * cos2SMSq) -
(B / 6.0) * cos2SM *
(-3.0 + 4.0 * sinSigma * sinSigma) *
(-3.0 + 4.0 * cos2SMSq)));
lambda = L +
(1.0 - C) * f * sinAlpha *
(sigma + C * sinSigma *
(cos2SM + C * cosSigma *
(-1.0 + 2.0 * cos2SM * cos2SM))); // (11)
double delta = (lambda - lambdaOrig) / lambda;
if (Math.abs(delta) < 1.0e-12) {
break;
}
}
float distance = (float) (b * A * (sigma - deltaSigma));
float initialBearing = (float) Math.atan2(cosU2 * sinLambda,
cosU1 * sinU2 - sinU1 * cosU2 * cosLambda);
initialBearing *= 180.0 / Math.PI;
float finalBearing = (float) Math.atan2(cosU1 * sinLambda,
-sinU1 * cosU2 + cosU1 * sinU2 * cosLambda);
finalBearing *= 180.0 / Math.PI;
return finalBearing;
}
private void drawArrow(Canvas canvas, MapView mapView, GeoPoint destination) {
Point screenPts = mapView.getProjection().toPixels(destination, null);
int deltaX = mapView.getMapCenter().getLatitudeE6() - destination.getLatitudeE6();
int deltaY = mapView.getMapCenter().getLongitudeE6() - destination.getLongitudeE6();
double angle = getAngle(mapView.getMapCenter(), destination);
double tan = Math.tan(Math.toRadians(angle));
Matrix matrix = new Matrix();
matrix.postRotate((float) angle);
Bitmap rotatedBmp = Bitmap.createBitmap(
arrowBitmap,
0, 0,
arrowBitmap.getWidth(),
arrowBitmap.getHeight(),
matrix,
true
);
int currentPositionX = screenPts.x - (rotatedBmp.getWidth() / 2);
int currentPositionY = screenPts.y - (rotatedBmp.getHeight() / 2);
if ((currentPositionX < 0) || (currentPositionY < 0) ||
(currentPositionX > maximalPositionX) || (currentPositionY > maximalPositionY)) {
int arrowPositionX = (int) (mapView.getWidth() / 2 - Math.signum(deltaX) * mapView.getHeight() / 2 * tan);
arrowPositionX = Math.min(Math.max(arrowPositionX, 0), maximalPositionX);
int arrowSpanX = (int) (mapView.getWidth() / 2.);
int arrowPositionY = (int) (mapView.getHeight() / 2 + Math.signum(deltaY) * arrowSpanX / tan);
arrowPositionY = Math.min(Math.max(arrowPositionY, 0), maximalPositionY);
canvas.drawBitmap(
rotatedBmp,
arrowPositionX,
arrowPositionY,
null
);
}
}
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
super.draw(canvas, mapView, shadow);
maximalPositionX = mapView.getWidth() - arrowBitmap.getWidth();
maximalPositionY = mapView.getHeight() - arrowBitmap.getHeight();
for (GeoPoint geoPoint : directions) {
drawArrow(canvas, mapView, geoPoint);
}
}
}
Bitmap arrow points top in my case
My solution would be to do a search for the places of interest (in this case coffee shops) which are within a certain distance from the users current location. This distance could be a 'screen' north, south, east or west of your current location - the screen distance will depend on your zoom level. The shops which are not on the current screen would then be represented by an arrow placed on the screen edge with the direction set between your current location and the shop.