首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GeoCoder不显示contryName

GeoCoder不显示contryName
EN

Stack Overflow用户
提问于 2022-07-18 09:54:29
回答 2查看 40关注 0票数 0

我想用我创建的LocationTracker类来获取我所在位置的国家名,但是我不能使用GeoCoder获得国家名

这是我的LocationTracker课

代码语言:javascript
复制
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创建用于测试的国家名称

代码语言:javascript
复制
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();
        }

我是新来的,所以这里的问题似乎是什么?

另外,忽略我没有要求用户许可的部分--这只是为了测试目的.

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-18 10:16:44

我脑子里有两件事。

第一例:

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

您可能想尝试使用VPN来更改您的IP,并尝试查看您是否在响应中获得数据。

第二个案件:

您可能希望尝试不同的位置,并查看响应中是否存在数据检索。如果另一个位置确实撤回了数据,那么这很可能意味着Geo编码器实际上没有从先前提供的位置获取任何数据。

票数 0
EN

Stack Overflow用户

发布于 2022-07-18 12:32:02

试试这个

代码语言:javascript
复制
    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();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73020381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档