1

您好,请看下面我的代码。

        public  void onClick(View arg0)  {      
                // create class object
                gps = new GPSTracker(MainActivity.this);

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

                    double latitude = gps.getLatitude();
                    double longitude = gps.getLongitude();

                     String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());

                    // \n is for new line

                    Toast.makeText(getApplicationContext(), "Time : " +mydate + " Your Location is - \nLat: " 
                    + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show(); 

                }

在 GPSTracker 课程中:

public double getLatitude(){
    if(location != null){
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}

/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

我想将纬度和经度保存到数组中。

就像单击 btnShowLocation 按钮时,纬度将设置为 n1x,其经度设置为 n1y。

然后,第二次单击该按钮时,纬度将设置为 n2x,其经度设置为 n2y。

4

2 回答 2

2

您可以创建一个名为 LatLong 的内部类或使用simplelatlng

public class LatLong {
  double lat;
  double long;

  public LatLong(double lat, double long) {
    this.lat = lat;
    this.long = long;
  }

  public double getLat(){
    return this.lat;
  }

  public double getLong(){
    return this.long;
  }
}

然后你可以将它添加到MainActivity

final List<LatLong> latLongList = new ArrayList<LatLong>();

public void onClick(View arg0) {
  //...
  double latitude = gps.getLatitude();
  double longitude = gps.getLongitude();
  latLongList.add(new LatLong(latitude, longitude));
  //...
}
于 2013-07-08T17:14:22.660 回答
1

如果要一次保存一个坐标单位(一个单位是纬度和经度的组合),请创建一个保存坐标的类:

public class Coordinates {
    public double Latitude;
    public double Longitude;
}

在您的活动中,创建一个包含坐标类型元素的ArrayListList :

private ArrayList<Coordinates> coordinates = new ArrayList<Coordinates>();

在每个按钮上单击一个坐标类型的新元素到您的 ArrayList:

var cc = new Coordinates();
cc.Latitude = lat;
cc.Longitude = lon;
this.coordinates.add(cc);

稍后在您需要坐标的某个时候,只需使用getremove获取它们并使用坐标:

Coordinates cc = (Coordinates)this.coordinates.get(position);
// cc.Latitude ...

这样你就有了一个灵活的数组。

于 2013-07-08T17:24:40.583 回答