Esempio n. 1
0
  private Geopoint getDestination() {
    Geopoint result = null;
    Geopoint coords = null;

    String bearingText = ((EditText) findViewById(R.id.bearing)).getText().toString();
    String distanceText = ((EditText) findViewById(R.id.distance)).getText().toString();
    String latText = latButton.getText().toString();
    String lonText = lonButton.getText().toString();

    if (StringUtils.isBlank(bearingText)
        && StringUtils.isBlank(distanceText)
        && StringUtils.isBlank(latText)
        && StringUtils.isBlank(lonText)) {
      showToast(res.getString(R.string.err_point_no_position_given));
      return null;
    }

    if (StringUtils.isNotBlank(latText) && StringUtils.isNotBlank(lonText)) {
      try {
        coords = GeopointParser.parse(latText, lonText);
      } catch (GeopointParser.ParseException e) {
        showToast(res.getString(e.resource));
        return null;
      }
    } else {
      if (geo == null || geo.coordsNow == null) {
        showToast(res.getString(R.string.err_point_curr_position_unavailable));
        return null;
      }

      coords = geo.coordsNow;
    }

    if (StringUtils.isNotBlank(bearingText) && StringUtils.isNotBlank(distanceText)) {
      // bearing & distance
      Double bearing = null;
      try {
        bearing = new Double(bearingText);
      } catch (Exception e) {
        // probably not a number
      }
      if (bearing == null) {
        helpDialog(
            res.getString(R.string.err_point_bear_and_dist_title),
            res.getString(R.string.err_point_bear_and_dist));
        return null;
      }

      double distance;
      try {
        distance = DistanceParser.parseDistance(distanceText, Settings.isUseMetricUnits());
      } catch (NumberFormatException e) {
        showToast(res.getString(R.string.err_parse_dist));
        return null;
      }

      final Geopoint coordsDst = coords.project(bearing, distance);

      if (coordsDst == null) {
        showToast(res.getString(R.string.err_point_location_error));
        return null;
      }

      result = coordsDst;
    } else if (coords != null) {
      result = coords;
    } else {
      return null;
    }

    saveCoords(result);

    return result;
  }
    public void onClick(View arg0) {
      final String bearingText = ((EditText) findViewById(R.id.bearing)).getText().toString();
      final String distanceText = ((EditText) findViewById(R.id.distance)).getText().toString();
      final String latText = ((Button) findViewById(R.id.buttonLatitude)).getText().toString();
      final String lonText = ((Button) findViewById(R.id.buttonLongitude)).getText().toString();

      if (StringUtils.isBlank(bearingText)
          && StringUtils.isBlank(distanceText)
          && StringUtils.isBlank(latText)
          && StringUtils.isBlank(lonText)) {
        helpDialog(
            res.getString(R.string.err_point_no_position_given_title),
            res.getString(R.string.err_point_no_position_given));
        return;
      }

      double latitude;
      double longitude;

      if (StringUtils.isNotBlank(latText) && StringUtils.isNotBlank(lonText)) {
        try {
          latitude = GeopointParser.parseLatitude(latText);
          longitude = GeopointParser.parseLongitude(lonText);
        } catch (GeopointParser.ParseException e) {
          showToast(res.getString(e.resource));
          return;
        }
      } else {
        if (geo == null || geo.coordsNow == null) {
          showToast(res.getString(R.string.err_point_curr_position_unavailable));
          return;
        }

        latitude = geo.coordsNow.getLatitude();
        longitude = geo.coordsNow.getLongitude();
      }

      Geopoint coords = null;
      if (StringUtils.isNotBlank(bearingText) && StringUtils.isNotBlank(distanceText)) {
        // bearing & distance
        double bearing = 0;
        try {
          bearing = Double.parseDouble(bearingText);
        } catch (NumberFormatException e) {
          helpDialog(
              res.getString(R.string.err_point_bear_and_dist_title),
              res.getString(R.string.err_point_bear_and_dist));
          return;
        }

        double distance;
        try {
          distance = DistanceParser.parseDistance(distanceText, Settings.isUseMetricUnits());
        } catch (NumberFormatException e) {
          showToast(res.getString(R.string.err_parse_dist));
          return;
        }

        coords = new Geopoint(latitude, longitude).project(bearing, distance);
      } else {
        coords = new Geopoint(latitude, longitude);
      }

      String name = ((EditText) findViewById(R.id.name)).getText().toString().trim();
      // if no name is given, just give the waypoint its number as name
      if (name.length() == 0) {
        name = res.getString(R.string.waypoint) + " " + String.valueOf(wpCount + 1);
      }
      final String note = ((EditText) findViewById(R.id.note)).getText().toString().trim();

      final cgWaypoint waypoint = new cgWaypoint(name, type);
      waypoint.setGeocode(geocode);
      waypoint.setPrefix(prefix);
      waypoint.setLookup(lookup);
      waypoint.setCoords(coords);
      waypoint.setNote(note);

      if (app.saveOwnWaypoint(id, geocode, waypoint)) {
        finish();
        return;
      } else {
        showToast(res.getString(R.string.err_waypoint_add_failed));
      }
    }