aktuelle Position bestimmten GPS

H

herrmie

Neues Mitglied
0
Hallo Gemeinde,

ich habe mir ein kleines APP geschrieben, dass mir die Koordinaten und das ganze auf Google Maps anzeigt. Problem was ich aber nun habe ist folgendes: Ändere ich die Koordinaten in meiner DDMS dann funktioniert es wunderbar. Wenn ich das Programm aber gestartet habe, dann soll es ja die aktuelle Position bestimmen. Hab im Internet Tutorials gefunden, bei denen es einen Befehl getCurrentPosition gibt. Nur leider gibt es den Befehl nicht bei mir.

Kann mir jemand vielleicht sagen an was das liegt? Oder wie ich sonst noch die aktuelle Position bekommen kann. Denn unter getLastKnownPosition bekomme ich ja nur die letzte Position, als das GPS aktiv war.

Ich benutze API Level 3 (1.5).

Danke schonmal

gruß
herrmie
 
Hallo,
du mußt dir beim LocationManager mit requestLocationUpdates einen LocationListener registrieren. Und den beizeiten wieder abmelden....
Gruß
A.
 
Hallo,
das wäre mein Code. wenn ich bei requestLocationUpdate für die Distanz 0 eingebe, dann müsste er doch immer nach der angegeben Zeit, hier 5 Sekunden, die Position updaten oder?

Code:
public class GPS extends MapActivity implements LocationListener {
    
    TextView myLoc = null;
    MapView myMap = null;
    double lat = 0;
    double lng = 0;
    GeoPoint geoPoint = null;
    MapController mapController = null;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        myLoc = (TextView)findViewById(R.id.Ausgabe);
        
        myLoc.setText("Lat: " + lat + " Long: " + lng);
        
        myMap = (MapView) findViewById(R.id.myMap);
        geoPoint = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
        myMap.setSatellite(false);
        
        //Getting the MapController
        mapController = myMap.getController();
        mapController.setCenter(geoPoint);
        mapController.setZoom(15);
        
        //get Overlay
        MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
        List<Overlay> list = myMap.getOverlays();
        list.add(myLocationOverlay);
        
        //Adding zoom controls
        myMap.setBuiltInZoomControls(true);
        myMap.displayZoomControls(true);
        
        LocationManager locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500l, 0, this);
        
        
    }

    @Override
    public void onLocationChanged(Location location) {
        if(location != null) {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            
            myLoc.setText("Lat: " + lat + " Long: " + lng);
             
            geoPoint = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
            mapController.animateTo(geoPoint);
                
            Context context = getApplicationContext();
            CharSequence text = "Lat: " + lat + " Long: " + lng;
             
            Toast toast = Toast.makeText(context, text, 5);
            toast.show();
        }
        
    }

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

    @Override
    public void onProviderEnabled(String arg0) {
    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub
    }
    
    protected class MyLocationOverlay extends com.google.android.maps.Overlay {
        
        @Override
        public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
            Paint paint = new Paint();
            
            super.draw(canvas, mapView, shadow);
            // Converts lat/lng-Point to OUR coordinates on the screen.
            Point myScreenCoords = new Point();
            mapView.getProjection().toPixels(geoPoint, myScreenCoords);
            
            paint.setStrokeWidth(1);
            paint.setARGB(255, 255, 255, 255);
            paint.setStyle(Paint.Style.STROKE);
            
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
            
            canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
            canvas.drawText("Das bist du ...", myScreenCoords.x, myScreenCoords.y, paint);
            return true;
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}
 
Hallo,
deine onLocationChanged Methode wird nur aufgerufen, wenn GPS enabled ist und deine APP die entsprechenden Rechte hat. Und selbst dann kann es bis zum ersten Aufruf mehr als 5 sec dauern, denn das Anschalten von GPS und das Suchen nach Satelliten dauert.
Gruß
A.
 

Ähnliche Themen

R
Antworten
9
Aufrufe
727
koje71
koje71
H
Antworten
2
Aufrufe
928
swa00
swa00
M
Antworten
2
Aufrufe
771
Mimi1989
M
Zurück
Oben Unten