假模拟位置不会在谷歌地图上改变




我对android应用程序的创建非常陌生,所以我有一个问题:

你能告诉我为什么真正的android设备不能识别从jSON中获取的mock(假)位置吗?

功能:

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());

函数和发送数据到函数是有效的,因为它用Toast.makeText打印出来又长又晚,但问题是谷歌地图,谷歌导航器不会看到新的位置,设置是真实的。它使用网络位置代替…

我试过在没有GPS的设备上运行这个Android 4.0.3Android 4.2.2

提前感谢!

我想我明白你的意思了,对于你的这个具体问题,我是这样做的:
您必须创建一个类来扩展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);
                }
        }
}

现在你在google地图中所要做的就是实例化这个类,并尝试使用我给你的提供你想要的位置。
您可以在以下地址找到有关激活/取消激活的更多详细信息:
http://developer.android.com/reference/com/google/android/gms/maps/LocationSource.html
此外,你可以看看commonswguy:
https://github.com/commonsguy/cw-omnibus/tree/master/MapsV2/Location

最新更新