2017-12-16 2 views
0

Ich erstelle eine Android-App. Momentan ist die Position fest codiert. Die Längen- und Breitengrade werden in der oncreate-Methode angegeben. Ich habe eine Methode erstellt, um den Längen- und Breitengrad außerhalb der Oncreate-Methode zu erhalten.Übergeben von Variablen aus der Methode in eine andere Variable in der onCreate-Methode

Dies ist die oncreate Methode

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    getLocation(); 

    //Doublin Co-ordinates 
    final double latitude = 53.3498; 
    final double longitude = -6.2603; 

    //Getting th data 
    getForecast(latitude, longitude); 
    Log.d(Tag,"MAIN UI code running"); 
} 

Wie Sie die Lage der sehen in den latitude und latitude Variablen fest einprogrammiert ist.

Ich habe auch eine Methode erstellt, um die letzten bekannten Koordinaten des Telefons des Benutzers zu erhalten. Dies ist außerhalb der Oncreate-Methode.

void getLocation() { 
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION); 

    } else { 
     Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

     if (location != null){ 
      double latti = location.getLatitude(); 
      double longi = location.getLongitude();; 
     } else { 
      Toast.makeText(this, "Unable to get location. Please try again:", Toast.LENGTH_LONG).show(); 

     } 
    } 
} 

Wie würde ich ordnen Sie die Werte von latti und longi in der Lage Methode, um longitude und latitude im oncreate Verfahren.

+1

Sie sollten diese beiden Variablen vor 'getLocation();', dann übergeben Sie diese beiden Variablen an 'getLocation' Funktion –

+1

Sie Ich werde Probleme bekommen, weil man sich nicht auf 'getLastKnownLocation()' verlassen kann und sobald man tatsächlich Standortaktualisierungen verlangt - oder ein einzelnes Standortupdate - kann man 'getForecast() 'nicht mehr in' onCreate() 'aufrufen , weil das Laden des Standorts dann eine asynchrone Operation ist. Sie müssen es dann nur aufrufen, nachdem Sie den Standort erhalten haben. Die Antworten werden dich also ein wenig vorwärts bringen, aber nicht sehr weit. –

+0

Bitte akzeptieren Sie eine der Lösung, die Ihnen helfen –

Antwort

0

Wie würde ich ordnen Sie die Werte von latti und longi in der Lage Methode, um Länge und Breite in der oncreate Methode.

Ich würde Ihnen vorschlagen, Breiten- und longtitude als globale Variable zu deklarieren (Out of onCreate Methode)

final double latitude = 53.3498; 
final double longitude = -6.2603; 

dann in getLocation Funktion

if (location != null){ 
     double latti = latitude ; 
     double longi = longitude; 
    } else { 
     Toast.makeText(this, "Unable to get location. Please try again:", Toast.LENGTH_LONG).show(); 
    } 

Oder Sie können auf diese beiden Variablen übergeben getLocation Methode.

//Doublin Co-ordinates 
final double latitude = 53.3498; 
final double longitude = -6.2603; 
getLocation(latitude,longitude); 

Ändern getLocation Funktion auf diese Weise

void getLocation(double latitude,double longitude) { 
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION); 
    } else { 
     Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     if (location != null){ 
      double latti = latitude; 
      double longi = longitude; 
     } else { 
      Toast.makeText(this, "Unable to get location. Please try again:", Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
+0

Das ist nur Zuweisung von latti und longi die fest codierte Position nicht? – JAWER

+0

Antwort gerade bearbeitet –

0
  1. Der beste Weg, um die Felder als global zu erklären wäre und dann den Wert auf die Felder zuweisen und dann verwenden, in der onCreate() oder wo Sie möchten.

  2. Return ein Paar Instanz von getLocation()

    onCreate() { 
        .... 
        Pair<Double, Double> locationInfo = getLocation(); 
    
        final double latitude = locationInfo.first; 
        final double longitude = locationInfo.second; 
    } 
    
    
    Pair<Double, Double> getLocation(){ 
        .... 
        double latti = location.getLatitude(); 
        double longi = location.getLongitude(); 
    
        return new Pair<Double, Double>(latti, longi); 
    } 
    
  3. Was Sie tun können, Location-Instanz aus der getLocation() Methode ist und holen die latti und longi im onCreate()

0
  1. erklären die Latti und Longi in MainActivity. wie ich in diesem Code getan habe.

2.After die Methode getLocation() aufrufen, die die latti und longi Werte eingestellt, den Wert von latitude und longitude.sehen Sie bitte den Code

bitte versuchen Sie diesen Code:

public class MainActivity extends Activity 

{ 
     double latti; //new code 
     double longi; //new code 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 

    locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    getLocation(); 

    //Doublin Co-ordinates 
    final double latitude = latti; //i changed this line 
    final double longitude = longi;//i changed this line 

    //Getting th data 
    getForecast(latitude, longitude); 
    Log.d(Tag,"MAIN UI code running"); 
} //end of onCreate 

void getLocation() { 
    if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 

     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION); 

    } else { 
     Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 

     if (location != null){ 
      latti = location.getLatitude();//i changed this line, (drooped the double) 
      longi = location.getLongitude();//i changed this line, (drooped the double) 
     } else { 
      Toast.makeText(this, "Unable to get location. Please try again:", Toast.LENGTH_LONG).show(); 

     } 
    } 
} 

    } //end of MainActvity 

eine andere Lösung Getter & Setzern. (Randnotiz: Sie brauchen nicht die Breite und Länge, können Sie nur Latti und Longi verwenden)

Verwandte Themen