【问题标题】:getting 0.0 as latitude and longitude得到 0.0 作为纬度和经度
【发布时间】:2014-06-16 07:45:32
【问题描述】:

我正在尝试对地址进行地理编码并获取坐标。我不需要它显示在地图上,根据我的搜索,它不需要 API 密钥。在运行我的代码时,我得到了NullPointerException,所以我将服务器连接代码放在一个单独的线程中,但现在我得到 0.0、0.0 作为我的纬度和经度。 那么,我在哪里犯错了?

这是我的代码:

public class MainActivity extends Activity {

    private TextView myAddress;
    private EditText addressET;
    private Button okButton;

    GeocodeResponse gr;
    Button hiButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myAddress = (TextView) findViewById(R.id.myAdrs);
        addressET = (EditText) findViewById(R.id.addressEditText);
        okButton = (Button) findViewById(R.id.OK);

        okButton.setOnClickListener(new OnClickListener() {

            double lattitude;
            double longitude;
            private String address;

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                address = addressET.getText().toString();
                if (address == null) {
                    Toast.makeText(getApplicationContext(), "Enter address",
                            Toast.LENGTH_SHORT).show();
                } else {
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            gr.gecodeResponse(address);
                        }
                    });

                    if(lattitude!=0)lattitude = gr.lattitude();
                    if(longitude!=0)longitude = gr.longitude();

                    myAddress.setText(lattitude + " " + longitude);
                }
            }
        });
    }
}

GeocodeResponse.java:

public class GeocodeResponse {

     double lng;
     double lat;


    public void gecodeResponse(String address) {
        // TODO Auto-generated method stub
        String uri = "http://maps.google.com/maps/api/geocode/json?address="+address+"&sensor=false";
        HttpGet httpGet = new HttpGet(uri);
        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) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());

            lng = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
                    .getJSONObject("geometry").getJSONObject("location")
                    .getDouble("lng");

            lat = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
                    .getJSONObject("geometry").getJSONObject("location")
                    .getDouble("lat");

             Log.e("lattitude"+"", lat+"");
             Log.e("longitude", lng+""); 
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    double lattitude() {
        return lat;

    }

    double longitude() {
        return lng;
    }

}

json 响应:

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA

【问题讨论】:

  • 在此处发布您的 logcat 错误消息。
  • 仔细分析数据一步一步。表示每次记录数据并检查结果。很可能是解析错误
  • @MdAbdulGafur logcat 没有错误。
  • @DeadlyDroid 你能看一下json响应吗,我找不到任何错误。
  • @kiran 你是对的,解析没有问题。记录 stringBuilder 并检查它是否具有正确的值。您可以使用字符串 str=EntityUtils.toString(entity)。用于将其转换为字符串

标签: android geolocation


【解决方案1】:

您未来的问题将是您没有检查收到的响应类型。是天台还是别的什么。我不知道这对您是否重要,但有时您可以获得非常远的结果。

我正在像下面那样做。遍历整个结果(如果您有任何结果,这会为调试提供专业帮助),检查结果的类型(请参阅 gmaps 文档),例如将其添加到全局列表中。

if (result!=null){
    List<Integer> rooftopsIds = new ArrayList<Integer>();
    JSONArray result = ((JSONArray) jsonObj.get("results"));
    int length = result.length();
    for (int i = 0; i < length; i++) {
        String type = result.getJSONObject(i).getJSONObject("geometry").getString("location_type");
        if (type.equalsIgnoreCase("ROOFTOP")) {
            //probably the most accurate response
            rooftopsIds.add(i); 
        }
        if (type.equalsIgnoreCase("RANGE_INTERPOLATED")) {
            //less accurate response
        }
        if (type.equalsIgnoreCase("GEOMETRIC_CENTER")) {
            //the worst result as for me :)
        }
    }
}

然后我可以检查我是否得到了每种类型的多个结果。这很重要,因为我遇到过一个地址有两个屋顶的情况,不同之处在于另一个地址在附近的城市,而不是用户提供的一个。所以我的建议是检查一下。

您还应该在 gmaps 网址中添加“&language=en-EN”(或您的语言)

您是否检查过您的 JSON 结果对象,它包含什么?您可以将 gmaps url 粘贴到 Firefox 中,并附上您要进行地理编码的地址,您将得到打印结果。

收到结果后最好在设备上使用断点和调试(您可以轻松查看整个 JSON 对象。

【讨论】:

    【解决方案2】:

    基兰,

    浏览代码,感觉还可以,问题是你在哪里测试过应用?

    • 您必须使用DDMS 分别将经纬度发送到模拟器
    • 否则,您必须通过手机调试应用程序(首选)

    我希望你理解这个问题:)

    编辑:

    JSONObject jsonObject = new JSONObject();
        try {
            jsonObject = new JSONObject(stringBuilder.toString());
    
            lng = ((JSONArray) jsonObject.get("results")).getJSONObject(0)
                    .getJSONObject("geometry").getJSONObject("location")
                    .getDouble("lng");
    
            lat = ((JSONArray) jsonObject.get("results")).getJSONObject(1)
                    .getJSONObject("geometry").getJSONObject("location")
                    .getDouble("lat");
    

    试试这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多