2011-11-23 88 views
2

我有一個活動,它顯示兩個地點之間的路線。這需要兩個地點。將兩個相同的值傳遞給一個活動

這actvity看起來像

final double fromLat = location.getLatitude(); // user location 
final double fromLon = location.getLongitude(); 

final double toLat=this.getIntent().getDoubleExtra("tolat", 0.0); 
final double toLon=this.getIntent().getDoubleExtra("tolng",0.0); 
    Log.v(tag, "flat"+fromLat+"flon"+fromLon+"toLat"+toLat+"tolon"+toLon); 
      new Thread() { 
       @Override 
        public void run() { 
        String url = RoadProvider.getUrl(fromLat, fromLon, toLat, toLon); 
          InputStream is = getConnection(url); 
          mRoad = RoadProvider.getRoute(is); 
          mHandler.sendEmptyMessage(0); 
        } 
      }.start(); 
    } 

而在第一兩個活動它加載neaby地方的列表,並在第二,我們可以搜索任何一個城市,它也顯示在城市附近的地方列表。

如果用戶點擊兩個活動的任何列表中的項目,我想顯示路線。這意味着我想從兩個活動中傳遞lat和lng(geopoints)。

在上面的代碼toLat和toLng將保持不變,但fromLat和fromLng將根據用戶點擊哪個ListActivity進行更改。

如何從我的citysearch活動中傳遞值,以便fromLat和fromLng會根據特定活動進行更改。

希望我的問題很清楚。

在此先感謝。

回答

3

您可以使用意向的putextra方法意圖傳遞值。 並使用getExtra方法獲取值。

// Put values with intent. 
Intent intent_name = new Intent(nearby_place.this,geopoint.class); 
intent_name.putExtra("longitude", "72"); 
intent_name.putExtra("latitude", "22"); 
startActivity(intent_name); 

// Put values with intent. 
Intent intent_name = new Intent(city_search.this,geopoint.class); 
intent_name.putExtra("longitude", "72"); 
intent_name.putExtra("latitude", "22"); 
startActivity(intent_name); 

//Get values which comes with intent 
// You can get the values of longitude & latitude in geopoint class as below method 
int longitude = getIntent().getExtras().getInt("longitude"); 
int latitude = getIntent().getExtras().getInt("latitude"); 
+0

雅我知道,但在我的代碼書面上面僅適用於用戶當前位置的作品,但如果我將通過lat和lng從seacrhCity活動。那麼fromLat和fromLng會如何相應改變。我想說的是,我只能使用fromLat和fromLng中的一個,並將它傳遞給函數,但我有兩個。 –

+0

您可以使用getExtra方法獲取geopoint活動中的lat和long值。當你在當前位置活動之後進行地理參與活動時,您將獲得當前位置活動的經緯度,如果您在城市搜索活動之後參加了地理參與活動,則會從城市搜索活動中獲得緯度和經度 –

相關問題