tags:

views:

90

answers:

3

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"
    />

A: 

Here is how to handle gps in android. It is a bad solution to make your main activity listen to location updates. Use and modify the following class:

public MyGPS implements LocationListener{

    public LocationManager lm = null;
    private MainActivity SystemService = null;
    //lat, lng
    private double mLongitude = 0;
    private double mLatitude = 0;

    public MyGPS(MainActivity sservice){
        this.SystemService = sservice;
        this.startLocationService();
    }

    public void startLocationService(){
        this.lm = (LocationManager) this.SystemService.getSystemService(Context.LOCATION_SERVICE);
        this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 5, this);
    }

    public void onLocationChanged(Location location) {
        location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        try {
            this.mLongitude = location.getLongitude();
            this.mLatitude = location.getLatitude();
        } catch (NullPointerException e) {
            Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
        }
    }
}

In your onCreate method make an instance of this class and the locationlistener will start to listen for gps updates. But you can not access lng and lat since you do not know from your activity wheather they are set or null. So you need a handler that sends a message to your main activity:

Modify in the following method:

public void onLocationChanged(Location location) {
        location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        try {
            this.mLongitude = location.getLongitude();
            this.mLatitude = location.getLatitude();
            Message msg = Message.obtain();
            msg.what = UPDATE_LOCATION;
            this.SystemService.myViewUpdateHandler.sendMessage(msg);
        } catch (NullPointerException e) {
            Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
        }
    }

In your main activity add this:

Handler myViewUpdateHandler = new Handler(){

        public void handleMessage(Message msg) {
                switch (msg.what) {
                case UPDATE_LOCATION:
                //access lat and lng   
         }));
               }

                super.handleMessage(msg);
        }
};
ArtWorkAD
hi thanks for ur reply...I dont want current location..I want to set longitude and latitude to get the desired location...with blue blinking bubble.Any help
then you do not need location listener at all, just some dummy data. do you want to set the location on a map?
ArtWorkAD
yes i want to set the location on map and on that location the blue blinking should appear...How can i do this ArtWork..?Please help me..Similar to location Spoofer..
ok, add to your question that you want to set map location because that is not clear
ArtWorkAD
Hi ArtWorkAD Thanks a lot for ur help..I had edited my question please go through it..Actally i will give the latitude and logitude in edit text fields as input..When i click on locate me button it has to show me that i am in that location..We know that when we open google maps it will show our current location with blue blinking bubble...But i want to show my location in the coordinates that i entered..
so whats your problem? you have two input fields for lng and lat and a map to display the entered location. If locate button is clicked you have to update the map. do you have problem displaying the dialog or what?
ArtWorkAD
@ArtWork Thanks for ur answer..i followed the steps to generate the goole map API key and i got it..but i am getting gridlines only..i am unable to display the map..?why..?i have givem permissiona and xml file everything correct..even though i am unable to display map..?
what do you mean with gridlines? can you post a screenshot? maybe you have configured your layout xml in a wrong way
ArtWorkAD
HI ArtWork i thought of pasting snapshot but as i had less than 10 reputations i cannot paste the snapshot..gridlines means instead of map it is showing only sqaure boxes and i am not getting any map..i have posted xml.
hm xml seems ok. do you have internet connection and permission?
ArtWorkAD
Yeah yeah Finally I got it ArtWork..Thanks a Million for ur help..But i had a question here friend..i have done this in emulator.But how can i do this on real device..ie.how to substitute the debug.keystore file with my own keystore file to deploy it on real device..?
just deploy the app on your device. eclipse does everything else for you. you do not need to substitute anything
ArtWorkAD
A: 

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

ArtWorkAD
A: 

I think you have to extend the MyLocationOverlay class and Override the onDraw and change the location to your desired location and then call the superclass.onDraw method.

ferdy182