简体   繁体   English

有没有办法使用Android模拟器和“地理定位”来传递当前速度数据以及纬度和经度?

[英]Is there a way to pass current speed data along with latitude and longitude using the Android emulator and 'geo fix'?

I'm currently building an Android app that uses the Android emulator to pass GPS location data, specifically the latitude , longitude , and speed . 我目前正在构建一个Android应用,该应用使用Android模拟器传递GPS位置数据,尤其是latitudelongitudespeed I have used a real device and everything is okay, but I now need to do so using only an emulator. 我使用了真实的设备,一切正常,但是现在我只需要使用模拟器即可。

The getSpeed method of the Location class is used to get the speed information. Location类的getSpeed方法用于获取speed信息。 The speed is reported in meters/second. speed以米/秒为单位报告。 The Location class also provides latitude and longitude using getLatitude and getLongitude , respectively. Location类还分别使用getLatitudegetLongitude提供latitudelongitude

Using geo fix longitude latitude when "telnet"-ing into the emulator only allows you to pass latitude and longitude as parameters in the following format geo fix <longitude> <latitude> . 在通过“ telnet”进入仿真器时,仅使用geo fix longitude latitude可以将latitudelongitude作为参数传递为以下格式的geo fix <longitude> <latitude>

If I were to pass speed from the command line is there a way to do this? 如果我要从命令行传递速度,有没有办法做到这一点? I have already read into using the Ripple extention for the Chrome browser. 我已经读过将Ripple扩展用于Chrome浏览器的内容。

Thank you. 谢谢。

Regarding the units... Beware! 关于单位...当心! On my Android 2.2 phone, Location.getSpeed() returns the speed in KNOTS , not m/s as stated in the Android developer documentation :( 在我的Android 2.2手机上,Location.getSpeed()以KNOTS返回速度,而不是Android开发人员文档中所述的m / s:

There are a couple of other posts on the Net about this. 网络上还有其他一些与此相关的帖子。

In my Android app, I have a class to record NMEA data to a file. 在我的Android应用中,我有一个用于将NMEA数据记录到文件中的类。

package log.nmea;

import android.location.GpsStatus;
import android.location.LocationManager;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * NMEA logging... just write NMEA stuff to file on SD card
 *
 * @author Frank van der Hulst
 */
public class NmeaLog {

  private static BufferedWriter logWriter = null;
  private static FileWriter fstream = null;
  private static File logFile;
  private static LocationManager locationManager = null;
  // Default pattern is to log any message starting with $GP
  private static final Pattern pattern = Pattern.compile("^$GP\\w+,");
  private static final String LOGNAME = "NmeaLog";
  private static GpsStatus.NmeaListener nmeaListener = new GpsStatus.NmeaListener() {
    public void onNmeaReceived(long timestamp, String nmea) {
      Log.d(LOGNAME, "onNmeaReceived(" + nmea + ")");
      if (logWriter == null)
        return;
      if (!pattern.matcher(nmea).matches())
        return;
      Log.d(LOGNAME, "onNmeaReceived appending");
      try {
        logWriter.append(timestamp + "," + nmea);
      } catch (IOException ex) {
        Log.e(LOGNAME, ex.getMessage());
      }
    }
  };

  /**
   * Initialise NMEA log file & open for writing
   *
   * @param filepath
   * @param append
   * @param lm
   */
  public static void open(String filepath, boolean append, LocationManager lm) {
    if (logWriter != null)
      return;          // Already open

    // Check SD card is available
    String SDCardStatus = Environment.getExternalStorageState();
    if (!SDCardStatus.equals(Environment.MEDIA_MOUNTED)) {
      Log.e(LOGNAME, "SD card error: " + SDCardStatus);
      return;
    }
    // SD card is OK... try to create/open file
    logFile = new File(filepath);
    if (logFile == null || logFile.isDirectory()) {
      Log.e(LOGNAME, filepath + " cannot be opened");
      return;
    }

    locationManager = lm;
    if (!locationManager.addNmeaListener(nmeaListener)) {
      Log.e(LOGNAME, "Failed to add NMEA listener");
      return;
    }
    Log.d(LOGNAME, "opening " + filepath);
    try {
      fstream = new FileWriter(logFile, append);
      logWriter = new BufferedWriter(fstream, 1024);
    } catch (Exception ex) {
      Log.e(LOGNAME, "Open failed: " + ex.getMessage());
      return;
    }
    Log.d(LOGNAME, "NMEA listener added successfully");
  }

  /**
   * Close log file. The file can be reopened, so don't null locationManager
   */
  public static void close() {
    Log.d(LOGNAME, "close()");
    try {
      locationManager.removeNmeaListener(nmeaListener);
      logWriter.close();
    } catch (Exception ex) {
      Log.e(LOGNAME, "Close error: " + ex.getMessage());
    }
    logWriter = null;
  }
}

Below is a regular Java class which plays these files to the emulator. 下面是一个常规Java类,它将这些文件播放到模拟器。 Very useful when debugging GPS apps. 在调试GPS应用程序时非常有用。

  package sendgps;

  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.net.Socket;
  import java.net.UnknownHostException;
  import java.util.Date;
  import util.Log;
  import util.OSSpecific;
  import util.Util;

  /**
   *
   * @author Frank van der Hulst
   */
  public class SendGPS {

      private static Socket socket;
      private static OutputStreamWriter w;
      private static BufferedReader r;
      private static final String NMEA_dir = "/home/frank/NetBeansProjects/SendGPS/";

    @SuppressWarnings("UseOfSystemOutOrSystemErr")
      private static void write(String s) throws IOException, InterruptedException {
  //        System.out.println("writing '" + s + "'");
          w.append(s).append("\r\n");
          w.flush();
          String result = r.readLine();
          if (!result.equals("OK")) {
              System.out.println(s + " -> " + result);
          }
      }

    @SuppressWarnings("UseOfSystemOutOrSystemErr")
      private static void read() throws IOException {
          while (r.ready()) {
              System.out.print((char) r.read());
          }

      }

    @SuppressWarnings({"SleepWhileInLoop", "UseOfSystemOutOrSystemErr", "deprecation"})
      public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
          socket = new Socket("localhost", 5554);
          final String filename = NMEA_dir + "nmea.log";
          socket.setKeepAlive(true);
          r = new BufferedReader(new InputStreamReader(socket.getInputStream()));
          w = new OutputStreamWriter(socket.getOutputStream());
          Thread.sleep(100);
          read();          // Remove initial prompt

          String[] lines = Util.getFileContents(filename);
          long prevTimestamp = 0;
          for (int line_num = 0; line_num < lines.length; line_num++) {
              String[] parts = lines[line_num].split(",", 2); // Separate timestamp from NMEA string
              if (parts[1].isEmpty()) {
                  continue;
              }
              long timestamp = Long.parseLong(parts[0]);
  //            System.out.println(timestamp + ", "+ prevTimestamp);
              if (timestamp != prevTimestamp) {
                  if (prevTimestamp != 0) {
                      int interval = (int) (timestamp - prevTimestamp);
   //                   interval /= 10;
  //                    System.out.print("Sleep: " + interval);
                      if (interval > 50) {
                          Thread.sleep(interval - 50);
                      }
                  }
                  prevTimestamp = timestamp;
              }
              if (parts[1].startsWith("$GPGGA") || parts[1].startsWith("$GPRMC")) {
                  // Replace hhmmss.ss timestamp at field 1 with current time
                  Date now = new Date();
                  String[] data = parts[1].split(",", 3);
                  parts[1] = data[0] + "," + now.getHours() + "" + now.getMinutes() + "" + now.getSeconds() + "," + data[2];
              }
              write("geo nmea " + parts[1]);
              System.out.println(parts[1]);
          }
          socket.close();
      }
  }

You have to use the geo nmea command via telnet: 您必须通过telnet使用geo nmea命令:

geo nmea $GPRMC,081836,A,3751.65,S,14507.36,E,000.0,360.0,130998,011.3,E*62

The definition of the format is here . 格式的定义在这里 In this example, 000.0 is the ground speed in knots. 在此示例中,000.0是以节为单位的地面速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android工具(DDMS和仿真器“ geo fix”)为什么在纬度之前需要经度,是否有技术上的原因? - Is there a technical reason why Android tools (DDMS, and emulator “geo fix”) expect longitude before latitude? 如何使用 Android mapview 在模拟器中获取当前位置(纬度和经度)? - How to get current location (latitude and longitude) in emulator using Android mapview? 在Android中使用gps获取当前经度和纬度 - get current longitude and latitude using gps in android 如何在Android模拟器中使用经度和纬度获取完整的地方地址? - how to get complete place address using latitude and longitude on emulator in android? geo:latitude,longitude?z =在Android中缩放将我带到当前位置 - geo:latitude,longitude?z=zoom in android taking me to my current location Android当前位置的经纬度? - latitude and longitude of current location in android? 获取当前的经纬度android - Get current latitude and longitude android 如何使用loopj概念在Android中以Json格式在本地服务器上发送当前的纬度和经度数据? - How to send current latitude and longitude data on localhost server in Json format in android using loopj concept? android在android的模拟器上获取经纬度 - android get latitude and longitude on emulator of android State 的显示名称以及 Android 中的纬度和经度 - Display Name of the State along with Latitude and Longitude in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM