Android Geocoder getFromLocationName 总是返回 null [英] Android Geocoder getFromLocationName always returns null

查看:35
本文介绍了Android Geocoder getFromLocationName 总是返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been trying to geocode a string to get its coordinates but my program always crashes because when ever I try to use getFromLocationName() it returns null. I have been trying to fix this for hours but nothing is working. Here is my code

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));

        }
}

推荐答案

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

Geocoder doesn't always return a value. You can try to send a request 3 times in a for loop. I should be able to return atleast once. If not then, their might be a connection issue or can be other issues like server dis not reply to your request. Try and see these threads:

Geocoder 并不总是返回值geocoder.getFromLocationName 只返回空值

更新:

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

I had a while loop as well but I used to try it maximum for 10 times. Sometimes, it never returned anything even if it was connected t internet. Then, I used this much more reliable way to get the address everytime:

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 对象.你会看到刚刚发生了什么.

Hope this helps. I was also tired of relying on geocoder. This worked for me. If you replace the URL with the lat and longitude coordinates and see the returned JSON object in a web browser. You'll see what just happened.

这篇关于Android Geocoder getFromLocationName 总是返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆