سؤال

I have a method to get the current location of a device and I want to set its value to be assigned to a LatLng variable in onCreate.I'm sure this is a simple fix but I can't think of a solution.Can someone provide a solution to this?

Its easier to explain this in the code below:

protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.journey_planner);

  //Get direction between from and to positions
  //want to set fromposition to point to the values of lat and long in below method onCreate

  LatLng fromPosition = new LatLng(13.687140112679154, 100.53525868803263);
  LatLng toPosition = new LatLng(13.683660045847258, 100.53900808095932);   
}

@Override
public void onLocationChanged(Location location) {
  int lat = (int) (location.getLatitude());
  int lng = (int) (location.getLongitude());
}
هل كانت مفيدة؟

المحلول

If I am understanding this question correctly you would want to make your lat and lng to be class variables like @Clay said

int lat;
int lng;

Then you can set your fromPosition

protected void onCreate(Bundle savedInstanceState) {
...
LatLng fromPosition = new LatLng(lat, lng);
...
}

نصائح أخرى

Change your lat and lng variables from local to instance. In your class, outside the onCreate, say

int lat;

int lng;

Now your LatLng variables in your onCreate should have access to lat and lng

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top