Domanda

Here is my Player class

public class Player {
private double xCor;
private double yCor;

public Player(double xCor, double yCor)
{
    this.xCor = xCor;
    this.yCor = yCor;
}


public void setX(double latitude)
{
    this.xCor = latitude;
}

public void setY(double longitude)
{
    this.yCor = longitude;
}

public double getX()
{
    return xCor;

}

public double getY()
{
    return xCor;
}



}

Here is my GameSurface

public class GameSurface extends View implements Runnable{
private ArrayList<Ghost> ghostList;
private Paint paint;
private Player pl;
private LocationManager lm;
private MyLocationListener ml;

public GameSurface(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
    pl = new Player(300,300);
    lm = (LocationManager)     context.getSystemService((Context.LOCATION_SERVICE));
    ml = new MyLocationListener();
}

public GameSurface(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
    pl = new Player(300,300);
    lm = (LocationManager) context.getSystemService((Context.LOCATION_SERVICE));
    ml = new MyLocationListener();
}

public GameSurface(Context context) {
    super(context);
    init();
    pl = new Player(300,300);
    lm = (LocationManager) context.getSystemService((Context.LOCATION_SERVICE));
    ml = new MyLocationListener();

}

public void init() {
    paint = new Paint();
    paint.setColor(0xff00ff00);
    paint.setStyle(Style.STROKE);
    paint.setStrokeWidth(3);
}

public Player getPlayer()
{
    return pl;
}

@Override
public void onDraw(Canvas c) {
    super.onDraw(c);
    c.drawCircle((float) pl.getX(), (float) pl.getY(),20,paint);
    postDelayed(this, 16);
}


@Override
public void run() {
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, ml);
    pl.setX(ml.latitude*10);
    pl.setY(ml.longitude*10);
    invalidate();
}


}

And here is MyLocationListener

public class MyLocationListener implements LocationListener {
public double latitude;
public double longitude;

@Override
public void onLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
}

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

}

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

}

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

}

}

When I hold my device and try to move physically, the circle would not move accordingly. Is there anything wrong with my implementation of LocationListener?

È stato utile?

Soluzione

You should only be registering for location updates once. Try moving lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0, ml); to the last line of your constructor methods so it's executed only on startup.

Also, be aware that you may need to walk around outside for a bit for a GPS location to be returned, depending on your device.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top