๋‚˜๋งŒ์˜ ์˜ค๋ž˜๋‹ฌ๋ฆฌ๊ธฐ๋ฅผ ํ•˜๋Š” ์‚ฌ๋žŒ

Become a ghost

Programming/Android

[Android/JAVA] ์œ„๋„, ๊ฒฝ๋„ ๊ฐ’์œผ๋กœ ๊ฑฐ๋ฆฌ ๊ตฌํ•˜๊ธฐ (Km, M, S)

jennnnna 2024. 2. 8. 20:53

์•ˆ๋“œ๋กœ์ด๋“œ๋กœ ๋ฉ”๋ชจ์žฅ์„ ๊ตฌํ˜„ ์ค‘์ด๋‹ค. 
์ œ๋ชฉ, ๋‚ด์šฉ๊ณผ ํ•จ๊ป˜ ์žฅ์†Œ๋„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค. (์›ํ•˜๋Š” ์žฅ์†Œ) 
๊ตฌ๊ธ€ ์žฅ์†Œ ๊ฒ€์ƒ‰ api๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ๋‚ด๊ฐ€ ๊ฒ€์ƒ‰ํ•œ ์žฅ์†Œ์˜ ํ‚ค์›Œ๋“œ์™€ ์ฃผ์†Œ๋ฅผ ๊ฐ™์ด ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋งŒ๋“ค์—ˆ๋‹ค. 

๋˜ํ•œ ์•ˆ๋“œ๋กœ์ด๋“œ notification ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•ด ๋‚ด๊ฐ€ ์ €์žฅํ•œ ์žฅ์†Œ๋“ค๋กœ๋ถ€ํ„ฐ
์‚ฌ์šฉ์ž๊ฐ€ ์ผ์ • ๋ฒ”์œ„ ์ด๋‚ด๋กœ ๋“ค์–ด์˜ค๋ฉด, ์•Œ๋žŒ์ด ์šธ๋ฆฌ๋Š” ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜๋ ค๊ณ  ํ•œ๋‹ค. 

๋จผ์ € ๋‚ด ์œ„์น˜์™€ ์ €์žฅ๋œ ์žฅ์†Œ๋“ค์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด๋ณด๋„๋ก ํ•œ๋‹ค. 
GPS์™€ ๊ตฌ๊ธ€ API๋กœ๋ถ€ํ„ฐ ์œ„๋„, ๊ฒฝ๋„ ๊ฐ’์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ์ด์šฉํ•ด ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋„๋ก ํ•œ๋‹ค. 
์—๋ฎฌ๋ ˆ์ดํ„ฐ์˜ ์œ„์น˜, ์ฆ‰ ์‚ฌ์šฉ์ž์˜ ํ˜„์žฌ ์œ„์น˜๋Š” GPS ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ›์•„์™”๋‹ค. 
๋ฉ”๋ชจ์žฅ์„ ์ž‘์„ฑํ•˜๋ฉฐ ์žฅ์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ, ํ•ด๋‹น ์žฅ์†Œ์˜ ์œ„๋„, ๊ฒฝ๋„๊ฐ’๋„ ํ•จ๊ป˜ ๋ฐ›์•„์„œ ์‰์–ด๋“œ ํ”„๋ฆฌํผ๋Ÿฐ์Šค์— 
๋”ฐ๋กœ ์ €์žฅ์„ ์‹œ์ผฐ๋‹ค. 

์ด ๊ฒŒ์‹œ๊ธ€์— ์ฒจ๋ถ€ํ•œ ์ฝ”๋“œ๋Š” ์œ„์—์„œ 
์œ„๋„, ๊ฒฝ๋„ ๊ฐ’์„ ํ™œ์šฉํ•˜์—ฌ ๋‘ ์ง€์  ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•˜๋Š” ์˜ˆ์ œ ๋‚ด์šฉ์ด๋‹ค.
๋ฌด์ž‘์œ„๋กœ ๋‘ ์žฅ์†Œ๋ฅผ ๊ณจ๋ผ์„œ ์œ„๋„, ๊ฒฝ๋„ ๊ฐ’์„ ๋„ฃ์–ด๋ณด์•˜๋‹ค ! 

 

package com.example.distanceexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    double lat1 ;
    double lat2 ;
    double lon1;
    double lon2;

    TextView dist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lat1 = 37.48593340000001; // ๋™์ž‘๊ตฌ ์–ด๋”˜๊ฐ€ 
        lon1 = 126.9687783; // ๋™์ž‘๊ตฌ ์–ด๋”˜๊ฐ€ . . . 

        lat2 = 37.73753610000001; // ๊ฒฝ๊ธฐ๋„ ์–‘์ฃผ 
        lon2 = 127.0462942; // ๊ฒฝ๊ธฐ๋„ ์–‘์ฃผ 


        String unit="K";
        double  howFar = distance(lat1, lon1, lat2, lon2,unit);

        dist = findViewById(R.id.distance);
        dist.setText("๋‘ ์žฅ์†Œ์˜ ๊ฑฐ๋ฆฌ๋Š” "+howFar + "km ์ž…๋‹ˆ๋‹ค.");


    }

    private static double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
        if ((lat1 == lat2) && (lon1 == lon2)) {
            return 0;
        }
        else {
            double theta = lon1 - lon2;
            double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta));
            dist = Math.acos(dist);
            dist = Math.toDegrees(dist);
            dist = dist * 60 * 1.1515;
            if (unit.equals("K")) {
                dist = dist * 1.609344;
            } else if (unit.equals("N")) {
                dist = dist * 0.8684;
            }
            return (dist);
        }
    }

}

์ด์ œ ๋‘ ์ง€์  ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ๋ฅผ ๊ตฌํ•ด๋ณด์ž. (์ง์„ ๊ฑฐ๋ฆฌ)
๋‘์ง€์ ์˜ ์œ„๋„(latitude)์™€ ๊ฒฝ๋„(longitude) ๋ฅผ double ํ˜•์œผ๋กœ
๊ทธ๋ฆฌ๊ณ  ๋ฐ˜ํ™˜๋ฐ›๊ณ ์ž ํ•˜๋Š” ๊ฑฐ๋ฆฌ์˜ ๋‹จ์œ„ (Km ํ‚ฌ๋กœ๋ฏธํ„ฐ, M ๋งˆ์ผ, N ํ•ด๋ฆฌ )๋ฅผ String ๊ฐ’์œผ๋กœ unit ์— ๋‹ด์•„์„œ double ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” distance ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋œ๋‹ค. 

๋ฉ”์†Œ๋“œ ์•ˆ์—๋Š” 
Haversine ๊ณต์‹์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์‚ผ๊ฐํ•จ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ๋‚ด์šฉ์ด ๋‹ด๊ฒจ ์žˆ๋‹ค !

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ๋‚ด์šฉ์€ ์•„๋‹ˆ๊ณ  ์•„์ฃผ ๊น”๋”ํ•˜๊ณ  ์นœ์ ˆํ•œ ์˜คํ”ˆ ์†Œ์Šค๊ฐ€ ์žˆ์–ด์„œ ์ฐธ๊ณ ํ–ˆ๋‹ค.
Geo Data Souce ๋ผ๋Š” ์‚ฌ์ดํŠธ์ธ๋ฐ, ์ „์„ธ๊ณ„ ๋‹ค์–‘ํ•œ ๊ตญ๊ฐ€ ๋„์‹œ์— ๋Œ€ํ•œ ์ง€ํ˜•, ์ง€์—ญ ์ •๋ณด๋“ค์˜ ์˜จ๋ผ์ธ ๋ฐ์ดํ„ฐ ๋ฒ ์ด์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ์‚ฌ์ดํŠธ๋ผ๊ณ  ํ•œ๋‹ค. developer ๋ฉ”๋‰ด๊ฐ€ ๋”ฐ๋กœ ์žˆ์–ด์„œ ์ด๋ ‡๊ฒŒ ์˜ˆ์ œ ์ฝ”๋“œ๋„ ์ œ๊ณตํ•˜๋Š” ๋“ฏ. 
์ž๋ฐ” ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ PHP, C ๋“ฑ๋“ฑ ๋‹ค์–‘ํ•˜๊ฒŒ ์˜ˆ์ œ๊ฐ€ ์žˆ์Œ. 

https://www.geodatasource.com/developers/java 

์›ํ•˜๋Š” ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 
์ง์„  ๊ฑฐ๋ฆฌ๊ฐ€ ๊ณ„์‚ฐ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋„ค๋น„๊ฒŒ์ด์…˜์—์„œ ๋งํ•˜๋Š” ๊ฑฐ๋ฆฌ์™€๋Š” ๋‹น์—ฐํžˆ ๋‹ค๋ฅด๋‹ค. 


์œ„ ๊ฒฐ๊ณผ ๊ฐ’์€ ์ˆซ์ž๊ฐ€ ๋งค์šฐ ๊ธธ๊ฒŒ ๋‚˜์˜จ๋‹ค. 
๋ณดํ†ต ๊ฑฐ๋ฆฌ๋ฅผ ๋”ฐ์งˆ ๋•Œ์—๋Š” ์†Œ์ˆ˜์  ์ฒซ์งธ, ํ˜น์€ ๋‘˜์งธ ์ž๋ฆฌ๊นŒ์ง€๋งŒ ๋ณด์—ฌ์ค˜๋„ ์ถฉ๋ถ„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 
return ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ์ „์— ์‹ค์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ํ•œ ์ค„ ์ถ”๊ฐ€ํ•ด๋ณธ๋‹ค. 

private static double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
        if ((lat1 == lat2) && (lon1 == lon2)) {
            return 0;
        }
        else {
            double theta = lon1 - lon2;
            double dist = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta));
            dist = Math.acos(dist);
            dist = Math.toDegrees(dist);
            dist = dist * 60 * 1.1515;
            if (unit.equals("K")) {
                dist = dist * 1.609344;
            } else if (unit.equals("N")) {
                dist = dist * 0.8684;
            }
            dist = Math.round(dist * 10.0) / 10.0; // ์†Œ์ˆ˜์  ์ฒซ์งธ์ž๋ฆฌ๊นŒ์ง€๋งŒ ๋‚˜์˜ค๊ฒŒ ํ•˜๋Š” ๊ณต์‹
            // ์ž๋ฐ” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Math ์— ์žˆ๋Š” round ๋ฉ”์†Œ๋“œ
            // ๋งŒ์•ฝ ์†Œ์ˆ˜์  ๋‘˜์งธ์ž๋ฆฌ ๊นŒ์ง€ ํ•„์š”ํ•˜๋‹ค๋ฉด 100.0 
            return dist;
        }
    }

 

์งœ์ž” ~! 

์†Œ์ˆ˜์  ๋‘๋ฒˆ์งธ ์ž๋ฆฌ๊ฐ€ ๋ฐ˜์˜ฌ๋ฆผ๋˜์–ด 28.8 km ๋ผ๊ณ  ๋‚˜์˜จ๋‹ค ! 

๋