39

我是新的 Android 编码员,我在请求本地化更新时遇到问题。

我使用来自http://developer.android.com/training/location/receive-location-updates.html的教程。

我的应用程序可以处理异常,正确获取纬度和经度,地理编码器可以处理显示地址。但我只要求位置一次 - 或者当位置发生变化时。我想做时间间隔。现在我开始实现教程中的代码,它看起来像这样:

public class MainActivity extends Activity implements 
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener,
LocationListener {

private static final int MILLISECONDS_PER_SECOND = 1000;

public static final int UPDATE_INTERVAL_IN_SECONDS = 5;
private static final long UPDATE_INTERVAL =
          MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS;

private static final int FASTEST_INTERVAL_IN_SECONDS = 1;
private static final long FASTEST_INTERVAL =
          MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS;

private TextView tvStatus;
private TextView tvLatitude;
private TextView tvLongitude;

LocationRequest mLocationRequest;
LocationClient mLocationClient;
Location mCurrentLocation;

boolean bNetworkEnabled;
boolean bGPSEnabled;

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

    setContentView(R.layout.activity_main);
    tvStatus = (TextView)findViewById(R.id.tvStatus);
    tvLatitude = (TextView)findViewById(R.id.tvLatitude);
    tvLongitude = (TextView)findViewById(R.id.tvLongitude);

    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationClient = new LocationClient(this, this, this);

    checkProviders();
}

因此,已经实施了间隔和位置请求。但是在我之前给出的链接中,有一条我应该在某处使用的评论requestLocationUpdates()(可能onCreate()onStart()并在 上删除请求onStop()),但我对此有疑问。因此,Eclipse 向我展示了 3 种方法:

requestLocationUpdates(LocationRequest request, LocationListener listener)
requestLocationUpdates(LocationRequest request, PendingIntent CallbackIntent)
requestLocationUpdates(LocationRequest request, LocationListener listener, Looper looper)

所以我认为第一个在这个地方是最正确的。我应该在LocationListener插槽中放置什么?我寻求帮助,几乎没有解释它是如何工作的。

4

2 回答 2

38

您正在活动 MainActivity 中实现 LocationListener。因此,并发位置更新的调用将是这样的:

mLocationClient.requestLocationUpdates(mLocationRequest, this);

确保您正在实施的 LocationListener 来自 google api,即导入:

import com.google.android.gms.location.LocationListener;

而不是这个:

import android.location.LocationListener;

它应该可以正常工作。

在您执行此操作之前,LocationClient 确实已连接也很重要。我建议你不要在 onCreate 或 onStart 方法中调用它,而是在 onResume 中调用它。在 Google Location Api 的教程中,这一切都得到了很好的解释:https ://developer.android.com/training/location/index.html

于 2013-07-24T06:22:20.133 回答
13

我用这个:

LocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)

例如,使用 1s 间隔:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);

时间以毫秒为单位,距离以米为单位。

这会自动调用:

public void onLocationChanged(Location location) {
    //Code here, location.getAccuracy(), location.getLongitude() etc...
}

我也将这些包含在脚本中,但实际上并没有使用它们:

public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}

简而言之:

public class GPSClass implements LocationListener {

    public void onLocationChanged(Location location) {
        // Called when a new location is found by the network location provider.
        Log.i("Message: ","Location changed, " + location.getAccuracy() + " , " + location.getLatitude()+ "," + location.getLongitude());
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}
    public void onProviderEnabled(String provider) {}
    public void onProviderDisabled(String provider) {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,1000,0,this);
    }
}
于 2013-06-03T16:30:48.653 回答