我的应用只需知道用户的国家/地区,而我的应用并不关心用户的移动位置。
我想知道的是: 当用户启动应用程序时,应用程序会将用户的大致位置(经度和纬度)发送给服务器。
这是我的错误代码。我尝试将字符串发送到getlocation函数,我希望它可以通过添加纬度和经度信息返回原始字符串。
private String getlocation (String url){
LocationManager status = (LocationManager) (this.getSystemService(Context.LOCATION_SERVICE));
if (status.isProviderEnabled(LocationManager.GPS_PROVIDER) || status.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
Log.v("print","enable to locate");
} else {
Log.v("print","fail to locate");
}
Location location = status.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if(location!=null){
Log.v("print","succed!!!@@");
Double longitude = location.getLongitude();
String loc=Double.toString(longitude);
url=url+loc;
}
else{
Log.v("print","location return null");
}
return url;
}
我使用network_provider,它总是打印“enable to locate”,然后打印“location return null”
我的代码总是返回null,这对我的代码有什么影响。 我使用AVD manerger,我也尝试GPS_PROVIDER。我使用Emulator控件在位置控制上发送Decimal。它也不起作用。
顺便说一下,我也设置了网络和GPS权限。
非常感谢您的帮助!
答案 0 :(得分:1)
我通过以下代码获取位置,但在设置中启用“使用无线网络”,即(通过设置 - 位置服务 - 使用无线网络)
public void getCurrentLocation() {
LocationManager locationManager;
String bestProvider;
// Get the location manager
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(bestProvider);
Log.e(TAG, "Latitude: "+location.getLatitude()+" , Longitude: "+location.getLongitude());
}