Search code examples
javagpscodenameonelocationlistener

Codename one GPS tracker


I want to track the device with GPS signal. At the same time show latitude, longitude, altitude and speed.

I use this code and there is no result on the screen:

@Override
protected void onGpsTracker_ButtonAction(Component c, ActionEvent event) {
    try {
        int i = 0;
        Location loc = mylocLocationManager.getCurrentLocation();
        if(loc.getStatus()==LocationManager.AVAILABLE) {
        } else {
            Dialog.show("Error!", "Falla de señal", "OK", null);
        }
        mylocLocationManager.setLocationListener(new LocationListener() {
            public void locationUpdated(Location location) {
                gpsLocation= location;
                Component c = null;
                Label labelalt = (Label)findByName("altitudT", c);
                Label labellat = (Label)findByName("latitudT", c);
                Label labellng = (Label)findByName("longitudT", c);
                Label labeldist = (Label)findByName("distanciaT", c);
                Label labelspeed = (Label)findByName("speedT", c);
                altmax= location.getAltitude();
                double lat = location.getLatitude();
                double lng = location.getLongitude();
                float speed =  location.getVelocity();

                double alt = location.getAltitude();
                velprompos = velprompos + 1;
                totspeed = (int) (totspeed + speed);
                velopro = totspeed / velprompos;
                totalt = altmax - alt;

                velmax=speed;
                Coord lastLocation = new Coord(lat, lng);
                mapComponent.zoomTo(lastLocation, 15);

                prevdistance = totdistance;
                prevLon = currentLon;
                prevLat = currentLat;
                String Salt = String.valueOf(alt);
                labelalt.setText(Salt);
                String Slat = String.valueOf(lat);
                labellat.setText(Slat);
                String Slng = String.valueOf(lng);
                labellng.setText(Slng);
                String Sspeed = String.valueOf(speed);
                labelspeed.setText(Sspeed);

                //aca hay q pner dibujo lineas  
            }

            public void providerStateChanged(int newState) {
                //positionMethod();
            }
        });
    } catch (Exception ex) {
        ex.printStackTrace();
        gpsLocation = null;
    }
}     

The idea is to get the location when the device is moving, and show this results on some labels. Any help?


Solution

  • You are implementing couple of things wrongly.

    1. Add @override to overriden methods like locationUpdated and providerStateChanged .

    2. Don't re-declare Component c as and set it to null

    3. Get your labels by calling them directly with find method and pass c as a parameter

    4. revalidate or repaint your form after each update.

    5. As a general advice, keep your variable declaration consistence, declare Number of Mangoes as numOfMangoes and not numberofmangoes nor numofmangoes nor Numberofmangoes.

      @Override
      protected void onGpsTracker_ButtonAction(Component c, ActionEvent event) {
          try {
              int i = 0;
              Location loc = mylocLocationManager.getCurrentLocation();
              if (loc.getStatus() == LocationManager.AVAILABLE) {
                  System.out.println("Location available");
              } else {
                  Dialog.show("Error!", "Falla de señal", "OK", null);
              }
              final LocationManager mylocLocationManager = LocationManager.getLocationManager();
              mylocLocationManager.setLocationListener(new LocationListener() {
                  @Override
                  public void locationUpdated(Location location) {
                      gpsLocation = location;
                      Label labelspeed = ;
                      altmax = location.getAltitude();
                      double lat = location.getLatitude();
                      double lng = location.getLongitude();
                      float speed = location.getVelocity();
      
                      double alt = location.getAltitude();
                      velprompos = velprompos + 1;
                      totspeed = (int) (totspeed + speed);
                      velopro = totspeed / velprompos;
                      totalt = altmax - alt;
      
                      velmax = speed;
                      Coord lastLocation = new Coord(lat, lng);
                      mapComponent.zoomTo(lastLocation, 15);
      
                      prevdistance = totdistance;
                      prevLon = currentLon;
                      prevLat = currentLat;
                      String Salt = String.valueOf(alt);
                      findAltitudT(c).setText(Salt);
                      String Slat = String.valueOf(lat);
                      findLatitudT(c).setText(Slat);
                      String Slng = String.valueOf(lng);
                      findLongitudT(c).setText(Slng);
                      String Sspeed = String.valueOf(speed);
                      findSpeedT(c).setText(Sspeed);
      
                      c.getComponentForm().revalidate();
                      //aca hay q pner dibujo lineas  
                  }
      
                  @Override
                  public void providerStateChanged(int newState) {
                      //positionMethod();
                  }
              });
      
          } catch (Exception ex) {
              ex.printStackTrace();
              gpsLocation = null;
          }
      }