在FusedLocationProviderApi

时间:2016-08-30 06:22:36

标签: android location fusedlocationproviderapi

我有以下LocationService.java服务类。

public class LocationService extends Service
        implements LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
    private static final long INTERVAL = 1000 * 60;
    private static final long FASTEST_INTERVAL = 1000 * 5;
    Location mLastLocation;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;
    String lat, lon;


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        mGoogleApiClient.connect();
        return START_STICKY;
    }

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

    @Override
    public void onConnected(Bundle bundle) {
        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
            lat = String.valueOf(mLastLocation.getLatitude());
            lon = String.valueOf(mLastLocation.getLongitude());

            Util.WriteSharePrefrence(getApplicationContext(), "track_lat", "" + lat);
            Util.WriteSharePrefrence(getApplicationContext(), "track_lng", "" + lon);

        }
    }

    @Override
    public void onCreate() {
        int permissionCheck = ContextCompat.checkSelfPermission(LocationService.this, Manifest.permission.ACCESS_FINE_LOCATION);

        if (permissionCheck == PackageManager.PERMISSION_GRANTED) {
            //Execute location service call if user has explicitly granted ACCESS_FINE_LOCATION..
            buildGoogleApiClient();
        }
    }


    @Override
    public void onConnectionSuspended(int i) {
    }

    synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

    @Override
    public void onLocationChanged(Location location) {
        if (location != null) {
            Util.WriteSharePrefrence(getApplicationContext(), "track_lat", "" + location.getLatitude());
            Util.WriteSharePrefrence(getApplicationContext(), "track_lng", "" + location.getLongitude());
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        buildGoogleApiClient();
    }

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

在上述位置服务中我正在做的事情。 1.设置最快间隔以每5秒刷新一次位置。 2.一旦有位置可用onLocationChanged()。在onLocationChanged()方法内部,我在共享首选项中编写当前纬度和经度。 3.此服务将在后台运行24小时以获取当前位置。

我在我的清单中提到了两种位置条件,如下所示:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

我的问题是:

有些时候onLocationChanged()方法的纬度经度错误 例如:印度的用户设备和美国地址。设备版本为Xolo - 4.2.1.(India)

是设备特定问题吗?或者我需要在代码中更改某些内容? 我该怎么做才能使我的位置服务更好?

1 个答案:

答案 0 :(得分:0)

如果需要,请使用网络或GPS提供商或被动

public void Request_LocationUpdate(String type)
{
    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, 12);
    }
    else
    {
      LocationManager  locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

        switch (type)
        {
            case "Passive":
                locationManager.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,10000,10,locationListener);
                break;

            case "GPS":
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,10000,10,locationListener);
                break;

            case "Network":
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10000,10,locationListener);
                break;
        }
    }


}