/[zanavi_public1]/navit/navit/android/src/itcrowd/gps/util/DistanceUtil.java
ZANavi

Contents of /navit/navit/android/src/itcrowd/gps/util/DistanceUtil.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 28 - (show annotations) (download)
Sun Jun 17 08:12:47 2012 UTC (11 years, 9 months ago) by zoff99
File size: 3533 byte(s)
lots of new stuff and fixes
1 /**
2 * ZANavi, Zoff Android Navigation system.
3 * Copyright (C) 2012 Zoff <zoff@zoff.cc>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /**
21 * Copyright (C) 2011 Dipl.-Ing.(FH) Thomas Schindel, Bad Elster
22 *
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * version 2 as published by the Free Software Foundation.
26 *
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the
34 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
35 * Boston, MA 02110-1301, USA.
36 */
37
38 /**
39 * Copyright (C) 2010 Marcin Skruch <mskruch@gmail.com>
40 *
41 * http://code.google.com/p/gps-tools/source/browse/trunk/src/itcrowd/gps/util/DistanceUtil.java?r=7
42 *
43 * This program is free software; you can redistribute it and/or
44 * modify it under the terms of the GNU General Public License
45 * version 2 as published by the Free Software Foundation.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, write to the
54 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
55 * Boston, MA 02110-1301, USA.
56 */
57
58 package itcrowd.gps.util;
59
60 /**
61 * Created by IntelliJ IDEA.
62 * User: Dipl.-Ing.(FH) Thomas Schindel, Bad Elster
63 * Date: 01.11.11
64 * Time: 18:03
65 * To change this template use File | Settings | File Templates.
66 */
67 public class DistanceUtil
68 {
69
70 public static double distance(double lat1, double lon1, double lat2, double lon2)
71 {
72
73 //Wenn lat1=lat2 und lon1=lon2 (keine Distance) hier abfangen
74 if ((lat1 == lat2) && (lon1 == lon2))
75 {
76 return 0;
77 }
78 else
79 //Sonst Distance berechnen und zurückgeben
80 return distance(lat1, lon1, lat2, lon2, 'K');
81 }
82
83 public static double distance(double lat1, double lon1, double lat2, double lon2, char unit)
84 {
85 double theta = lon1 - lon2;
86 double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
87 dist = Math.acos(dist);
88 dist = rad2deg(dist);
89 dist = dist * 60 * 1.1515;
90 if (unit == 'K')
91 {
92 dist = dist * 1.609344;
93 }
94 else if (unit == 'N')
95 {
96 dist = dist * 0.8684;
97 }
98 return (dist);
99 }
100
101 private static double deg2rad(double deg)
102 {
103 return (deg * Math.PI / 180.0);
104 }
105
106 private static double rad2deg(double rad)
107 {
108 return (rad * 180.0 / Math.PI);
109 }
110
111 }

   
Visit the ZANavi Wiki