1

我正在尝试从这里使用 MyLocation 类。在下面的代码中,我需要在实例化 MyLocation 的类中的任何位置访问变量 currentLat 和 currentLon。我不知道如何访问currentLatand的值currentLon

LocationResult locationResult = new LocationResult(){ @Override public void gotLocation(Location location){ currentLat = location.getLatitude(); currentLon = location.getLongitude(); }; } MyLocation myLocation = new MyLocation(); myLocation.getLocation(this, locationResult);

假设我想在这里

Double x =currentLoc;

我怎么得到那个?任何帮助,将不胜感激

4

2 回答 2

0

而不是匿名类使用你自己的扩展/实现 LocationResult 类/接口并像这样添加getter

    class MyLocationResult extends/implments LocationResult{
    double currentLat;
    double currentLon;

    @Override 
    public void gotLocation(Location location){ 
        currentLat = location.getLatitude(); 
        currentLon = location.getLongitude(); 
    };
    public double getCurrentLat(){
       return currentLat;
    }
    public double getCurrentLon (){
       return currentLon ;
    }
}

然后你可以写

MyLocationResult locationResult = new MyLocationResult();
MyLocation myLocation = new MyLocation(); 
myLocation.getLocation(this, locationResult);

并且每当您需要 currentLat 或 currentLon 时,您都可以编写

locationResult.getCurrentLat();
于 2015-01-14T10:29:41.353 回答
0

您可以static对变量使用修饰符并在全局范围内定义它们。

public static double currentLat; // defined globally...
public static double currentLon;

LocationResult locationResult = new LocationResult(){
   @Override
      public void  gotLocation(Location location){
      currentLat =location.getLatitude(); // assuming getLatitude() returns double or int
      currentLon = location.getLongitude();
    };
}
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);

现在您可以在任何地方访问它们

double x =currentLon;
double y =currentLat;
于 2015-01-14T10:34:23.757 回答