本文介绍了android:如何用模拟(假)坐标来修复GPS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图在GPS信号不可用时以编程方式修复GPS。我想这样做是为了向其他应用程序提供模拟坐标,例如谷歌地图和其他应用程序。我试图使用GPS_PROVIDER和NETWORK_PROVIDER,但只有后者我可以得到一个有效的谷歌地图的新位置。但是,这只适用于第一次提马,如果我想重新修复更多的模拟位置谷歌地图没有看到任何改变......为什么? 我必须为我想要做的事情做一项服务? 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); 位置loc =新位置(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); $ / code $ / pre $ b $位置监听器 public class llistener implements LocationListener { @Override public void onLocationChanged(Location loc){ // TODO自动生成的方法存根} @覆盖 public void onProviderDisabled(String provider){ // TODO自动生成的方法存根} @Override public void onProviderEnabled(String provider){ // TODO自动生成方法存根} @Override public void onStatusChanged(String provider,int status, Bundle extras){} } 谢谢 解决方案需要什么样的模拟地点:$ b $ b <使用权限android:name =android.permission.ACCESS_MOCK_LOCATION> 同样在android手机设置中,请确保您勾选了允许模拟位置复选框 locationManager.addTestProvider(mocLocationProvider,false,false, false,false,true,true,true,0,5); locationManager.setTestProviderEnabled(mocLocationProvider,true); 。 现在,只要您想发送一个位置,请调用以下代码: 位置mockLocation = new Location(mocLocationProvider); //一个字符串 mockLocation.setLatitude(location.getLatitude()); // double mockLocation.setLongitude(location.getLongitude()); mockLocation.setAltitude(location.getAltitude()); mockLocation.setTime(System.currentTimeMillis()); locationManager.setTestProviderLocation(mocLocationProvider,mockLocation); 。因为你曾经这么称呼过它。 你可以做到这一点,即使是AsyncTask也可以做到这一点:lm.requestLocationUpdates() $ b 是的,您必须使用 mocLocationProvider 你可以使用Thread,TimerTask或任何你喜欢的东西。这个想法是每秒钟都在框架中注入位置。 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);}and location listener public class llistener implements LocationListener {@Overridepublic void onLocationChanged(Location loc) { // TODO Auto-generated method stub}@Overridepublic void onProviderDisabled(String provider) { // TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider) { // TODO Auto-generated method stub}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {}}thanks 解决方案 What mock locations need : <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 stringmockLocation.setLatitude(location.getLatitude()); // doublemockLocation.setLongitude(location.getLongitude());mockLocation.setAltitude(location.getAltitude());mockLocation.setTime(System.currentTimeMillis());locationManager.setTestProviderLocation( mocLocationProvider, mockLocation);.Because you called it one time. You can do this, even an AsyncTask will doYes you have to with the mocLocationProviderThere is no method called setLocation(), use setTestLocation()You can use a Thread, a TimerTask or anything you like. The idea is to inject location every second into the Framework. 这篇关于android:如何用模拟(假)坐标来修复GPS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-13 23:02