set Gps Parameters - Android Hardware

Android examples for Hardware:Gps

Description

set Gps Parameters

Demo Code

/*/*from w  w w . j  av a2  s  .co  m*/
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import android.hardware.Camera.Parameters;
import android.location.Location;
import android.util.Log;

public class Main {
  private static final String TAG = "Util";

  public static void setGpsParameters(Parameters parameters, Location loc) {
    // Clear previous GPS location from the parameters.
    parameters.removeGpsData();

    // We always encode GpsTimeStamp
    parameters.setGpsTimestamp(System.currentTimeMillis() / 1000);

    // Set GPS location.
    if (loc != null) {
      double lat = loc.getLatitude();
      double lon = loc.getLongitude();
      boolean hasLatLon = (lat != 0.0d) || (lon != 0.0d);

      if (hasLatLon) {
        Log.d(TAG, "Set gps location");
        parameters.setGpsLatitude(lat);
        parameters.setGpsLongitude(lon);
        parameters.setGpsProcessingMethod(loc.getProvider().toUpperCase());
        if (loc.hasAltitude()) {
          parameters.setGpsAltitude(loc.getAltitude());
        } else {
          // for NETWORK_PROVIDER location provider, we may have
          // no altitude information, but the driver needs it, so
          // we fake one.
          parameters.setGpsAltitude(0);
        }
        if (loc.getTime() != 0) {
          // Location.getTime() is UTC in milliseconds.
          // gps-timestamp is UTC in seconds.
          long utcTimeSeconds = loc.getTime() / 1000;
          parameters.setGpsTimestamp(utcTimeSeconds);
        }
      } else {
        loc = null;
      }
    }
  }
}

Related Tutorials