2011-05-06 88 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