0

我只是試圖在我的地圖活動中獲取國家/地區名稱,當我觸摸地圖時。 我已經按照Android: Reverse geocoding - getFromLocation,這裏是我的代碼中的onTouchEvent:當在地圖活動中請求國家名稱時,服務不可用Android

public boolean onTouchEvent(MotionEvent event, MapView mapView) 
    { 
     //---when user lifts his finger--- 

     if (event.getAction() == 1) {     
      GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY()); 

      Geocoder geoCoder = new Geocoder(mContext, Locale.getDefault()); 
      try { 
       List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6()/1E6, p.getLongitudeE6()/1E6, 1); 

       String add = ""; 
       if (addresses.size() > 0) 
       { 
        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++) 
        { 
         add += addresses.get(0).getAddressLine(i) + "\n"; 
         add += addresses.get(0).getCountryName() + "\n"; 
        } 
       } 

       Toast.makeText(mContext, add, Toast.LENGTH_LONG).show(); 
      } 
      catch (IOException e) {     
       e.printStackTrace(); 
      } 
      return false; 
     } 
     else     
      return false; 
    } 

所有我想要的是,當用戶擡起手指吐司將顯示地址和國名。當應用程序運行時,它不顯示任何錯誤,但也不顯示Toast。 Logcat顯示java.io.IOException: Service not Available

起初我以爲我的代碼在onTouchEvent內有錯誤,所以我做了一個實驗,將它改成這個,檢查Toast是否顯示。

public boolean onTouchEvent(MotionEvent event, MapView mapView) { 
     // TODO Auto-generated method stub 
     if (event.getAction() == 1) { 
      GeoPoint p = mapView.getProjection().fromPixels((int)event.getX(), (int)event.getY()); 
      Toast.makeText(mContext, p.getLatitudeE6()/1E6 + ", " + p.getLongitudeE6()/1E6, Toast.LENGTH_SHORT).show(); 
     } 
     return false; 
    } 

吐司顯示的經緯度完美,所以我仍然想知道什麼是錯的時候,我只想得到國名。 而我仍然不知道logcat的說:服務不可用。 當我想要做反向地理編碼時,是否有任何限制?

回答

相關問題