开发者

Fake location using GPS in android

开发者 https://www.devze.com 2023-01-18 02:16 出处:网络
Hi am developing an app that will set the coordinates(latitude and longitude). And it has to show my location as i am at that coordinates..It is similar to location spoofer.. http://www.androidzoom.co

Hi am developing an app that will set the coordinates(latitude and longitude). And it has to show my location as i am at that coordinates..It is similar to location spoofer.. http://www.androidzoom.com/android_applications/tools/location-spoofer_gkmc.html But I am failing to do that.. here is my code..Please any one help me.

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 开发者_C百科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
    }
}    
}


I would use this method http://developer.android.com/guide/developing/tools/ddms.html#emulator-control. Haven't tried this one: http://code.google.com/p/android/issues/detail?id=915


You basically set the coordinates on real device in the same way you do in the emulator by using DDMS in Eclipse.

Just make sure you enable "fake locations" in your device. You can find the setting at Settings > Applications > Development > Allow mock locations.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号