2011-05-14 79 views
0

我想點擊一個按鈕,接收當前位置,我知道我不能立即得到位置,所以這是我所做的: 單擊事件:獲得位置立即在Android的

 public void onClick(View v) 
     { 
      ProgressDialog MyDialog = ProgressDialog.show(MainPage.this, " " , " Loading. Please wait ... ", true); 
      MyActionsHandler myActionsHandler = new myActionsHandler(MainPage.this); 
      myActionsHandler.startSearch(); 
      MyDialog.dismiss(); 
      Intent intent = new Intent(MainPage.this, ResultPage.class); 
      startActivity(intent); 
     } 

,這是搜索的位置

public void startSearch(long timeInterval,float distanceInterval) 
{ 
    LocationManager lm = (LocationManager)_context.getSystemService(Context.LOCATION_SERVICE); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, timeInterval, 
      distanceInterval, this); 

    while(!_locationFound) 
    { 
     //wait till location is found 
    } 
} 

public void onLocationChanged(Location location) 
{ 
    if (location != null) 
    { 
     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 
     float speed = location.getSpeed(); 
     float bearing = location.getBearing(); 

     Log.d("LOCATION CHANGED", location.getLatitude() + ""); 
     Log.d("LOCATION CHANGED", location.getLongitude() + ""); 
     try 
     { 
      doTheProcess(_searchType,latitude, longitude, speed, bearing); 
      _locationFound = true; 
     } 
     catch (Exception e) 
     { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

我明白,這不工作的處理程序,因爲迴路是在同一個線程, 所以你有什麼建議做最好的解決辦法?

在requestLocationUpdates的javadoc的

,有「調用線程必須是一個彎針線諸如呼叫活動的主線程」。但我還沒有找到任何例子,所以我不知道它是否是正確的解決方案。

還有一個問題, getLastKnownLocation()的工作,即使我以前從來沒有調用locationManager? 感謝

回答

3

我有同樣的問題before..but我已經得到了solution..this是獲得位置瞬間最簡單的方法。

public class LocationFinder extends Activity { 

    TextView textView1; 
    Location currentLocation; 
    double currentLatitude,currentLongitude; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     textView1 = (TextView) findViewById(R.id.textView1); 
     Log.i("@@@@@@@@@@ Inside LocationFinder onCreate", "LocationFinder onCreate"); 

     FindLocation(); 

    } 

    public void FindLocation() { 
     LocationManager locationManager = (LocationManager) this 
       .getSystemService(Context.LOCATION_SERVICE); 

     LocationListener locationListener = new LocationListener() { 
      public void onLocationChanged(Location location) { 
       updateLocation(location); 

       Toast.makeText(
         LocationFinder.this, 
         String.valueOf(currentLatitude) + "\n" 
           + String.valueOf(currentLongitude), 5000) 
         .show(); 

       } 

      public void onStatusChanged(String provider, int status, 
        Bundle extras) { 
      } 

      public void onProviderEnabled(String provider) { 
      } 

      public void onProviderDisabled(String provider) { 
      } 
     }; 
     locationManager.requestLocationUpdates(
       LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

    } 


    void updateLocation(Location location) { 
      currentLocation = location; 
      currentLatitude = currentLocation.getLatitude(); 
      currentLongitude = currentLocation.getLongitude(); 
      textView1.setText(String.valueOf(currentLatitude) + "\n" 
        + String.valueOf(currentLongitude)); 

     } 
} 
+0

使用NETWORK_PROVIDER中的LocationManager .requestLocationUpdates – MKJParekh 2011-11-07 12:24:18

0

你可以擺脫完全由_locationFound變量 - 什麼是您最初將不得不在

while(!_locationFound) {} 

塊?

如果你擺脫這一點,移動任何東西,你會原本在該塊到您的doTheProcess功能(或者你設置_locationFound爲true),那麼它應該工作,我相信。

+0

您好我剛剛更新了我的帖子,我使用'而(!_ locationFound){}'總是運行進度對話框,直到它找到位置。 – 2011-05-14 15:31:26

+1

在這種情況下,您可以使ProgressDialog myDialog成爲您類的私有成員變量。像你一樣在onClick()函數中創建對話框,然後忘掉它。然後,您可以在onLocationChanged函數調用myDialog.dismiss()(雖然你可能會想檢查是否已經先撤了,因爲onLocationChanged會越來越調用,直到你從位置更新退訂。 – actionshrimp 2011-05-14 15:35:30

+0

你認爲這是最好的方法? 我者優先,使從呼叫者獲取獨立的位置的作用。因此,我可以從其他地方調用它(服務,其他活動...)或者,也許我已經得到了概念錯誤? – 2011-05-14 19:57:10

0

我經歷了,我不能接受的位置更新與我自己的線程/ HandlerThreads混合LocationListener的類似的東西。解決方案是使用PendingIntent和requestLocationUpdates(provider,minTime,minDistance,intent)。看一看:How to receive location updates without using a service