使用GPS到两个位置(位置1到2),OnLocation已更改

时间:2011-09-28 04:07:11

标签: java android distance

我的目标是使用Android 2.2来查找使用GPS的距离,但是在我的位置更改方面存在一些问题。以前,我将我的代码放在On click listener上,但它不能正常工作。我设法在多次按下开始和停止按钮后才能获得距离。

public class MyLocationListener implements LocationListener
    {
        public void onLocationChanged(Location loc) {  
            if(originalCoordinates!=null){
                coordinates1 = getGPS();
                gpsEnd=coordinates1;
                double d = distFrom(originalCoordinates[0],originalCoordinates[1],gpsEnd[0],gpsEnd[1]);
                totalDistanceTravel+=d;
                disTrav.setText(Double.toString(d));
                txtViews.setText("" + coordinates1.length + " new counts!!!" + coordinates1[0] + "," + coordinates1[1] );


            }   
            if(coordinates!=null)
                {
                    double[] coordinatesPrev=coordinates;
                    txtView.setText("" + coordinates.length + " counts!!!" );
                    //double d = distFrom(coordinatesPrev[0],coordinatesPrev[1],coordinates[0],coordinates[1]);
                    //totalDistanceTravel+=d;
                }
                else
                {
                    coordinates = getGPS();       
                    gpsOrg=coordinates;
                    if(originalCoordinates == null){
                        originalCoordinates = gpsOrg;
                        coordinates1 = getGPS();
                        txtView.setText("" + originalCoordinates.length + " original counts!!! "  + originalCoordinates[0] + "," + originalCoordinates[1] );


                    }
                    //txtView.setText("" + coordinates.length + " counts" );
                }
        startButton.setEnabled(true);
        }

2 个答案:

答案 0 :(得分:0)

亲爱的,就这样做。

  1. 开启设备的GPS
  2. 等到它连接到卫星。
  3. 制作一个像booelan detection_position的旗帜。
  4. 在开始按钮上设置为true。
  5. OnLocationChange Listner,检查flag是否为true,然后获取 使用location.getLat和getLong ...进行分数并保存它们。
  6. 在这里你可以做一个技巧,初始化起始位置变量 null,然后在第一次迭代时检查startPosition是否为null 位置列表器将long和lat设置为起始位置 位置更改迭代设置long和lat到stop_location 变量
  7. 点击停止按钮设置标志为false。
  8. 现在你有开始和结束位置。使用距离公式 计算距离。

答案 1 :(得分:0)

首先采取行动,如下所示

public class MainActivity extends Activity implements OnClickListener {

Button btnStart = null;
Button btnStop = null;

Context context = null;
Button btnTouch = null;

float distance ;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.main);


    context = this;

    btnStart = (Button)findViewById(R.id.btnStart);
    btnStop = (Button)findViewById(R.id.btnStop);

    btnStart.setOnClickListener(this);
    btnStop.setOnClickListener(this);

}

@Override
public void onClick(View v) 
{
    Intent i =  new Intent(context, MyService.class);
    if(v.getId() == R.id.btnStart)
    {
        startService(i);
    }
    else if(v.getId() == R.id.btnStop)
    {
        distance = MyService.final_distance;
        stopService(i);
    }
   }

Button btnStart = null; Button btnStop = null; Context context = null; Button btnTouch = null; float distance ; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); context = this; btnStart = (Button)findViewById(R.id.btnStart); btnStop = (Button)findViewById(R.id.btnStop); btnStart.setOnClickListener(this); btnStop.setOnClickListener(this); } @Override public void onClick(View v) { Intent i = new Intent(context, MyService.class); if(v.getId() == R.id.btnStart) { startService(i); } else if(v.getId() == R.id.btnStop) { distance = MyService.final_distance; stopService(i); } }
然后采取一项服务并将我们的位置变更逻辑放在此服务中。我有使用服务,因为服务在后台运行继续,并在位置更新时查找位置更改。
}


public class MyService extends Service implements LocationListener {

 public static float final_distance = 0.0f;

private float distance = 0.0f;

private double old_latitude = 0.0;
private double old_longitude = 0.0;
private double new_latitude = 0.0;
private double new_longitude = 0.0;



LocationManager locationManager = null;


float[] result = null;

@Override    
public IBinder onBind(Intent intent) 
{
    return null;    
}        
@Override    
public void onCreate() 
{

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0L, 0.0f, this);

    result = new float[3];
}    
@Override    
public void onStart(Intent intent, int startid) 
{

} 
@Override 
public void onDestroy() 
{
    locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) 
{

    if(old_latitude == 0.0 && old_longitude == 0.0)
    {
        old_latitude = location.getLatitude();
        old_longitude = location.getLongitude();
    }

    new_latitude = location.getLatitude();
    new_longitude = location.getLongitude();



    Location.distanceBetween(old_latitude, old_longitude, new_latitude, new_longitude, result);

    distance = final_distance +result[0];//distance in meters between two locations 

    final_distance = distance;

    old_latitude = new_latitude;
    old_longitude = new_longitude;
}
@Override
public void onProviderDisabled(String provider) 
{

}
@Override
public void onProviderEnabled(String provider) 
{

}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) 
{

} 

并在android manifest.xml中添加使用权限android.permission.ACCESS_FINE_LOCATION

希望这可以帮到你