Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中获取用户当前城市_Android_Reverse Geocoding - Fatal编程技术网

在Android中获取用户当前城市

在Android中获取用户当前城市,android,reverse-geocoding,Android,Reverse Geocoding,我想在Android应用程序中获取用户当前的城市名称,但根据我的一些研究(,和),这是一项相当复杂的任务,需要在网络提供商之间切换,并在一段时间后获取位置更新 我的应用程序不是基于位置的应用程序,它不需要使用该应用程序的用户的准确和最新位置。我只需要从网络提供商(或任何其他提供商)那里获取城市名称,但如果它无法获取城市名称,也可以。这只是应用程序中的一个功能,在某些情况下,如果无法获取城市名称,这并不重要 我正在使用下面的代码,但它总是将纬度和经度显示为0.0 Location location

我想在Android应用程序中获取用户当前的城市名称,但根据我的一些研究(,和),这是一项相当复杂的任务,需要在网络提供商之间切换,并在一段时间后获取位置更新

我的应用程序不是基于位置的应用程序,它不需要使用该应用程序的用户的准确和最新位置。我只需要从
网络提供商
(或任何其他提供商)那里获取城市名称,但如果它无法获取城市名称,也可以。这只是应用程序中的一个功能,在某些情况下,如果无法获取城市名称,这并不重要

我正在使用下面的代码,但它总是将
纬度
经度
显示为0.0

Location location = new Location(LocationManager.NETWORK_PROVIDER);
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
    Log.d("Lat-Lng", location.getLatitude()+","+location.getLongitude());
    // Doesn't really return anything as both latitude and longitude is 0.0.
    List<Address> address = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch(Exception e) {

}
Location Location=新位置(LocationManager.NETWORK\u PROVIDER);
Geocoder Geocoder=新的地理编码器(上下文,Locale.getDefault());
试一试{
Log.d(“Lat Lng”,location.getLatitude()+”,“+location.getLongitude());
//因为纬度和经度都是0.0,所以不会返回任何内容。
列表地址=geocoder.getFromLocation(location.getLatitude(),location.getLatitude(),1);
}捕获(例外e){
}

您在这里所做的就是创建一个新的
位置
对象,默认情况下,其纬度和经度的初始值为零

您需要做的是将该位置连接到用户的GPS信息

// Getting LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

// Creating a criteria object to retrieve provider
Criteria criteria = new Criteria();

// Getting the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);

// Getting Current Location From GPS
Location location = locationManager.getLastKnownLocation(provider);

if(location != null) {
    Log.d("Lat-Lng", location.getLatitude()+","+location.getLongitude());
    getUserGeoInfo(location.getLatitude(), location.getLongitude());
}

// 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.
        Log.d("Lat-Lng", location.getLatitude()+","+location.getLongitude());
        getUserGeoInfo(location.getLatitude(), location.getLongitude());  
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
};

// Set how often you want to request location updates and where you want to receive them
locationManager.requestLocationUpdates(provider, 20000, 0, locationListener);

// ...

void getUserGeoInfo(double lat, double lon) {
    Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
    if (Geocoder.isPresent()) {
        List<Address> addresses = geoCoder.getFromLocation(lat, lon, 1);
        if (addresses.size() > 0) {
            // obtain all information from addresses.get(0)
        }
    }
}
//从系统服务位置\u服务获取LocationManager对象
LocationManager LocationManager=(LocationManager)getSystemService(LOCATION\u服务);
//创建条件对象以检索提供程序
标准=新标准();
//获取最佳提供商的名称
字符串提供程序=locationManager.getBestProvider(条件为true);
//从GPS获取当前位置
Location Location=locationManager.getLastKnownLocation(提供者);
如果(位置!=null){
Log.d(“Lat Lng”,location.getLatitude()+”,“+location.getLongitude());
getUserGeoInfo(location.getLatitude(),location.getLatitude());
}
//定义响应位置更新的侦听器
LocationListener LocationListener=新LocationListener(){
已更改位置上的公共无效(位置){
//当网络位置提供程序找到新位置时调用。
Log.d(“Lat Lng”,location.getLatitude()+”,“+location.getLongitude());
getUserGeoInfo(location.getLatitude(),location.getLatitude());
}
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){}
公共无效onProviderEnabled(字符串提供程序){}
公共无效onProviderDisabled(字符串提供程序){}
};
//设置请求位置更新的频率以及接收位置
locationManager.RequestLocationUpdate(提供程序,20000,0,locationListener);
// ...
void getUserGeoInfo(双lat,双lon){
Geocoder Geocoder=新的地理编码器(上下文,Locale.getDefault());
if(Geocoder.isPresent()){
名单


就Android上获取用户位置的一般技术而言,

听起来很简单。我如何实现
您的位置\u侦听器
?好吧,假设您有一个
活动
声明为
类位置活动扩展活动
,其中包含所有这些代码。只需将
位置活动扩展活动实现LocationListener
,您将可以访问
onLocationChanged
方法。这意味着在
requestLocationUpdates
方法中,第三个参数将是
This
。请在我提供的链接上阅读有关此方法的更多信息,它有很多功能it@BasitSaeed请阅读此链接。@nem我可以创建一个单独的链接吗
class
并在函数中使用代码并将其返回到活动?我只需要
class
中实现
,对吗?是的。但是请记住,我的答案只显示了实现这一点的多种方法中的一种。请查看android的谷歌文档,了解这些方法和方法界面以查看您拥有的所有选项。