두지점(위도,경도) 사이의 거리
java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package com.ghj.blog_036; import android.location.Location; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { //UI TextView txtJava; TextView txtAndroid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //UI txtJava = (TextView)findViewById(R.id.txtJava); txtAndroid = (TextView)findViewById(R.id.txtAndroid); txtJava.setText(DistanceByDegree(37.476780, 126.981429, 37.504445, 127.049317)+"m"); txtAndroid.setText(DistanceByDegreeAndroid(37.476780, 126.981429, 37.504445, 127.049317)+"m"); } //두지점(위도,경도) 사이의 거리 public double DistanceByDegree(double _latitude1, double _longitude1, double _latitude2, double _longitude2){ double theta, dist; theta = _longitude1 - _longitude2; dist = Math.sin(DegreeToRadian(_latitude1)) * Math.sin(DegreeToRadian(_latitude2)) + Math.cos(DegreeToRadian(_latitude1)) * Math.cos(DegreeToRadian(_latitude2)) * Math.cos(DegreeToRadian(theta)); dist = Math.acos(dist); dist = RadianToDegree(dist); dist = dist * 60 * 1.1515; dist = dist * 1.609344; // 단위 mile 에서 km 변환. dist = dist * 1000.0; // 단위 km 에서 m 로 변환 return dist; } //안드로이드 - 두지점(위도,경도) 사이의 거리 public double DistanceByDegreeAndroid(double _latitude1, double _longitude1, double _latitude2, double _longitude2){ Location startPos = new Location("PointA"); Location endPos = new Location("PointB"); startPos.setLatitude(_latitude1); startPos.setLongitude(_longitude1); endPos.setLatitude(_latitude2); endPos.setLongitude(_longitude2); double distance = startPos.distanceTo(endPos); return distance; } //degree->radian 변환 public double DegreeToRadian(double degree){ return degree * Math.PI / 180.0; } //randian -> degree 변환 public double RadianToDegree(double radian){ return radian * 180d / Math.PI; } } | cs |
xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="(37.476780, 126.981429) 와 (37.504445, 127.049317) 사이의 거리" android:textSize="24dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txtJava" android:textSize="24dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/txtAndroid" android:textSize="24dp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> | cs |
결과
두 API 사이에 차이가 있다
'IT > - 프로그래밍' 카테고리의 다른 글
안드로이드 구글맵1 구글맵 띄우기 (8) | 2016.11.21 |
---|---|
정보보안기사 리눅스 로그파일 (0) | 2016.11.19 |
안드로이드 좌표의 몇 m거리의 위도, 경도 구하기 (0) | 2016.11.17 |
안드로이드 좌표변환 도(degree)를 도분초로 변환 (0) | 2016.11.16 |
안드로이드 좌표변환 도분초를 도(degree)로 변환 (0) | 2016.11.15 |