android – 在导航应用程序中使用模拟器位置

前端之家收集整理的这篇文章主要介绍了android – 在导航应用程序中使用模拟器位置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试开发一个通过Google导航应用程序模拟路由的应用程序.我在本网站的其他一些帖子( Android mock location on device?)中发现了如何实现模拟位置提供商的一些很好的例子.
通过对源代码 http://www.cowlumbus.nl/forum/MockGpsProvider.zip的简单修改,我的模拟位置显示在Google的Google Maps应用程序中. (唯一的更改是位置管理器GPS_PROVIDER的模拟提供者名称).
我的问题是当我打开导航应用程序时,它显示搜索GPS信号”.我仍然看到我的位置在地图上移动;但是,它不会生成到目的地的路由.我想知道有没有人知道我需要做什么来伪造导航,看到我的模拟位置作为GPS信号.
谢谢.
  1. public class MockGpsProviderActivity extends Activity implements LocationListener {
  2.  
  3. private MockGpsProvider mMockGpsProviderTask = null;
  4. private Integer mMockGpsProviderIndex = 0;
  5.  
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10.  
  11. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  12. String mocLocationProvider = LocationManager.GPS_PROVIDER;
  13. locationManager.addTestProvider(mocLocationProvider,false,true,5);
  14. locationManager.setTestProviderEnabled(mocLocationProvider,true);
  15. locationManager.requestLocationUpdates(mocLocationProvider,this);
  16.  
  17. try {
  18.  
  19. List<String> data = new ArrayList<String>();
  20.  
  21. InputStream is = getAssets().open("test.csv");
  22. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  23.  
  24. String line = null;
  25. while ((line = reader.readLine()) != null) {
  26. data.add(line);
  27. }
  28.  
  29. // convert to a simple array so we can pass it to the AsyncTask
  30. String[] coordinates = new String[data.size()];
  31. data.toArray(coordinates);
  32.  
  33. // create new AsyncTask and pass the list of GPS coordinates
  34. mMockGpsProviderTask = new MockGpsProvider();
  35. mMockGpsProviderTask.execute(coordinates);
  36. }
  37. catch (Exception e) {}
  38. }
  39.  
  40. @Override
  41. public void onDestroy() {
  42. super.onDestroy();
  43.  
  44. // stop the mock GPS provider by calling the 'cancel(true)' method
  45. try {
  46. mMockGpsProviderTask.cancel(true);
  47. mMockGpsProviderTask = null;
  48. }
  49. catch (Exception e) {}
  50.  
  51. // remove it from the location manager
  52. try {
  53. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  54. locationManager.removeTestProvider(MockGpsProvider.GPS_MOCK_PROVIDER);
  55. }
  56. catch (Exception e) {}
  57. }
  58.  
  59. @Override
  60. public void onLocationChanged(Location location) {
  61. // show the received location in the view
  62. TextView view = (TextView) findViewById(R.id.text);
  63. view.setText( "index:" + mMockGpsProviderIndex
  64. + "\nlongitude:" + location.getLongitude()
  65. + "\nlatitude:" + location.getLatitude()
  66. + "\naltitude:" + location.getAltitude() );
  67. }
  68.  
  69. @Override
  70. public void onProviderDisabled(String provider) {
  71. // TODO Auto-generated method stub
  72. }
  73.  
  74.  
  75. @Override
  76. public void onProviderEnabled(String provider) {
  77. // TODO Auto-generated method stub
  78. }
  79.  
  80.  
  81. @Override
  82. public void onStatusChanged(String provider,int status,Bundle extras) {
  83. // TODO Auto-generated method stub
  84. }
  85.  
  86.  
  87. /** Define a mock GPS provider as an asynchronous task of this Activity. */
  88. private class MockGpsProvider extends AsyncTask<String,Integer,Void> {
  89. public static final String LOG_TAG = "GpsMockProvider";
  90. public static final String GPS_MOCK_PROVIDER = "GpsMockProvider";
  91.  
  92. /** Keeps track of the currently processed coordinate. */
  93. public Integer index = 0;
  94.  
  95. @Override
  96. protected Void doInBackground(String... data) {
  97. // process data
  98. for (String str : data) {
  99. // skip data if needed (see the Activity's savedInstanceState functionality)
  100. if(index < mMockGpsProviderIndex) {
  101. index++;
  102. continue;
  103. }
  104.  
  105. // let UI Thread know which coordinate we are processing
  106. publishProgress(index);
  107.  
  108. // retrieve data from the current line of text
  109. Double latitude = null;
  110. Double longitude = null;
  111. Double altitude= null;
  112. try {
  113. String[] parts = str.split(",");
  114. latitude = Double.valueOf(parts[0]);
  115. longitude = Double.valueOf(parts[1]);
  116. altitude = Double.valueOf(parts[2]);
  117. }
  118. catch(NullPointerException e) { break; } // no data available
  119. catch(Exception e) { continue; } // empty or invalid line
  120.  
  121. // translate to actual GPS location
  122. Location location = new Location(LocationManager.GPS_PROVIDER);
  123. location.setLatitude(latitude);
  124. location.setLongitude(longitude);
  125. location.setAltitude(altitude);
  126. location.setTime(System.currentTimeMillis());
  127.  
  128. // show debug message in log
  129. Log.d(LOG_TAG,location.toString());
  130.  
  131. // provide the new location
  132. LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
  133. locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER,location);
  134.  
  135. // sleep for a while before providing next location
  136. try {
  137. Thread.sleep(200);
  138.  
  139. // gracefully handle Thread interruption (important!)
  140. if(Thread.currentThread().isInterrupted())
  141. throw new InterruptedException("");
  142. } catch (InterruptedException e) {
  143. break;
  144. }
  145.  
  146. // keep track of processed locations
  147. index++;
  148. }
  149.  
  150. return null;
  151. }
  152.  
  153. @Override
  154. protected void onProgressUpdate(Integer... values) {
  155. Log.d(LOG_TAG,"onProgressUpdate():"+values[0]);
  156. mMockGpsProviderIndex = values[0];
  157. }
  158. }
  159. }

解决方法

这是我所缺少的:
  1. location.setLatitude(latitude);
  2. location.setLongitude(longitude);
  3. location.setAccuracy(16F);
  4. location.setAltitude(0D);
  5. location.setTime(System.currentTimeMillis());
  6. location.setBearing(0F);

此外,重要的是计算导航轴承.否则,路由更新将不准确.

猜你在找的Android相关文章