Alerta por posicionamento

2 respostas
o_Wesley_o

Senhores,

estou com uma dúvida que para muitos pode ser bem simples, mas que está me dando dor de cabeça…
tenho uma classe que recupera as coordenadas onde estou…
o que eu quero é o seguinte:
guardar um local onde eu desejo, e me mover alguns metros. E quando eu estiver por exemplo 100 metros do local o celular me dá um aviso…
se eu estou a menos de 100 metros ele dá o alerta normalmente…
mas se eu ativo ele a mais de 100 metros, quando eu chego perto do ponto, ele não dá o alerta…

segue o código:

GetLocation location;
                try {
                    location = new GetLocation();


latitudeAtual = location.lat;
longitudeAtual = location.lon;


        GeoCoordinate PontoSalvo = new GeoCoordinate(latitudeSalva, longitudeSalva);
        GeoCoordinate PontoAtual = new GeoCoordinate(latitudeAtual, longitudeAtual);

double distancia = PontoSalvo.distanceInKm(PontoAtual);

distancia = distancia * 1000;


    while(distancia>100){
        latitudeAtual = location.lat;
        longitudeAtual= location.lon;


       PontoAtual = new GeoCoordinate(latitudeAtual, longitudeAtual);


        distancia = PontoSalvo.distanceInKm(PontoAtual);
        distancia = distancia * 1000;

    }

          for(int i=0; i<=50;i++){

   AlertType.ALARM.playSound(Display.getDisplay(this));
                    }
    
 } catch (Exception ex) {
                    ex.printStackTrace();
                }

Obrigado…

2 Respostas

lazaru
olha acho que dentro de seu while deveria ter novamente o seguinte codigo...
location = new GetLocation();   
  
  
latitudeAtual = location.lat;   
longitudeAtual = location.lon;   
  
  
               GeoCoordinate PontoAtual = new GeoCoordinate(latitudeAtual, longitudeAtual);   
  
double distancia = PontoSalvo.distanceInKm(PontoAtual);   
  
distancia = distancia * 1000;
Segue tambem outro codigo de exemplo, ve se ajuda:
/*
 *The following example illustrates how to use the LocationListener for subscribing to periodic location updates. This example creates a handler thread to handle the updates   *so that the methods on the listener would not block the platform implementation threads for a long time. 
*/


class MovementTracker implements LocationListener {
    float movementDistance;
    LocationProvider provider;
    Location lastValidLocation;
    UpdateHandler handler;
    boolean done;

    public MovementTracker(float movementDistance) throws LocationException {
	this.movementDistance = movementDistance;
	done = false;
	handler = new UpdateHandler();
	new Thread(handler).start();
	provider = LocationProvider.getInstance(null);
	provider.setLocationListener(this, -1, -1, -1);
    }

    public void locationUpdated(LocationProvider provider, Location location) {
	handler.handleUpdate(location);
    }

    public void providerStateChanged(LocationProvider provider, int newState) {
    }

    class UpdateHandler implements Runnable {
	private Location updatedLocation = null;

	// The run method performs the actual processing of the location 
	// updates
	public void run() {
	    Location locationToBeHandled = null;
	    while (!done) {
		synchronized(this) {
		    if (updatedLocation == null) {
			try {
			    wait();
			} catch (Exception e) {
			    // Handle interruption
			}
		    }   
		    locationToBeHandled = updatedLocation;
		    updatedLocation = null;
		}

		// The benefit of the MessageListener is here.
		// This thread could via similar triggers be
		// handling other kind of events as well in
		// addition to just receiving the location updates.
		if (locationToBeHandled != null) 
		    processUpdate(locationToBeHandled);
	    }
	}

	public synchronized void handleUpdate(Location update) {
	    updatedLocation = update;
	    notify();
	}

	private void processUpdate(Location update) {
	    if ( update.getQualifiedCoordinates().distance(
			lastValidLocation.getQualifiedCoordinates() ) 
		 > movementDistance ) {
		// Alert user to movement...
		    
		// Cache new position as we have moved a sufficient distance 
		// from last one
		lastValidLocation = location;
	    }
	}
    }
}

Da para ter uma ideia...

o_Wesley_o

eu fiz o teste…
mas da um erro na ultima linha…

lastValidLocation = location;

ele não está encontrando a variável location…

você tem a explicação de como utilizar o código???

Criado 2 de novembro de 2010
Ultima resposta 7 de nov. de 2010
Respostas 2
Participantes 2