OnClick und Geodaten

F

Fraver

Neues Mitglied
1
Hallo,

in meiner App habe ich einen Button. Wenn ich den drücke, werden die aktuellen GPS Koordinaten, inkl. Adressdaten, sowie die Uhrzeit ermittelt und alles jeweils in ein TextView geschrieben.

Grad komme ich von einem Feldversuch, wo ich folgendes feststellen musste:

Die Koordinaten und die Uhrzeit werden sofort aktualisiert. Nur die dazu gehörige Adresse ändert sich erst, wenn ich den Button ein zweites Mal drücke.

Jetzt bin ich am Überlegen, das ganze OnClick Event in eine for-Schleife zu packen, also i=0 i<=1 i++,
um so den ganzen Block zwei Mal auszuführen.

Aber: Ist das machbar oder gibt es noch elegantere Lösungen?

Bin für alles offen.

Lg Andy
 
Bitte zeig die Codestellen wie du die "aktuellen" GPS-Koordinaten bzw. Adresse holst?
 
einmal das onClick:

Code:
        startstop.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                final GPSService mGPSService = new GPSService(getApplicationContext());
                
                
                for (count = 0; count <=1; count++) {
                              
                
                mGPSService.getLocation();
                if (mGPSService.isLocationAvailable == false){
                    Toast.makeText(getApplicationContext(), "Deine Position ist nicht verfügbar", Toast.LENGTH_LONG).show();
                    return;
                } else {
                    adresse1 = mGPSService.getLocationAddress();
                    TextView tvAdresseStart = (TextView)findViewById(R.id.tv_adresse);
                    tvAdresseStart.setText(adresse1);
                    
                    TextView tvKoordinaten = (TextView)findViewById(R.id.tv_koordinaten);
                    tvKoordinaten.setText("Latitude: " + mGPSService.getLatitude() + " | Longitude: " + mGPSService.getLongitude());

                    TextView tvDate = (TextView)findViewById(R.id.tv_datetime);

                    SimpleDateFormat sdf = new SimpleDateFormat("EEEE, dd.MM.yyyy");
                    String datum = sdf.format(new Date());

                    SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm");
                    String uhrzeit = sdf2.format(new Date());

                    tvDate.setText(datum + " | " + uhrzeit + " Uhr");

                    mPLZ = mGPSService.getPLZ();
                    mOrt = mGPSService.getOrt();
                    mStrasseNr = mGPSService.getStrasseNr();
                    
                    TextView tvC = (TextView)findViewById(R.id.textView3);
                    tvC.setText(Integer.toString(count));
                    

                }     
                
                Handler handler = new Handler();
                           
                handler.postDelayed(new Runnable() {
                @Override
                public void run() {
               
                        mGPSService.closeGPS();
                    }
                },5000); //adding one sec delay
                
                
                }

wobei ich das mit der for-Schleife eben ausprobiert habe. Der Effekt bleibt der gleiche.
Erst beim zweiten Mal klicken wird die Adresse aktualisiert.


Und das ist meine komplette GPS Klasse:

Code:
public class GPSService extends Service implements LocationListener {

    private final Context mContext;

    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;

    public boolean isLocationAvailable = false;

    Location mLocation;
    double mLatitude, mLongitude;
    String mStrasse, mPLZ, mOrt, mStrasseNr;

    private static final long TIME = 1000;
    private static final long DISTANCE = 0;

    protected LocationManager mLocationManager;

    List<Address> addresses = null;

    public GPSService(Context context) {
        this.mContext = context;
        mLocationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
    }

    public Location getLocation(){
        try {
            isGPSEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            if (isGPSEnabled){
                mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TIME, DISTANCE, this);
                if (mLocationManager != null){
                    mLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    if (mLocation != null) {
                        mLatitude = mLocation.getLatitude();
                        mLongitude = mLocation.getLongitude();
                        isLocationAvailable = true;

                        return mLocation;
                    }
                }
            }
            isNetworkEnabled = mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
            if (isNetworkEnabled) {
                mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, TIME, DISTANCE, this);
                if (mLocationManager != null) {
                    mLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (mLocation != null) {
                        mLatitude = mLocation.getLatitude();
                        mLongitude = mLocation.getLongitude();
                        isLocationAvailable = true;

                        return mLocation;
                    }
                }
            }

            if (!isGPSEnabled) {
                askUserToOpenGPS();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        isLocationAvailable = false;
        return null;
    }

    public String getLocationAddress(){
        if (isLocationAvailable) {
            Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());


            try {
                addresses = geocoder.getFromLocation(mLatitude, mLongitude, 1);
            } catch (IOException e1){
                e1.printStackTrace();
            } catch (IllegalArgumentException e2){
                String errorString = "Illegal arguments "
                                + Double.toString(mLatitude) + " , "
                                + Double.toString(mLongitude)
                                + " passed to address service";
                e2.printStackTrace();
                return errorString;
            }
            if (addresses != null && addresses.size() > 0){
                Address address = addresses.get(0);

                String addressText = String.format("%s, %s %s",
                                                    address.getMaxAddressLineIndex() >0 ? address.getAddressLine(0) : "",
                                                    address.getPostalCode(),
                                                    address.getLocality());
                return addressText;
            } else {
                return "Keine Adresse gefunden";
            } } else {
                return "Standort nicht verfügbar.";
            }
        }


    public double getLatitude(){
        if (mLocation != null){
            mLatitude = mLocation.getLatitude();
        }
        return mLatitude;
    }

    public double getLongitude(){
        if (mLocation != null){
            mLongitude = mLocation.getLongitude();
        }
        return mLongitude;
    }

    public String getStrasse(){
        if (isLocationAvailable) {
            Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());


            try {
                addresses = geocoder.getFromLocation(mLatitude, mLongitude, 1);
            } catch (IOException e1){
                e1.printStackTrace();
            } catch (IllegalArgumentException e2){
                String errorString = "Illegal arguments "
                        + Double.toString(mLatitude) + " , "
                        + Double.toString(mLongitude)
                        + " passed to address service";
                e2.printStackTrace();
                return errorString;
            }
            if (addresses != null && addresses.size() > 0){
                Address address = addresses.get(0);

                String strasse = address.getThoroughfare();
                return strasse;
            } else {
                return "Keine Adresse gefunden";
            } } else {
            return "Standort nicht verfügbar.";
        }
    }

    public String getPLZ(){
        if (isLocationAvailable) {
            Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());


            try {
                addresses = geocoder.getFromLocation(mLatitude, mLongitude, 1);
            } catch (IOException e1){
                e1.printStackTrace();
            } catch (IllegalArgumentException e2){
                String errorString = "Illegal arguments "
                        + Double.toString(mLatitude) + " , "
                        + Double.toString(mLongitude)
                        + " passed to address service";
                e2.printStackTrace();
                return errorString;
            }
            if (addresses != null && addresses.size() > 0){
                Address address = addresses.get(0);

                String plz = address.getPostalCode();
                return plz;
            } else {
                return "Keine Adresse gefunden";
            } } else {
            return "Standort nicht verfügbar.";
        }
    }

    public String getOrt(){
        if (isLocationAvailable) {
            Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());


            try {
                addresses = geocoder.getFromLocation(mLatitude, mLongitude, 1);
            } catch (IOException e1){
                e1.printStackTrace();
            } catch (IllegalArgumentException e2){
                String errorString = "Illegal arguments "
                        + Double.toString(mLatitude) + " , "
                        + Double.toString(mLongitude)
                        + " passed to address service";
                e2.printStackTrace();
                return errorString;
            }
            if (addresses != null && addresses.size() > 0){
                Address address = addresses.get(0);

                String ort = address.getLocality();
                return ort;
            } else {
                return "Keine Adresse gefunden";
            } } else {
            return "Standort nicht verfügbar.";
        }
    }

    public String getStrasseNr(){
        if (isLocationAvailable) {
            Geocoder geocoder = new Geocoder(mContext, Locale.getDefault());


            try {
                addresses = geocoder.getFromLocation(mLatitude, mLongitude, 1);
            } catch (IOException e1){
                e1.printStackTrace();
            } catch (IllegalArgumentException e2){
                String errorString = "Illegal arguments "
                        + Double.toString(mLatitude) + " , "
                        + Double.toString(mLongitude)
                        + " passed to address service";
                e2.printStackTrace();
                return errorString;
            }
            if (addresses != null && addresses.size() > 0){
                Address address = addresses.get(0);

                String strasseNr = address.getAddressLine(0);
                return strasseNr;
            } else {
                return "Keine Adresse gefunden";
            } } else {
            return "Standort nicht verfügbar.";
        }
    }

    public void closeGPS(){
        if (mLocationManager != null){
            mLocationManager.removeUpdates(GPSService.this);
        }
    }
    public void askUserToOpenGPS() {
        AlertDialog.Builder mAlertDialog = new AlertDialog.Builder(mContext);

        // Setting Dialog Title
        mAlertDialog.setTitle("Standort nicht verfügbar. GPS öffnen?")
                .setMessage("GPS aktivieren, um Standort zu bestimmen?")
                .setPositiveButton("Einstellungen öffnen", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        mContext.startActivity(intent);
                    }
                })
                .setNegativeButton("Abbrechen",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                }).show();
    }

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

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }


}
 
Ich glaube, das Problem erst Mal wie folgt gelöst zu haben:

Beim Klicken werden die Koordinaten ermittelt, welche an Variablen übergeben werden.
Der Geocoder sucht nun anhand der in den Variablen gespeicherten Koordinaten nach der Adresse.

Das scheint zu funktionieren.
 

Ähnliche Themen

M
  • MikelKatzengreis
Antworten
5
Aufrufe
171
swa00
swa00
Laser5001
Antworten
3
Aufrufe
656
swa00
swa00
W
Antworten
2
Aufrufe
753
rene3006
R
Zurück
Oben Unten