在Android中使用GPS的假位置

时间:2010-09-25 18:17:18

标签: android

您正在开发一款可以设置坐标(经度和纬度)的应用。它必须显示我的位置,因为我在那个坐标。它类似于位置欺骗者.. http://www.androidzoom.com/android_applications/tools/location-spoofer_gkmc.html 但我没有这样做..这是我的代码..请任何人帮助我。

public class Mock extends MapActivity 
{    
  private LocationManager lm;
  private LocationListener locationListener;

  private MapView mapView;
  String mocLocationProvider;
  private MapController mc;


  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

    //---use the LocationManager class to obtain GPS locations---
    lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);    

   locationListener = new MyLocationListener();

    mocLocationProvider=LocationManager.GPS_PROVIDER;

    lm.addTestProvider(mocLocationProvider, false, false,false, false, true, true, true, 0, 5);
    lm.setTestProviderEnabled(mocLocationProvider,true);
    lm.requestLocationUpdates(mocLocationProvider,0,0,locationListener);


    mapView = (MapView) findViewById(R.id.mapview1);
    mc = mapView.getController();

  }

  @Override
  protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
  }        

  private class MyLocationListener implements LocationListener 
  {
    @Override
    public void onLocationChanged(Location loc) {

        loc = new Location(mocLocationProvider);

         Double latitude = 1.352566007;
         Double longitude = 103.78921587;

         //Double altitude = Double.valueOf(parts[2]);

         loc.setLatitude(latitude);
         loc.setLongitude(longitude);
         loc.setTime(System.currentTimeMillis());
         lm.setTestProviderLocation(mocLocationProvider, loc);


            mc.setZoom(16);                
            mapView.invalidate();

    }

    @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) {
        // TODO Auto-generated method stub
    }
}    
}

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分:0)

通过在Eclipse中使用DDMS,您基本上可以像在模拟器中一样设置真实设备上的坐标。

只需确保在您的设备中启用“假位置”即可。您可以在设置>中找到该设置。应用程序>开发>允许模拟位置