{Android应用}如何从我的当前位置和指定的点显示的距离? [英] {Android App} How can I display distance from my current location and specified point?

查看:166
本文介绍了{Android应用}如何从我的当前位置和指定的点显示的距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究这几个小时,唯一的答案,我看到我指向
http://developer.android.com/reference/android/location/Location.html

和使用方法

公共静态无效distanceBetween(双startLatitude,双startLongitude,双endLatitude,双endLongitude,浮法[]的结果)

我需要帮助了解如何工作的有关我的应用程序。这就是我如何获取我的位置。

 的LocationManager的LocationManager;
    字符串语境= Context.LOCATION_SERVICE;
    的LocationManager =(的LocationManager)getSystemService(背景);    字符串提供商= LocationManager.GPS_PROVIDER;
    地点= locationManager.getLastKnownLocation(供应商);    updateWithNewLocation(位置);
  }  私人无效updateWithNewLocation(地点){
    串latLongString;
    TextView的myLocationText;
    myLocationText =(的TextView)findViewById(R.id.myLocationText);
    如果(位置!= NULL){
      双纬度= location.getLatitude();
      双LNG = location.getLongitude();
      latLongString =纬度:+纬度+\\ nLong:+ LNG;
    }其他{
      latLongString =没有发现地点;
    }
    myLocationText.setText(您当前的位置为:\\ n+
                           latLongString);
  }

有人可以帮我出了一点理解如何导入我的当前位置到这个方程,然后让我的应用程序显示的距离是多少?
谢谢


解决方案

 公共双calcdist()
            {
                诠释百万= 1000000;
                INT EARTH_RADIUS_KM = 6371;                双LAT1 = LA1 /万元;位置1 //纬度
            双lon1 = LO1 /万元; //位置1的经度
            双LAT2 = LA2 /万元; //位置2纬度
            双lon2 = L02 /万元; //经度位置2            双lat1Rad = Math.toRadians(LAT1);
            双lat2Rad = Math.toRadians(LAT2);
            双deltaLonRad = Math.toRadians(lon2 - lon1);            双DIST = Math.acos(Math.sin(lat1Rad)* Math.sin(lat2Rad)+ Math.cos(lat1Rad)* Math.cos(lat2Rad)
                    * Math.cos(deltaLonRad))
                    * EARTH_RADIUS_KM;
            返回DIST; }

您可以使用此code,如果你已经有纬度和两个位置的经度。

i have researched this for hours and the only answers i see point me to http://developer.android.com/reference/android/location/Location.html

and to use the Method

public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)

i need help understanding how this works in relation to my app. this is how i am retrieving my location.

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);

    String provider = LocationManager.GPS_PROVIDER;
    Location location = locationManager.getLastKnownLocation(provider);

    updateWithNewLocation(location);
  }

  private void updateWithNewLocation(Location location) {
    String latLongString;
    TextView myLocationText; 
    myLocationText = (TextView)findViewById(R.id.myLocationText);
    if (location != null) {
      double lat = location.getLatitude();
      double lng = location.getLongitude();
      latLongString = "Lat:" + lat + "\nLong:" + lng;
    } else {
      latLongString = "No location found"; 
    }
    myLocationText.setText("Your Current Position is:\n" + 
                           latLongString);
  }

Can someone please help me out a little in understanding how to import my current location into this equation and then having the distance shown in my app? thank you

解决方案

public double calcdist()
            {
                int MILLION = 1000000;
                int EARTH_RADIUS_KM = 6371;

                double lat1 = la1 / MILLION;// latitude of location 1
            double lon1 = lo1 / MILLION; //longitude of location 1
            double lat2 = la2 / MILLION; //latitude of location 2
            double lon2 = lo2 / MILLION;//longitude of location 2

            double lat1Rad = Math.toRadians(lat1);
            double lat2Rad = Math.toRadians(lat2);
            double deltaLonRad = Math.toRadians(lon2 - lon1);

            double dist = Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad) * Math.cos(lat2Rad)
                    * Math.cos(deltaLonRad))
                    * EARTH_RADIUS_KM;
            return dist;

 }

You can use this code if you already got latitude and longitude of both the locations.

这篇关于{Android应用}如何从我的当前位置和指定的点显示的距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆