GeoCoder不显示国家名称



我正在制作一个android应用程序。在我的应用程序中,我想使用我创建的LocationTracker类获得我的位置的国家名称,但我不能使用GeoCoder

获得国家名称这是我的LocationTracker类

package com.farzin.locationmap.Location;

import android.app.AlertDialog;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.provider.Settings;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.farzin.locationmap.R;
import java.util.List;
public class LocationTracker extends Service implements LocationListener {
Location location;
LocationManager locationManager;
Context context;
boolean isGPSEnabled = false;
boolean isNetworkEnabled = true;
boolean canGetLocation;
double longitude;
double latitude;
static final long MIN_DISTANCE = 10;
static final long MIN_TIME = 1000 * 60;
public LocationTracker(Context context) {
this.context = context;
getLocation();
}

public Location getLocation(){
locationManager = (LocationManager)context.getSystemService(LOCATION_SERVICE);
//gps check
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
//network check
isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isNetworkEnabled && !isGPSEnabled){
canGetLocation = false;
}else {
canGetLocation = true;

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
MIN_TIME,MIN_DISTANCE,this);
if (locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null){
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
if (isGPSEnabled){
if (location == null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
MIN_TIME,MIN_DISTANCE,this);
if (locationManager != null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null){
longitude = location.getLongitude();
latitude  = location.getLatitude();
}
}
}
}
}
return location;
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
public boolean hasLocation(){
return canGetLocation;
}
public void showAlertSettings(){
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle(R.string.warning);
alertDialog.setMessage(R.string.gps_warning_massage);
alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
alertDialog.setNeutralButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
}

这是我的主要活动,我试图创建Toast的国家名称进行测试

locationTracker = new LocationTracker(getApplicationContext());
if (locationTracker.hasLocation()){
lat = locationTracker.getLatitude();
lon = locationTracker.getLongitude();
Geocoder geocoder = new Geocoder(getApplicationContext(),new Locale("fa"));
try {
List<Address> addressList = geocoder.getFromLocation(lat, lon, 1);
Address address = addressList.get(0);
String countryName = address.getCountryName();
Toast.makeText(this, countryName, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}

}else {
locationTracker.showAlertSettings();
}

我是新手,有什么问题吗?

加上忽略我没有向用户请求权限的部分,这只是为了测试目的…

我想到了两个例子。

第一个例子:

如果设备位于谷歌拒绝来自它的请求的区域,Geocoder不会接收任何数据。(如克里米亚、古巴、所谓的顿涅茨克人民共和国和卢甘斯克人民共和国、伊朗、朝鲜和叙利亚。)

你可能想尝试使用一个VPN来改变你的IP,并尝试看看你是否在响应中获得数据。

第二个案例:

您可能想要尝试不同的位置,看看是否有数据检索的响应。如果另一个位置没有检索数据,那么这很可能意味着Geocoder实际上没有从先前提供的位置获取任何数据。

试试这个

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(requireActivity(), Locale.getDefault());
try {
addresses = geocoder.getFromLocation(lat, lng, 1);
String country = addresses.get(0).getCountryName();
}catch (IOException e)
{
e.printStackTrace();
}

相关内容

  • 没有找到相关文章

最新更新