本文介绍了显示累积距离GPS。字符串转换/双的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序的功能是通过GPS显示累积距离当人行走。显示在像null9.55355.9346855.94574547和由第二增加的距离值。我的GOS敏感位置的变化非常快,即使我没有动我的手机。但主要问题是如何增加价值,并不能使其增加其长度。我想打开字符串DIST到Double1和double2 + =的valueOf double1的。然后将其转换double2我TextView的字符串和显示。但我不知道如何在Android的转换。首先我甚至不知道我的想法是可行的。

编辑:当我运行我的输出变为1.093507E7。为什么有E7的?它确实对我TextView的显示器,但随后不久后坠毁。

我的Java code

 公共类MainActivity延伸活动{受保护的LocationManager的LocationManager;
的EditText userNumberInput;
的EditText userTextInput;
TextView的distanceText;
TextView的纬度;
TextView的经度;
双LAT1,lon1,LAT2,lon2,lat3,lon3,lat4,lon4;
串DIST =;
字符串值;
双finalDist1,finalDist2;
浮动[]的结果;
私有静态最后长MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; //在米
    私有静态最后长MINIMUM_TIME_BETWEEN_UPDATES = 0;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    distanceText =(的TextView)findViewById(R.id.Distance);
    纬度=(的TextView)findViewById(R.id.currentLat);
    经度=(的TextView)findViewById(R.id.currentLon);    的LocationManager =(的LocationManager)getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, myLocationListener);
    Log.d(GPS已启用,GPS功能的);
    标准标准=新标准();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    字符串提供商= locationManager.getBestProvider(标准,真正的);
    地点= locationManager.getLastKnownLocation(供应商);    如果(位置!= NULL)
    {
        //显示在吐司当前位置
        字符串消息=的String.format(
                当前位置\\ n经度:%1 $ S \\北纬:%2 $ S
                location.getLongitude(),location.getLatitude()
        );
        Toast.makeText(MainActivity.this,消息,
                Toast.LENGTH_LONG).show();        //显示在TextView的当前位置
        latitude.setText(当前纬度:+将String.valueOf(location.getLatitude()));
        longitude.setText(当前经度:+将String.valueOf(location.getLongitude()));
        LAT1 = location.getLatitude();
        lon1 = location.getLongitude();
    }
    否则,如果(位置== NULL)
    {
        Toast.makeText(MainActivity.this,
                位置为空,
                Toast.LENGTH_LONG).show();
    }}私人CharSequence中的ToString(双latitude2){
    // TODO自动生成方法存根
    返回null;
}LocationListener的myLocationListener =新LocationListener的()
{
    公共无效onLocationChanged(位置LOC2)
    {
        Toast.makeText(MainActivity.this,
                位置发生了变化,
                Toast.LENGTH_LONG).show();
        如果(LOC2!= NULL)
        {
            latitude.setText(当前纬度:+将String.valueOf(loc2.getLatitude()));
            longitude.setText(当前经度:+将String.valueOf(loc2.getLongitude()));
            浮动[] =结果新的浮动[1];
            Location.distanceBetween(LAT1,lon1,loc2.getLatitude(),loc2.getLongitude(),结果);
            的System.out.println(距离是:+结果[0]);
            如果(DIST = NULL&放大器;!&安培;!将String.valueOf(结果[0])= NULL)
            {
                DIST + =将String.valueOf(结果[0]); //你的字符串
                finalDist1 = Double.parseDouble(DIST);
                finalDist2 + = finalDist1;
                distanceText.setText(将String.valueOf(finalDist2));
                Toast.makeText(MainActivity.this,
                        距离积累,
                        Toast.LENGTH_LONG).show();
                LAT1 = loc2.getLatitude();
                lon1 = loc2.getLongitude();
            }
            Toast.makeText(MainActivity.this,
                    距离,
                    Toast.LENGTH_LONG).show();
            LAT1 = loc2.getLatitude();
            lon1 = loc2.getLongitude();
        }
}公共无效onProviderDisabled(字符串提供商)
{
    Toast.makeText(MainActivity.this,
            GPS已禁用,
            Toast.LENGTH_LONG).show();
}
公共无效onProviderEnabled(字符串提供商)
{
    Toast.makeText(MainActivity.this,
            启用GPS
            Toast.LENGTH_LONG).show();
}
公共无效onStatusChanged(字符串提供商,INT状态,
        捆绑演员)
{
    Toast.makeText(MainActivity.this,
            GPS状态改为
            Toast.LENGTH_LONG).show();
}};
@覆盖
保护无效的onPause(){
super.onPause();
locationManager.removeUpdates(myLocationListener);
}
@覆盖
保护无效onResume(){
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, myLocationListener);
}}

日志猫

  01-29 23:53:04.814:D / gralloc_goldfish(1076):无仿真模拟GPU检测。
01-29 23:53:16.444:我/的System.out(1076):距离是:1.093507E7
01-29 23:53:16.614:I /编舞(1076):45跳过帧!该应用程序可能会做它的主线程的工作太多了。
01-29 23:53:23.784:I /编舞(1076):57跳过帧!该应用程序可能会做它的主线程的工作太多了。
01-29 23:53:34.774:我/的System.out(1076):距离是:6412915.0
01-29 23:53:34.774:D / AndroidRuntime(1076):关闭VM
01-29 23:53:34.774:W / dalvikvm(1076):主题ID = 1:螺纹未捕获的异常(组= 0xb3a3dba8)退出
01-29 23:53:34.814:E / AndroidRuntime(1076):致命异常:主要
01-29 23:53:34.814:E / AndroidRuntime(1076):工艺:com.example.validationapp,PID:1076
01-29 23:53:34.814:E / AndroidRuntime(1076):java.lang.NumberFormatException:无效的双:1.093507E76412915.0
01-29 23:53:34.814:E / AndroidRuntime(1076):在java.lang.StringToReal.invalidReal(StringToReal.java:63)
01-29 23:53:34.814:E / AndroidRuntime(1076):在java.lang.StringToReal.initialParse(StringToReal.java:114)
01-29 23:53:34.814:E / AndroidRuntime(1076):在java.lang.StringToReal.parseDouble(StringToReal.java:263)
01-29 23:53:34.814:E / AndroidRuntime(1076):在java.lang.Double.parseDouble(Double.java:295)
01-29 23:53:34.814:E / AndroidRuntime(1076):在com.example.validationapp.MainActivity $ 1.onLocationChanged(MainActivity.java:112)
01-29 23:53:34.814:E / AndroidRuntime(1076):在android.location.LocationManager $ ListenerTransport._handleMessage(LocationManager.java:279)
01-29 23:53:34.814:E / AndroidRuntime(1076):在android.location.LocationManager $ ListenerTransport.access $ 000(LocationManager.java:208)
01-29 23:53:34.814:E / AndroidRuntime(1076):在android.location.LocationManager $ ListenerTransport $ 1.handleMessage(LocationManager.java:224)
01-29 23:53:34.814:E / AndroidRuntime(1076):在android.os.Handler.dispatchMessage(Handler.java:102)
01-29 23:53:34.814:E / AndroidRuntime(1076):在android.os.Looper.loop(Looper.java:136)
01-29 23:53:34.814:E / AndroidRuntime(1076):在android.app.ActivityThread.main(ActivityThread.java:5017)
01-29 23:53:34.814:E / AndroidRuntime(1076):在java.lang.reflect.Method.invokeNative(本机方法)
01-29 23:53:34.814:E / AndroidRuntime(1076):在java.lang.reflect.Method.invoke(Method.java:515)
01-29 23:53:34.814:E / AndroidRuntime(1076):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)
01-29 23:53:34.814:E / AndroidRuntime(1076):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-29 23:53:34.814:E / AndroidRuntime(1076):在dalvik.system.NativeStart.main(本机方法)
01-29 23:53:38.814:I /流程(1076):发送信号。 PID:1076 SIG:9


解决方案

看着你最新的修改,我想你已经过认为这。尝试这样的事情吧。注意我们只存储一个双击值的距离,我们增加每个用户已经走过的时间。

 公共类MainActivity延伸活动{  受保护的LocationManager的LocationManager;
  的EditText userNumberInput;
  的EditText userTextInput;
  TextView的distanceText;
  TextView的纬度;
  TextView的经度;
  双LAT1,lon1,LAT2,lon2,lat3,lon3,lat4,lon4;
  双DIST = 0;
  字符串值;
  浮动[]的结果;
  私有静态最后长MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; //在米
  私有静态最后长MINIMUM_TIME_BETWEEN_UPDATES = 0;  //方法略  LocationListener的myLocationListener =新LocationListener的()
  {
    公共无效onLocationChanged(位置LOC2)
    {
      Toast.makeText(MainActivity.this,
      位置发生了变化,
      Toast.LENGTH_LONG).show();
      如果(LOC2!= NULL)
      {
        latitude.setText(当前纬度:+将String.valueOf(loc2.getLatitude()));
        longitude.setText(当前经度:+将String.valueOf(loc2.getLongitude()));
        浮动[] =结果新的浮动[1];
        Location.distanceBetween(LAT1,lon1,loc2.getLatitude(),loc2.getLongitude(),结果);
        的System.out.println(距离是:+结果[0]);        DIST + =结果[0];
        DecimalFormat的DF =新的DecimalFormat(###); //这个调整适当
        distanceText.setText(df.format(DIST));        Toast.makeText(MainActivity.this,
        距离,
        Toast.LENGTH_LONG).show();
        LAT1 = loc2.getLatitude();
        lon1 = loc2.getLongitude();
      }
    }  //方法略

my android app function is displaying accumulated distance through gps when the person is walking. the distance value displayed in like null9.55355.9346855.94574547 and increasing by the second. my gos is sensitive location change very fast even though i am not moving my phone. but the main problem is how do i add the value and not make it increase its length. i was thinking of turning the string dist to a Double1 and double2 += valueof double1. then convert double2 to string and display of my TextView. but i do not know how to convert in android. in the first place i am not even sure my idea can work.

EDIT: when i run my output becomes 1.093507E7 . why is there the E7? it did display on my textview but then it crash soon after.

My java code

    public class MainActivity extends Activity{

protected LocationManager locationManager;
EditText userNumberInput;
EditText userTextInput;
TextView distanceText;
TextView latitude;
TextView longitude;
double lat1,lon1,lat2,lon2,lat3,lon3,lat4,lon4;
String dist = "";
String value;
double finalDist1, finalDist2;
float[] result;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    distanceText=(TextView)findViewById(R.id.Distance);
    latitude=(TextView)findViewById(R.id.currentLat);
    longitude=(TextView)findViewById(R.id.currentLon);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, myLocationListener);
    Log.d("GPS Enabled", "GPS Enabled");
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, true);
    Location location=locationManager.getLastKnownLocation(provider);

    if(location!= null)
    {
        //Display current location in Toast
        String message = String.format(
                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(MainActivity.this, message,
                Toast.LENGTH_LONG).show();

        //Display current location in textview
        latitude.setText("Current Latitude: " + String.valueOf(location.getLatitude()));
        longitude.setText("Current Longitude: " + String.valueOf(location.getLongitude()));
        lat1 = location.getLatitude();
        lon1 = location.getLongitude();
    }
    else if(location == null)
    {
        Toast.makeText(MainActivity.this,
                "Location is null",
                Toast.LENGTH_LONG).show();
    }

}

private CharSequence ToString(double latitude2) {
    // TODO Auto-generated method stub
    return null;
}

LocationListener myLocationListener = new LocationListener()
{
    public void onLocationChanged(Location loc2)
    {
        Toast.makeText(MainActivity.this,
                "Location has changed",
                Toast.LENGTH_LONG).show();
        if(loc2 != null)
        {
            latitude.setText("Current Latitude: " + String.valueOf(loc2.getLatitude()));
            longitude.setText("Current Longitude: " + String.valueOf(loc2.getLongitude()));
            float[] results = new float[1];
            Location.distanceBetween(lat1, lon1, loc2.getLatitude(), loc2.getLongitude(), results);
            System.out.println("Distance is: " + results[0]);


            if(dist!=null &&  String.valueOf(results[0])!=null)
            {
                dist += String.valueOf(results[0]); // your String
                finalDist1 = Double.parseDouble(dist);
                finalDist2 += finalDist1;
                distanceText.setText(String.valueOf(finalDist2));
                Toast.makeText(MainActivity.this,
                        "Distance is accumulated",
                        Toast.LENGTH_LONG).show();
                lat1=loc2.getLatitude();
                lon1=loc2.getLongitude();
            }


            Toast.makeText(MainActivity.this,
                    "Distance",
                    Toast.LENGTH_LONG).show();
            lat1=loc2.getLatitude();
            lon1=loc2.getLongitude();
        }
}

public void onProviderDisabled(String provider)
{
    Toast.makeText(MainActivity.this,
            "GPS is disabled",
            Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String provider)
{
    Toast.makeText(MainActivity.this,
            "GPS is enabled",
            Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status,
        Bundle extras)
{
    Toast.makeText(MainActivity.this,
            "GPS status changed",
            Toast.LENGTH_LONG).show();
}

};
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(myLocationListener);
}
@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, myLocationListener);
}}

log cat

01-29 23:53:04.814: D/gralloc_goldfish(1076): Emulator without GPU emulation detected.
01-29 23:53:16.444: I/System.out(1076): Distance is: 1.093507E7
01-29 23:53:16.614: I/Choreographer(1076): Skipped 45 frames!  The application may be doing too much work on its main thread.
01-29 23:53:23.784: I/Choreographer(1076): Skipped 57 frames!  The application may be doing too much work on its main thread.
01-29 23:53:34.774: I/System.out(1076): Distance is: 6412915.0
01-29 23:53:34.774: D/AndroidRuntime(1076): Shutting down VM
01-29 23:53:34.774: W/dalvikvm(1076): threadid=1: thread exiting with uncaught exception (group=0xb3a3dba8)
01-29 23:53:34.814: E/AndroidRuntime(1076): FATAL EXCEPTION: main
01-29 23:53:34.814: E/AndroidRuntime(1076): Process: com.example.validationapp, PID: 1076
01-29 23:53:34.814: E/AndroidRuntime(1076): java.lang.NumberFormatException: Invalid double: "1.093507E76412915.0"
01-29 23:53:34.814: E/AndroidRuntime(1076):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at java.lang.StringToReal.initialParse(StringToReal.java:114)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at java.lang.StringToReal.parseDouble(StringToReal.java:263)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at java.lang.Double.parseDouble(Double.java:295)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at com.example.validationapp.MainActivity$1.onLocationChanged(MainActivity.java:112)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:279)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:208)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:224)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at android.os.Looper.loop(Looper.java:136)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at   android.app.ActivityThread.main(ActivityThread.java:5017)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at java.lang.reflect.Method.invoke(Method.java:515)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-29 23:53:34.814: E/AndroidRuntime(1076):     at dalvik.system.NativeStart.main(Native Method)
01-29 23:53:38.814: I/Process(1076): Sending signal. PID: 1076 SIG: 9
解决方案

Looking at your latest edits, I think you've over-thought this. Try something like this instead. Note how we just store one double value for your distance and we increment that each time the user has travelled.

public class MainActivity extends Activity{

  protected LocationManager locationManager;
  EditText userNumberInput;
  EditText userTextInput;
  TextView distanceText;
  TextView latitude;
  TextView longitude;
  double lat1,lon1,lat2,lon2,lat3,lon3,lat4,lon4;
  double dist = 0;
  String value;
  float[] result;
  private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters
  private static final long MINIMUM_TIME_BETWEEN_UPDATES = 0;

  // methods omitted

  LocationListener myLocationListener = new LocationListener()
  {
    public void onLocationChanged(Location loc2)
    {
      Toast.makeText(MainActivity.this,
      "Location has changed",
      Toast.LENGTH_LONG).show();
      if(loc2 != null)
      {
        latitude.setText("Current Latitude: " + String.valueOf(loc2.getLatitude()));
        longitude.setText("Current Longitude: " + String.valueOf(loc2.getLongitude()));
        float[] results = new float[1];
        Location.distanceBetween(lat1, lon1, loc2.getLatitude(), loc2.getLongitude(), results);
        System.out.println("Distance is: " + results[0]);

        dist += results[0];
        DecimalFormat df = new DecimalFormat("#.##"); // adjust this as appropriate
        distanceText.setText(df.format(dist));

        Toast.makeText(MainActivity.this,
        "Distance",
        Toast.LENGTH_LONG).show();
        lat1=loc2.getLatitude();
        lon1=loc2.getLongitude();
      }
    }

  // methods omitted

这篇关于显示累积距离GPS。字符串转换/双的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 05:40