android:如何使用模拟(假)坐标修复 gps?

android: how to fix gps with mock(fake) coordinates?(android:如何使用模拟(假)坐标修复 gps?)

本文介绍了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?

基础教程推荐