0

我的 FirstActivity 包含此代码

mHandler.postDelayed(mLaunchTask,10000);
private Runnable mLaunchTask = new Runnable() 
            {
                public void run() 
                {
                    Intent i = new Intent(getApplicationContext(),NearestPoint.class);
                    Bundle params = new Bundle();
                    params.putFloat("latiA", latA);
                    params.putFloat("longiA", longA);
                    params.putFloat("latiB", latB);
                    params.putFloat("longiB", longB);
                    params.putFloat("latiC", latC);
                    params.putFloat("longiC", longC);
                    i.putExtras(params);
                    startActivity(i);
                }
            };    

NearestPoint 活动包含此代码

public float latA, longA, latB, longB, latC, longC;
  Intent it = getIntent(); {        
   if (it != null)
   {
       Bundle params = it.getExtras();
       if (params != null)
       {
            latA = params.getFloat("latiA");
            longA = params.getFloat("longiA");  
            latB = params.getFloat("latiB");
            longB = params.getFloat("longiB");  
            latC = params.getFloat("latiC");
            longC = params.getFloat("longiC");  
        }
   }}

我无法在 NearestPoint 活动中正确访问 latA、longA 等的值。我尝试使用 Toast 显示:

Toast.makeText(getApplicationContext(),"\nPoint A: \nLatitude: " + latA  + "\nLongitude: " + longA,  Toast.LENGTH_LONG).show(); 

在 FirstActivity 它给出了正确的值,但在 NearestPoint 我得到 0.0 作为纬度和经度。

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gpsdistance"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gpsdistance.FirstActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.gpsdistance.NearestPoint"
            android:label="@string/title_activity_nearest_point" >
        </activity>
        <activity
            android:name="com.example.gpsdistance.MinimumDistance"
            android:label="@string/title_activity_minimum_distance" >
        </activity>
    </application>

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

这是我完整的 FirstActivity 课程:

package com.example.gpsdistance;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class FirstActivity extends Activity {

    Button btnShowLocation, btnMinDis;
    TextView display= null;
    //private final Context mctx;
    double latuser;
    double longuser;
    double dx1, dy1, delta_long1, delta_lat1, final_long1, final_lat1;
    double dx2, dy2, delta_long2, delta_lat2, final_long2, final_lat2;
    double dx3, dy3, delta_long3, delta_lat3, final_long3, final_lat3;
    public float latA, longA, latB, longB, latC, longC;
    public String latiA, longiA, latiB, longiB, latiC, longiC ;
    // GPSTracker class
    GPSTracker gps;

    void  getGPS() 
    {
      gps = new GPSTracker(FirstActivity.this);
    }

    void points()
    {

        latuser = gps.getLatitude();
        longuser = gps.getLongitude();

        dx1 = 120*Math.cos(35); 
        dy1 = 120*Math.sin(35); 
        delta_long1 = dx1/(111320*Math.cos(latuser));   
        delta_lat1 = dy1/110540;   
        final_long1 = longuser + delta_long1;
        final_lat1 = latuser + delta_lat1;
        dx2 = 150*Math.cos(38); 
        dy2 = 150*Math.sin(38); 
        delta_long2 = dx2/(111320*Math.cos(latuser));   
        delta_lat2 = dy2/110540;   
        final_long2 = longuser + delta_long2;
        final_lat2 = latuser + delta_lat2;
        dx3 = 180*Math.cos(38); 
        dy3 = 180*Math.sin(38); 
        delta_long3 = dx3/(111320*Math.cos(latuser));   
        delta_lat3 = dy3/110540;   
        final_long3 = longuser + delta_long3;
        final_lat3 = latuser + delta_lat3;

        latA= (float)final_lat1;
        longA= (float)final_long1;
        latB= (float)final_lat2;
        longB= (float)final_long2;
        latC= (float)final_lat3;
        longC= (float)final_long3;
    }

    private float getLatiA() {
          return this.latA;
        }
   /*public FirstActivity(Context ctx)
   {
       this.mctx = ctx;
       getGPS();
       points();
   } */
    private float computeDistance(double lat1, double lon1,
            double lat2, double lon2) {
            // Based on http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf
            // using the "Inverse Formula" (section 4)

            int MAXITERS = 20;
            // Convert lat/long to radians
            lat1 *= Math.PI / 180.0;
            lat2 *= Math.PI / 180.0;
            lon1 *= Math.PI / 180.0;
            lon2 *= Math.PI / 180.0;

            double a = 6378137.0; // WGS84 major axis
            double b = 6356752.3142; // WGS84 semi-major axis
            double f = (a - b) / a;
            double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b);

            double L = lon2 - lon1;
            double A = 0.0;
            double U1 = Math.atan((1.0 - f) * Math.tan(lat1));
            double U2 = Math.atan((1.0 - f) * Math.tan(lat2));

            double cosU1 = Math.cos(U1);
            double cosU2 = Math.cos(U2);
            double sinU1 = Math.sin(U1);
            double sinU2 = Math.sin(U2);
            double cosU1cosU2 = cosU1 * cosU2;
            double sinU1sinU2 = sinU1 * sinU2;

            double sigma = 0.0;
            double deltaSigma = 0.0;
            double cosSqAlpha = 0.0;
            double cos2SM = 0.0;
            double cosSigma = 0.0;
            double sinSigma = 0.0;
            double cosLambda = 0.0;
            double sinLambda = 0.0;

            double lambda = L; 
            for (int iter = 0; iter < MAXITERS; iter++) {
                double lambdaOrig = lambda;
                cosLambda = Math.cos(lambda);
                sinLambda = Math.sin(lambda);
                double t1 = cosU2 * sinLambda;
                double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda;
                double sinSqSigma = t1 * t1 + t2 * t2; 
                sinSigma = Math.sqrt(sinSqSigma);
                cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda; 
                sigma = Math.atan2(sinSigma, cosSigma); 
                double sinAlpha = (sinSigma == 0) ? 0.0 :
                    cosU1cosU2 * sinLambda / sinSigma; 
                cosSqAlpha = 1.0 - sinAlpha * sinAlpha;
                cos2SM = (cosSqAlpha == 0) ? 0.0 :
                    cosSigma - 2.0 * sinU1sinU2 / cosSqAlpha;

                double uSquared = cosSqAlpha * aSqMinusBSqOverBSq; 
                A = 1 + (uSquared / 16384.0) * 
                    (4096.0 + uSquared *
                     (-768 + uSquared * (320.0 - 175.0 * uSquared)));
                double B = (uSquared / 1024.0) * 
                    (256.0 + uSquared *
                     (-128.0 + uSquared * (74.0 - 47.0 * uSquared)));
                double C = (f / 16.0) *
                    cosSqAlpha *
                    (4.0 + f * (4.0 - 3.0 * cosSqAlpha)); 
                double cos2SMSq = cos2SM * cos2SM;
                deltaSigma = B * sinSigma * 
                    (cos2SM + (B / 4.0) *
                     (cosSigma * (-1.0 + 2.0 * cos2SMSq) -
                      (B / 6.0) * cos2SM *
                      (-3.0 + 4.0 * sinSigma * sinSigma) *
                      (-3.0 + 4.0 * cos2SMSq)));

                lambda = L +
                    (1.0 - C) * f * sinAlpha *
                    (sigma + C * sinSigma *
                     (cos2SM + C * cosSigma *
                      (-1.0 + 2.0 * cos2SM * cos2SM))); 

                double delta = (lambda - lambdaOrig) / lambda;
                if (Math.abs(delta) < 1.0e-12) {
                    break;
                }
            }

            float distance = (float) (b * A * (sigma - deltaSigma));

            return distance;
    }

    /*public FirstActivity()
        {
            getGPS();
            if(gps.canGetLocation())
            points();
        }*/


    @Override
    protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.firstactivity);
            final LinearLayout lView = new LinearLayout(this);  
            display= new TextView(this);
            btnMinDis=new Button(this);
            btnMinDis.setText("Nearest Point");
            final Handler mHandler = new Handler();
            btnShowLocation = (Button) findViewById(R.id.buttonLocation);

            btnShowLocation.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) 
                {
                    // TODO Auto-generated method stub

                    getGPS();
                    // check if GPS enabled
                    if(gps.canGetLocation())
                        {

                        points();
                            float initialdisA = computeDistance( latuser, longuser, latA, longA);
                            float initialdisB = computeDistance( latuser, longuser, latB, longB);
                            float initialdisC = computeDistance( latuser, longuser, latC, longC);         
                            Toast.makeText(getApplicationContext(),"Point A: \nLatitude: " + latA  + "\nLongitude: " + longA,  Toast.LENGTH_LONG).show();
                            lView.addView(display);
                            setContentView(lView);
                            display.setText("Initial Location is - \nLatitude: " + latuser + " N" + "\nLongitude: " + longuser + " E"
                                        + "\n\n\nPoint A:\n" + "Latitude: " + latA + "N" + "\nLongitude: " + longA + "E" 
                                        + "\nPoint B:\n" + "Latitude: " + latB + "N" + "\nLongitude: " + longB + "E" 
                                        + "\nPoint C:\n" + "Latitude: " + latC + "N" + "\nLongitude: " + longC + "E"
                                        + "\n\n\nInitial Distance A: " + initialdisA + "m" + "\nInitial Distance B: " + initialdisB + "m" + "\nInitial Distance C: " + initialdisC + "m");


                            mHandler.postDelayed(mLaunchTask,10000);
                        }
                    else
                        {
                    // GPS or Network is not enabled
                    // Ask user to enable GPS/network in settings
                    gps.showSettingsAlert();
                        }   
              }  

            private Runnable mLaunchTask = new Runnable() 
            {
                public void run() 
                {
                    Intent i = new Intent(getApplicationContext(),NearestPoint.class);
                   Bundle params = new Bundle();
                    params.putFloat("latiA", getLatiA());
                    params.putFloat("longiA", longA);
                    params.putFloat("latiB", latB);
                    params.putFloat("longiB", longB);
                    params.putFloat("latiC", latC);
                    params.putFloat("longiC", longC);
                    i.putExtras(params); 
                    startActivity(i);
                }
            };    
         });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.firstactivity, menu);
        return true;
    }

}

这是完整项目的链接: http ://www.filedropper.com/gpsdistance

请帮忙。

4

2 回答 2

0

发生这种情况是因为在您实例化您的可运行文件时,latA、longA、latB、longB、latC、longC 的值为零。您应该为每个值使用一个 getter,如下所示:

mHandler.postDelayed(mLaunchTask,10000);
private Runnable mLaunchTask = new Runnable() 
            {
                public void run() 
                {
                    Intent i = new Intent(getApplicationContext(),NearestPoint.class);
                    Bundle params = new Bundle();
                    params.putFloat("latiA", getLatiA()); //You should to call a getter here
                    i.putExtras(params);
                    startActivity(i);
                }
            };    



//In your class     
private double getLatiA() {
  return this.latiA;
}
于 2013-01-19T14:21:47.870 回答
0

我不明白您为什么要在另一个线程中运行活动。我建议使用服务。http://developer.android.com/guide/components/services.html

于 2013-01-19T15:09:47.080 回答