2011-05-06 56 views
3

我目前僅使用GPS來獲取用戶的當前位置以返回特定結果。和比利時一樣,如果你在裏面,大部分時間你都無法獲得GPS連接。所以我想通過wifi連接獲得當前位置。使用GPS和WIFI獲取當前位置

我發現這個例子:get the current location (gps/wifi)

我不知道這行告訴選擇哪個連接的設備。我猜這是這個:String provider = myLocationManager.getBestProvider(criteria,true);我必須添加到我的代碼。

當我檢查裏面有什麼,我總是得到一個空值,我不明白爲什麼。目前

我的代碼如下所示:

public void getOwnLngLat(){ 
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
final LocationListener locationListener = new LocationListener(){ 

public void onLocationChanged(Location location) { 
    // TODO Auto-generated method stub 
    longitude="" + location.getLongitude(); 
    latitude="" + location.getLatitude(); 
} 

public void onProviderDisabled(String provider) { 
    // TODO Auto-generated method stub 

} 

public void onProviderEnabled(String provider) { 
    // TODO Auto-generated method stub 

} 

public void onStatusChanged(String provider, int status, 
       Bundle extras) { 
    // TODO Auto-generated method stub 

    }   
}; 
locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), true), 2000, 10, locationListener); 
} 

回答

2

如果你想從一個特定的供應商更新,你可以使用LocationManagerrequestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener)方法。使用​​3210方法可獲得可用提供者的列表。

+0

你是什麼意思的更新? (英語不是我的母語)我只想獲得當前位置的經度和緯度。 我發現了另一個例子:http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in- an/3145655#3145655將執行此幫助我轉發? – Hannelore 2011-05-06 08:17:55

+0

位置提供者(例如GPS)會將位置更新(緯度,經度,時間,精度等)傳遞給您的LocationListener對象。這些更新在您調用requestLocationUpdates方法時開始,並且會定期繼續,直到您調用removeUpdates方法。 – 2011-05-06 08:21:20

3

您正在使用locationmanagers getbestprovider方法,該方法只獲取與您的條件最匹配的任何位置提供程序。 如果您想要獲取特定的位置提供程序,請使用getProvider方法。該參數應該是getProviders方法的結果之一。 部分鏈接: http://developer.android.com/guide/topics/location/obtaining-user-location.html http://developer.android.com/reference/android/location/LocationManager.html

+0

所以,如果我理解你,如果我的GPS沒有啓用,應用程序會自動選擇WiFi(這是啓用)來做詭計? – Hannelore 2011-05-06 08:24:40

+0

是的,你懂了!使用getProviders方法(參數true!)來查看啓用了哪些位置提供程序。 – arnodenhond 2011-05-06 08:32:08

+0

我測試過這個,在我的三星Galaxy Ace上,當我關掉我的GPS和我的wifi上(我連接到一個開放的wifi),我的p變量爲空... – Hannelore 2011-05-06 08:47:25