我对Android应用创建非常熟悉,所以我有一个问题:
你能否告诉我为什么真正的Android设备无法识别从jSON中获取的模拟(假)位置?
功能:
public void setLocation(double latitude, double longitude,Context ctx) {
LocationManager mLocationManager = (LocationManager) ctx.getSystemService(Service.LOCATION_SERVICE);
if (mLocationManager.getProvider("spoof") == null) {
mLocationManager.addTestProvider("spoof", false, true, false, false, false, false, false, 0, 5);
mLocationManager.setTestProviderEnabled("spoof", true);
}
Location loc = new Location("spoof");
loc.setLatitude(latitude);
loc.setLongitude(longitude);
loc.setTime(System.currentTimeMillis());
loc.setAccuracy(16F);
loc.setAltitude(0D);
loc.setBearing(0F);
mLocationManager.setTestProviderLocation("spoof", loc);
Toast.makeText(getBaseContext(), "Lat:"+latitude+" - Lng:"+longitude, Toast.LENGTH_LONG).show();
}
所以现在我有double
纬度和经度,它们被设置为lata
,longa
以下是运行函数的代码
setLocation(lata,longa, getBaseContext());
功能和发送数据到功能有效,因为它打印出lat和long Toast.makeText
,但问题是谷歌地图,谷歌浏览器不会看到新的位置和设置是真正的..它使用网络位置而不是..
我试图在没有GPS的设备上运行此功能 - Android 4.0.3 - Android 4.2.2
提前谢谢!
答案 0 :(得分:0)
我想我明白了,我为你的这个具体问题所做的是以下几点:
您必须创建一个扩展LocationSource的类,如下例所示:
public class MyLocationSource implements LocationSource{
//either save it or add it to a list
//adivsable would be to create one instance for each listener not an array.
OnLocationChangedListener listener;
public MyLocationSource () {
}
//Google maps somehow extends this OnLocationChangedListener
@Override
public void activate(OnLocationChangedListener mListener) {
this.listener = mListener;
//initially u save the listener
//here u can make a mix between what the GPS delivers and your manual loc
}
@Override
public void deactivate() {
//here u can stop the updates
listener = null;
}
protected void fireUpdate(Location loc) {
if (listener != null && loc != null) {
listener.onLocationChanged(loc);
}
}
}
现在,你在谷歌地图中所要做的就是实例化这个类,并尝试使用我给你的东西提供你想要的位置。
有关激活/取消激活的更多详细信息,请访问以下地址:
http://developer.android.com/reference/com/google/android/gms/maps/LocationSource.html
另外你可以看看commonswguy:
https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Location