安卓:如何用GPS测量距离 [英] Android: How to measure distance with gps

查看:195
本文介绍了安卓:如何用GPS测量距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个应用程序,这是采取的经度和纬度上的当前位置的用户。和算一时,什么距离行驶公里数的用户。

在currentLat和currentLon是当前的经纬度的user.But的我不知道该怎么把经纬度为endLat和ednLon。

对不起我的英文不好。

先谢谢了。

///////////////////////////////////

我做到了应用程序,但现在只有一个小问题。
当我开始第一次节目,我得到价值5536,当我重新启动该程序,我得到正常值0.0

我的英语不好再难过。 :)

和帮助我的家伙谢谢,你是最好的:)

 公共类全球定位系统扩展了活动{ TextView中显示;
  双currentLon = 0;
  双currentLat = 0;
  双lastLon = 0;
  双lastLat = 0;
  双距离;
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_test);
    显示=(的TextView)findViewById(R.id.info);  LM的LocationManager =(的LocationManager)getSystemService(LOCATION_SERVICE);
                lm.requestLocationUpdates(lm.GPS_PROVIDER,0,0,Loclist);
                位置LOC = lm.getLastKnownLocation(lm.GPS_PROVIDER);                如果(LOC == NULL){
                    display.setText(无GPS定位找到);
                    }
                    其他{
                        //设置当前经纬度
                        currentLon = loc.getLongitude();
                        currentLat = loc.getLatitude();                        }
                //设置最后的纬度和经度
                lastLat = currentLat;
                lastLon = currentLon;
} LocationListener的Loclist =新LocationListener的(){
@覆盖
公共无效onLocationChanged(地点){
    // TODO自动生成方法存根     //启动位置管理器
     LM的LocationManager =(的LocationManager)getSystemService(LOCATION_SERVICE);      //获取最后一个位置
     位置LOC = lm.getLastKnownLocation(lm.GPS_PROVIDER);    //请求新位置
      lm.requestLocationUpdates(lm.GPS_PROVIDER,0,0,Loclist);      //获取新位置
      位置LOC2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);      //获取当前的经纬度和长
     currentLat = loc.getLatitude();
     currentLon = loc.getLongitude();
    位置locationA =新的位置(点A);
        locationA.setLatitude(lastLat);
        locationA.setLongitude(lastLon);    位置locationB =新的位置(点B);
        locationB.setLatitude(currentLat);
        locationB.setLongitude(currentLon);        双distanceMeters = locationA.distanceTo(locationB);        双distanceKm = distanceMeters / 1000F;        display.setText(的String.format(。%2F公里,distanceKm));        }@覆盖
公共无效onProviderDisabled(字符串提供商){
    // TODO自动生成方法存根}@覆盖
公共无效onProviderEnabled(字符串提供商){
    // TODO自动生成方法存根}@覆盖
公共无效onStatusChanged(字符串提供商,INT地位,捆绑演员){
    // TODO自动生成方法存根} };}


解决方案

  

但我不知道该怎么把经纬度为endLat和ednLon。


endLat,endLon是一个令人困惑的名字:

您应该有lastLat,lastLon:在onGpsUpdate的previous呼叫收到的纬度,经度,保存到你例如distanceCalculator.lastLat
其中,distanceCalculator是一个对象,你存储lastLat和lastLon

那么你应该有curLat,curLon:当前纬度,经度在onGPSUpdate交付

然后从(lastLat,lastLon)计算距离 - >(curLat,curLon)
并更新distanceCalculator.lastLat和lastLon。

I am trying to make a application,which is take latitude and longitude on current place of the user. And count momentary,what distance is travel the user in km.

The "currentLat" and "currentLon" is current latitude and longitude of the user.But I don't know what latitude and longitude to put for "endLat" and "ednLon".

Sorry for my bad english.

Thanks in advance.

///////////////////////////////////

I made it the app,but now have just one little problem. When I started first time the program,I get value 5536 and when I restarted the program I get normal value 0.0

Again sorry for my bad english. :)

And guys thanks for helping me,you are the best :)

  public class Gps extends Activity   {

 TextView display;


  double currentLon=0 ;
  double currentLat=0 ;
  double lastLon = 0;
  double lastLat = 0;
  double distance;




public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);




    display = (TextView) findViewById(R.id.info);

  LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE); 
                lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);
                Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

                if(loc==null){
                    display.setText("No GPS location found");
                    }
                    else{
                        //set Current latitude and longitude
                        currentLon=loc.getLongitude();
                        currentLat=loc.getLatitude();

                        }
                //Set the last latitude and longitude
                lastLat=currentLat;
                lastLon=currentLon ;


}



 LocationListener Loclist = new LocationListener(){




@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

     //start location manager
     LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE);

      //Get last location
     Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

    //Request new location
      lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);

      //Get new location
      Location loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);

      //get the current lat and long
     currentLat = loc.getLatitude();
     currentLon = loc.getLongitude();


    Location locationA = new Location("point A");
        locationA.setLatitude(lastLat);
        locationA.setLongitude(lastLon);

    Location locationB = new Location("point B");
        locationB.setLatitude(currentLat);
        locationB.setLongitude(currentLon);

        double distanceMeters = locationA.distanceTo(locationB);

        double distanceKm = distanceMeters / 1000f;

        display.setText(String.format("%.2f Km",distanceKm ));

        }



@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}



@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}



@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub



}

 };

}

解决方案

But I don't know what latitude and longitude to put for "endLat" and "ednLon".

endLat, endLon is a confusing name:

You should have lastLat, lastLon: the lat,lon received in the previous call of onGpsUpdate, save that into your e.g distanceCalculator.lastLat where distanceCalculator is an object where you store that lastLat, and lastLon

Then you should have curLat, curLon: current lat, lon delivered in onGPSUpdate

Then calculate the distance from (lastLat, lastLon) -> (curLat, curLon), and update your distanceCalculator.lastLat and lastLon.

这篇关于安卓:如何用GPS测量距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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