2016-07-12 18 views
0

我在兩個活動之間傳遞數據。我想這樣做:當用戶長時間點擊地圖時,會打開新的InfoActivity窗口,顯示緯度,經度和製作longclick的城市。在Android中長時間點擊顯示城市

我已經通過經緯度,但卡住了城市的一部分。

@Override 
     public void onMapLongClick(LatLng arg0) { 

       Intent intent = new Intent(getActivity(), InfoActivity.class); 
       intent.putExtra("latitude", arg0.latitude); 
       intent.putExtra("longitude", arg0.longitude); 
       startActivity(intent); 

     } 

如何獲得地圖長按的城市並將其傳遞到我的InfoActivity。

任何幫助表示讚賞:)

+1

您可能想要谷歌搜索「反向地理編碼」。 –

+0

@Gerald Schenider會做。謝謝。 – Kemo

回答

1

我怎麼會得到其中地圖早已點擊

使用Geocoderarg0用於獲取城市所在的城市地圖長clicked.like :

Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
List<Address> allAddresses = geocoder.getFromLocation(
           arg0.latitude, arg0.longitude, 1); 

現在從長期點擊所需的allAddresses中獲得所有詳細信息。

,並添加它傳遞給我的InfoActivity

對於傳遞兩個活動之間的字符串見下面的帖子:

Passing strings between activities in android

+0

我試着把這個代碼塊放到我的onMapLongClick中,但是我在地理編碼器中Geocoder geocoder = new Geocoder(this,Locale.getDefault());' - geocoder(android.content.context,Locale)應用於(匿名com.google.android.gms.maps.android maps.onmaplongclicklistener) – Kemo

+1

@Kemo:使用你當前的活動上下文而不是'this',就好像你在使用Activity然後使用'YourActivityName。 this'或者如果使用Fragment,則使用getActivity()而不是'this' –

+0

getActivity()工作。我也有另一個錯誤 - 在'geocoder.getFromLocation(arg0.latitude,arg0.longitude,1);'得到這個錯誤 - 未處理的異常java.ioexception,即使我嘗試了: '嘗試ArrayList

allAddresses = geocoder。 getFromLocation(arg0.latitude,arg0.longitude,1); (IOException e){ } catch(IOException e)e.printStackTrace(); }' – Kemo

1

您可以隨時使用地理編碼:

Geocoder geocoder = new Geocoder(this, Locale.getDefault()); 
List<Address> addresses = geocoder.getFromLocation(arg0.latitude, arg0.longitude, 1); 
String cityName = addresses.get(0).getAddressLine(0); 
相關問題