从LocationManager和LocationRequest获取位置有什么区别

时间:2014-11-13 08:01:35

标签: android gps locationmanager

当我搜索获取位置时,我得到的结果是通过位置管理器获取位置,但在教程中我看到他们使用LocationRequest获取用户位置,我已设法创建一个基于时间给出位置的服务

import android.app.Service;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.os.IBinder;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.model.LatLng;

public class LocationTrackerService extends Service implements
        GooglePlayServicesClient.ConnectionCallbacks,
        GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

    private LocationClient nLocationClient;
    public static boolean serviceStopCheck = true;
    private DBHelper nDbHelper;
    private int timeForService = 60000;

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

//  @Override
//  public int onStartCommand(Intent intent, int flags, int startId) {
//      timeForService=Integer.valueOf(intent.getStringExtra("TIME_FOR_SERVICE"))*1000;
//      return super.onStartCommand(intent, flags, startId);
//  }

    @Override
    public void onDestroy() {
        super.onDestroy();
        serviceStopCheck = true;
    }

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

        nDbHelper = new DBHelper(this);
        nLocationClient = new LocationClient(this, this, this);
        nLocationClient.connect();
        serviceStopCheck = false;
    }

    @Override
    public void onConnectionFailed(ConnectionResult arg0) {

    }

    @Override
    public void onConnected(Bundle arg0) {
        LocationRequest request = LocationRequest.create();
        request.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        request.setInterval(timeForService);
        request.setFastestInterval(timeForService);
        nLocationClient.requestLocationUpdates(request, this);
    }

    @Override
    public void onDisconnected() {

    }

    @Override
    public void onLocationChanged(Location arg0) {
        if (!serviceStopCheck) {
            LocationItem nLocationItem = new LocationItem(new LatLng(
                    arg0.getLatitude(), arg0.getLongitude()),
                    CommonMethods.getCurentTime(),
                    CommonMethods.getCurentDate());
            try {
                nDbHelper.insertLocationItem(nLocationItem);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else {
            stopSelf();
        }
    }

}

所以更好或者我必须使用LocationManger。因为我觉得这个代码比其他人更容易使用LocationMnager或GPSTracker类提供程序。

1 个答案:

答案 0 :(得分:0)

至于我,我已经测试了这两种方法,我得出结论:LocationManager更好,因为它使用硬件GPS,它比LocationClient具有更好的准确性。 LocationClient是GooglePlay服务的一部分,所以我认为他们使用互联网坐标而不是GPS坐标。但对我来说,最好的方法是LocationManager,因为它给了我最好的准确性。