2011-05-14 38 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