問題描述
是否可以在不使用 GPS 或互聯網的情況下獲取用戶的當前位置?我的意思是在移動網絡提供商的幫助下.
Is it possible to get the current location of user without using GPS or the internet? I mean with the help of mobile network provider.
推薦答案
你要做的是使用 LocationManager.NETWORK_PROVIDER
而不是 LocationManager.GPS_PROVIDER
.NETWORK_PROVIDER
將在可用的 GSM 或 wifi 上解析.顯然,在關閉 wifi 的情況下,將使用 GSM.請記住,使用蜂窩網絡基本上可以精確到 500m.
What you are looking to do is get the position using the LocationManager.NETWORK_PROVIDER
instead of LocationManager.GPS_PROVIDER
. The NETWORK_PROVIDER
will resolve on the GSM or wifi, which ever available. Obviously with wifi off, GSM will be used. Keep in mind that using the cell network is accurate to basically 500m.
http://developer.android.com/guide/topics/location/obtaining-user-location.html 有一些非常棒的信息和示例代碼.
http://developer.android.com/guide/topics/location/obtaining-user-location.html has some really great information and sample code.
在完成OnCreate()
中的大部分代碼后,添加以下代碼:
After you get done with most of the code in OnCreate()
, add this:
// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
// Called when a new location is found by the network location provider.
makeUseOfNewLocation(location);
}
public void onStatusChanged(String provider, int status, Bundle extras) {}
public void onProviderEnabled(String provider) {}
public void onProviderDisabled(String provider) {}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
您還可以讓您的活動實現 LocationListener
類,從而在您的活動中實現 onLocationChanged()
.
You could also have your activity implement the LocationListener
class and thus implement onLocationChanged()
in your activity.
這篇關于在不使用 GPS 或互聯網的情況下獲取用戶在 Android 中的當前位置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!