Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mock location in android

Tags:

android

I am using the following to mock my location in Android.

public class GPSwork extends Activity implements LocationListener{
LocationManager locationManager;
String mocLocationProvider;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mocLocationProvider = LocationManager.GPS_PROVIDER; 
    setLocation(28.574853,78.063201);
}

public void setLocation(double latitude, double longitude) {   
    locationManager.addTestProvider(mocLocationProvider, false, true, false, false, false, false, false, 0, 5);
    locationManager.setTestProviderEnabled(mocLocationProvider, true);
    locationManager.requestLocationUpdates(mocLocationProvider, 0, 0, this);
    Location loc = new Location(mocLocationProvider);
    loc.setTime(System.currentTimeMillis());
    loc.setLatitude(latitude);
    loc.setLongitude(longitude);
    locationManager.setTestProviderLocation(mocLocationProvider, loc);   
}

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub      
}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub      
}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub      
}   
}

But it's not working. I want to see it in real device and the bubble blinking in that location, but I didnt get anything. Please correct my code, and help me out. I used the permissions.

I want build an application similatiom to location spoofer.Actually when we open google maps it will show our current location with blue blinking at our location..similarly if i changed the coordinates i.e, latitude and latitude it has to show that am at that location with blue blinking,

![alt text][1] this is the screen short..

My XML fine is

<?xml version="1.0" encoding="utf-8"?>

<com.google.android.maps.MapView 
    android:id="@+id/mapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="0Sy8vgJlQkFxcisUAkrxu3xN33dqFgBetCg4jxg"
    />

like image 959
user455422 Avatar asked Aug 26 '26 23:08

user455422


1 Answers

This is a basic pattern for an app that uses google maps:

public class MapsActivity extends MapActivity 
{    

    MapView mapView;
MapController mc;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        mapView = (MapView)findViewById(R.id.mapView);
        //zoom controlls
        mc = mapView.getController();
        mc.setZoom(12);
        setContentView(R.layout.main);
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }
}

In your AndroidManifest.xml file add:

 <uses-library android:name="com.google.android.maps" />

and:

<uses-permission android:name="android.permission.INTERNET" />

To display your particular location add this to the onCreate method:

 String coordinates[] = {"1.352566007", "103.78921587"};
 double lat = Double.parseDouble(coordinates[0]);
 double lng = Double.parseDouble(coordinates[1]);

 p = new GeoPoint(
       (int) (lat * 1E6), 
       (int) (lng * 1E6));

 mc.animateTo(p);
 mc.setZoom(17); 
 mapView.invalidate();

Finally you need to set your marker using a map overlay. Add the following class to your MapsActivity class:

class MapOverlay extends com.google.android.maps.Overlay
    {
        @Override
        public boolean draw(Canvas canvas, MapView mapView, 
        boolean shadow, long when) 
        {
            super.draw(canvas, mapView, shadow);                   

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(
                getResources(), R.drawable.my_bubble);            
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y-50, null);         
            return true;
        }
    } 

In this draw method you can to some marker animation if you want to. Now you have to add the overlay to your map:

//---Add a location marker---
 MapOverlay mapOverlay = new MapOverlay();
 List<Overlay> listOfOverlays = mapView.getOverlays();
 listOfOverlays.clear();
 listOfOverlays.add(mapOverlay);        

 mapView.invalidate();

This is a quick overview. You will need a google api key to work with google maps api. A detailed tutorial on google maps api can be found here, Using Google Maps in Android

like image 97
UpCat Avatar answered Aug 30 '26 17:08

UpCat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!