Tôi đang cố gắng phát triển một ứng dụng sẽ mô phỏng một tuyến đường thông qua ứng dụng Điều hướng của Google. Tôi đã tìm thấy một số ví dụ tuyệt vời về cách triển khai nhà cung cấp vị trí mô phỏng trong một số bài đăng khác trên trang web này (Android mock location on device?).
Với một sửa đổi đơn giản đối với mã nguồn http://www.cowlumbus.nl/forum/MockGpsProvider.zip, vị trí mô phỏng của tôi hiển thị trong ứng dụng Bản đồ của Google. (Thay đổi duy nhất là tên nhà cung cấp giả lập cho LocationManager.GPS_PROVIDER).
Vấn đề của tôi là khi tôi mở ứng dụng Điều hướng, nó nói "Tìm kiếm tín hiệu GPS". Tôi vẫn thấy vị trí của tôi di chuyển trên bản đồ; tuy nhiên, nó không tạo ra một con đường đến đích. Tôi đã tự hỏi nếu có ai biết những gì tôi cần phải làm để điều hướng giả vào xem vị trí giả của tôi như là tín hiệu GPS.
Cảm ơn.Sử dụng Vị trí Mô hình trong Ứng dụng Điều hướng
public class MockGpsProviderActivity extends Activity implements LocationListener {
private MockGpsProvider mMockGpsProviderTask = null;
private Integer mMockGpsProviderIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String mocLocationProvider = LocationManager.GPS_PROVIDER;
locationManager.addTestProvider(mocLocationProvider, false, false,
false, false, true, false, false, 0, 5);
locationManager.setTestProviderEnabled(mocLocationProvider, true);
locationManager.requestLocationUpdates(mocLocationProvider, 0, 0, this);
try {
List<String> data = new ArrayList<String>();
InputStream is = getAssets().open("test.csv");
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = null;
while ((line = reader.readLine()) != null) {
data.add(line);
}
// convert to a simple array so we can pass it to the AsyncTask
String[] coordinates = new String[data.size()];
data.toArray(coordinates);
// create new AsyncTask and pass the list of GPS coordinates
mMockGpsProviderTask = new MockGpsProvider();
mMockGpsProviderTask.execute(coordinates);
}
catch (Exception e) {}
}
@Override
public void onDestroy() {
super.onDestroy();
// stop the mock GPS provider by calling the 'cancel(true)' method
try {
mMockGpsProviderTask.cancel(true);
mMockGpsProviderTask = null;
}
catch (Exception e) {}
// remove it from the location manager
try {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.removeTestProvider(MockGpsProvider.GPS_MOCK_PROVIDER);
}
catch (Exception e) {}
}
@Override
public void onLocationChanged(Location location) {
// show the received location in the view
TextView view = (TextView) findViewById(R.id.text);
view.setText("index:" + mMockGpsProviderIndex
+ "\nlongitude:" + location.getLongitude()
+ "\nlatitude:" + location.getLatitude()
+ "\naltitude:" + location.getAltitude());
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
/** Define a mock GPS provider as an asynchronous task of this Activity. */
private class MockGpsProvider extends AsyncTask<String, Integer, Void> {
public static final String LOG_TAG = "GpsMockProvider";
public static final String GPS_MOCK_PROVIDER = "GpsMockProvider";
/** Keeps track of the currently processed coordinate. */
public Integer index = 0;
@Override
protected Void doInBackground(String... data) {
// process data
for (String str : data) {
// skip data if needed (see the Activity's savedInstanceState functionality)
if(index < mMockGpsProviderIndex) {
index++;
continue;
}
// let UI Thread know which coordinate we are processing
publishProgress(index);
// retrieve data from the current line of text
Double latitude = null;
Double longitude = null;
Double altitude= null;
try {
String[] parts = str.split(",");
latitude = Double.valueOf(parts[0]);
longitude = Double.valueOf(parts[1]);
altitude = Double.valueOf(parts[2]);
}
catch(NullPointerException e) { break; } // no data available
catch(Exception e) { continue; } // empty or invalid line
// translate to actual GPS location
Location location = new Location(LocationManager.GPS_PROVIDER);
location.setLatitude(latitude);
location.setLongitude(longitude);
location.setAltitude(altitude);
location.setTime(System.currentTimeMillis());
// show debug message in log
Log.d(LOG_TAG, location.toString());
// provide the new location
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.setTestProviderLocation(LocationManager.GPS_PROVIDER, location);
// sleep for a while before providing next location
try {
Thread.sleep(200);
// gracefully handle Thread interruption (important!)
if(Thread.currentThread().isInterrupted())
throw new InterruptedException("");
} catch (InterruptedException e) {
break;
}
// keep track of processed locations
index++;
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
Log.d(LOG_TAG, "onProgressUpdate():"+values[0]);
mMockGpsProviderIndex = values[0];
}
}
}
[Thông tin khác cho thiết bị JB] (http://jgrasstechtips.blogspot.com/2012/12/android-incomplete-location-object.html) – Paul
Bạn đã bao giờ nhận được Điều hướng bằng giọng nói hoạt động với Vị trí mô phỏng chưa? Các tuyến đường được tính toán lại, nhưng tôi dint nhận được bất kỳ chuyển hướng bằng giọng nói sau khi bắt đầu ứng dụng. – AndroidDev93
Rất tiếc, tôi không thấy điều này trước đó. Vâng, mọi thứ hoạt động tốt cho tôi. Hãy cho tôi biết nếu bạn có một câu hỏi cụ thể mà tôi có thể trợ giúp. – Paul