【问题标题】:Android Geocoder getFromLocationName always returns nullAndroid Geocoder getFromLocationName 始终返回 null
【发布时间】:2023-03-13 14:23:01
【问题描述】:

我一直在尝试对字符串进行地理编码以获取其坐标,但我的程序总是崩溃,因为当我尝试使用 getFromLocationName() 时,它会返回 null。我一直试图解决这个问题几个小时,但没有任何效果。这是我的代码

public class MainActivity extends Activity {

    private GoogleMap mMap;

    List<Address> addresses;
    MarkerOptions miami;
    String myLocation = "Miami,Florida";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (mMap == null) {
            mMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();
        }

        if (mMap != null) {

            Geocoder geocoder = new Geocoder(this);


            double latitude = 0;
            double longitude = 0;

            while(addresses==null){
            try {
                addresses = geocoder.getFromLocationName(myLocation, 1);

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            }
            Address address = addresses.get(0);
            if (addresses.size() > 0) {
                latitude = address.getLatitude();
                longitude = address.getLongitude();
            }
            LatLng City = new LatLng(latitude, longitude);

            miami = new MarkerOptions().position(City).title("Miami");

            mMap.addMarker(miami);

            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15));

        }
}

【问题讨论】:

  • 有人要回答这个问题吗?

标签: java android eclipse android-mapview google-maps-android-api-2


【解决方案1】:

Geocoder 并不总是返回值。您可以尝试在 for 循环中发送 3 次请求。我应该至少能回来一次。如果不是,那么它们可能是连接问题,也可能是服务器未回复您的请求等其他问题。试试看这些线程:

Geocoder doesn't always return a valuegeocoder.getFromLocationName returns only null

更新:

我也有一个 while 循环,但我曾经最多尝试 10 次。有时,即使连接到互联网,它也不会返回任何内容。然后,我每次都使用this更可靠的方式获取地址:

public JSONObject getLocationInfo() {

        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            response = client.execute(httpGet);
            HttpEntity entity = response.getEntity();
            InputStream stream = entity.getContent();
            int b;
            while ((b = stream.read()) != -1) {
                stringBuilder.append((char) b);
            }
        } catch (ClientProtocolException e) {
            } catch (IOException e) {
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
    

我这样称呼它:

JSONObject ret = getLocationInfo(); 
JSONObject location;
String location_string;
try {
    location = ret.getJSONArray("results").getJSONObject(0);
    location_string = location.getString("formatted_address");
    Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
    e1.printStackTrace();

}

希望这会有所帮助。我也厌倦了依赖地理编码器。这对我有用。 如果将 URL 替换为纬度和经度坐标并在 Web 浏览器中查看返回的 JSON 对象。你会看到刚刚发生了什么。

【讨论】:

  • 但我有一个 while 循环来确保我获得地理编码器的值,并且它会一直循环下去。
  • 查看我的更新答案。希望它能解决您想要实现的目标。
  • 我面对同样的problem。我发现设备上的NetworkLocator 由于某种原因(不在我们手中)已经停止工作或被杀死。您实际上可以通过重新启动设备来确认这一点。它现在必须工作(因为NetworkLocator re-boots too)。
  • 有3个案例。 1。您不会第 1 次获得位置,然后您尝试循环并获得第 4 次/第 5 次等。2。无论您循环多少次,您都不会获得任何位置详细信息,因此您假设服务可能暂时关闭并使用上述解决方案3。就像我在上面的评论中解释的那样,除非您重新启动设备,否则它将永远无法工作,这意味着我将始终经历第 1 步和第 2 步,每次可能需要大约 2-5 秒。没有人愿意为了获得像addressline 这样的单一数据而等待这么长时间。案例 3 有更好的解决方案吗?
猜你喜欢
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
  • 2016-05-31
  • 2015-08-15
相关资源
最近更新 更多