android: how to fix gps with mock(fake) coordinates?(android:如何使用模拟(假)坐标修复 gps?)
问题描述
当 GPS 信号不可用时,我正在尝试以编程方式修复 GPS.我想这样做是为了向谷歌地图等其他应用程序提供模拟坐标.我尝试使用 GPS_PROVIDER 和 NETWORK_PROVIDER,但只有使用后者我才能获得对谷歌地图有效的新位置.但这仅适用于第一个 tima,如果我想重新修复更多模拟位置,谷歌地图没有看到任何变化......为什么?我必须为我想做的事做服务?
I'm trying to fix GPS programmatically when the GPS signal is not available. I want to do this in order to provide mock coordinates to other apps like google maps and others. I tried to use GPS_PROVIDER and NETWORK_PROVIDER but only with the latter I can get a new location valid for google maps. But this works only the first tima and if I want to re-fix more mock locations google maps doesn't see any change... why?? I have to do a service for what I want to do?
public void setLocation(double latitude, double longitude) {
//mocLocationProvider = LocationManager.GPS_PROVIDER;
mocLocationProvider = LocationManager.NETWORK_PROVIDER;
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.clearTestProviderEnabled(mocLocationProvider);
lm.requestLocationUpdates(mocLocationProvider,0,0,new llistener());
lm.addTestProvider(mocLocationProvider, false, false, false, false, false, false, false, 0, 10);
lm.setTestProviderEnabled(mocLocationProvider, true);
Location loc = new Location(mocLocationProvider);
loc.setTime(System.currentTimeMillis());
loc.setLatitude(latitude);
loc.setLongitude(longitude);
loc.setAccuracy(10);
lm.setTestProviderStatus(mocLocationProvider, LocationProvider.AVAILABLE,null, System.currentTimeMillis());
lm.setTestProviderLocation(mocLocationProvider, loc);
}
和位置监听器
public class llistener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
}
@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) {
}
}
谢谢
推荐答案
模拟位置需要什么:
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION">
同样在安卓手机设置中确保你勾选了允许模拟位置"复选框
Also in the android phone settings make sure you have the "Allow mock locations" checkbox ticked
locationManager.addTestProvider(mocLocationProvider, false, false,
false, false, true, true, true, 0, 5);
locationManager.setTestProviderEnabled(mocLocationProvider, true);
.现在每当你想发送一个位置,调用这个代码:
. Now whenever you want to send a location, call this code:
Location mockLocation = new Location(mocLocationProvider); // a string
mockLocation.setLatitude(location.getLatitude()); // double
mockLocation.setLongitude(location.getLongitude());
mockLocation.setAltitude(location.getAltitude());
mockLocation.setTime(System.currentTimeMillis());
locationManager.setTestProviderLocation( mocLocationProvider, mockLocation);
.
但这只适用于第一次
因为你曾经调用过它.
我必须为我想做的事做服务
I have to do a service for what I want to do
你可以这样做,即使是 AsyncTask 也可以
You can do this, even an AsyncTask will do
执行与否:lm.requestLocationUpdates()
do this or not: lm.requestLocationUpdates()
是的,您必须使用 mocLocationProvider
我已经多次调用 setLocation(latitude,longitude) 方法,但它只在谷歌地图上第一次起作用(位置更新)
I already call more times the method setLocation(latitude,longitude) but it works(location updateding) only first time for google maps
没有调用setLocation()
的方法,使用setTestLocation()
There is no method called setLocation()
, use setTestLocation()
实际上我必须在 asynckTask 中做什么
What actually I have to do in asynckTask
您可以使用线程、TimerTask 或任何您喜欢的东西.这个想法是每秒将位置注入框架.
You can use a Thread, a TimerTask or anything you like. The idea is to inject location every second into the Framework.
这篇关于android:如何使用模拟(假)坐标修复 gps?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:android:如何使用模拟(假)坐标修复 gps?
基础教程推荐
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01