2013-07-08 3612 views
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。

+3

只发布您的代码的相关部分。 – arshajii

+0

@arshajii你告诉他,我甚至不打扰这样的代码。 –

+0

你的应用程序的其余部分在哪里? –

回答

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)); 
    //... 
} 
+0

打我10分钟,我想我需要写得更快;-) – pasty

+0

我不能做这个方法,它包含错误,因为我的getLatitude()是不同的 – doraemon

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 ... 

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