首页 文章

如何从JSON Web服务获取纬度和经度?

提问于
浏览
-3

我在 String format 转换 Country NameLongitude and Latitude .

为此,我使用以下 JSON web service .

https://maps.googleapis.com/maps/api/geocode/json?address=India&sensor=true

我不知道如何从那个JSON获得 Longitude and Latitude .

help me to get Longitude and Latitude from the above web service.

我曾尝试过下面的代码,但我不确定它是否正确:

try 
{
                    Log.v("MAPS","Lang-Lat::1");
                    String myjsonstring = stringBuffer.toString();
                    Log.v("My JSON STRING",""+myjsonstring);
                    Log.v("MAPS","Lang-Lat::2");    
                    JSONArray jsonArray = new JSONArray(myjsonstring);

                    Log.v("MAPS","Lang-Lat::3");
                    JSONObject jsonObj = null;

                    String status = jsonObj.getString("status");
                    Log.v("MAPS","Lang-Lat::4");
                    if (status.equals("OK")) 
                    {
                        Log.v("Log Receive","Json String:::"+myjsonstring);                 

                        jsonObj = jsonArray.getJSONObject(0);

                        JSONObject JSONObject2=jsonObj.getJSONObject("geometry");

                        JSONObject JSONObject3=JSONObject2.getJSONObject("bounds");

                        JSONObject JSONObject4=JSONObject3.getJSONObject("northeast");

                        for(int i=0; i<JSONObject4.length();i++)
                        {
                            lang = JSONObject4.getDouble("lng");
                            lat=JSONObject4.getDouble("lat");

                            Log.v("Lang:",""+lang);
                            Log.v("Lat",""+lat);

                        }
                    }
                } 
                catch (Exception e) 
                {
                    Log.v("Map_View::","Call JSON Exception in Getting Map--->"+e.toString());
                    e.printStackTrace();
                }

任何帮助将不胜感激 .

先感谢您 .

1 回答

  • 1
    public class ParseLatitudeLng {
    
        public static JSONObject getLocationInfo(String address) {
            StringBuilder stringBuilder = new StringBuilder();
            try {
    
            address = address.replaceAll(" ","%20");    
    
            HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
            HttpClient client = new DefaultHttpClient();
            HttpResponse response;
            stringBuilder = new StringBuilder();
    
    
                response = client.execute(httppost);
                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) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
            return jsonObject;
        }
    
    }
    

    / ************ 从地址获取经度和经度 ************* /

    public class GetLatLngAsyncTask extends AsyncTask<ArrayList<String>, Void, String>{
    
            JSONObject jObject;
            JSONObject places = null;
            String lat;
    
    
            protected void onPreExecute() {
    
            }
            @Override
            protected String doInBackground(ArrayList<String>... params) {
                //ParseLatitudeLng placeJsonParser = new ParseLatitudeLng();
                ArrayList<String> inSync=params[0];
    
                double lng1 = 0,lat1 = 0;
    
                try{
                        lng1=lat1=0;
    
    
                        places = ParseLatitudeLng.getLocationInfo(YOUR_ADDRESS);
                        lng1 = ((JSONArray)places.get("results")).getJSONObject(0)
                                .getJSONObject("geometry").getJSONObject("location")
                                .getDouble("lng");
    
                        lat1 = ((JSONArray)places.get("results")).getJSONObject(0)
                                .getJSONObject("geometry").getJSONObject("location")
                                .getDouble("lat");
    
    
    
                    }catch(Exception e){
                        Log.d("Exception",e.toString());
                    }
    
    
                return lat;
            }
    
            @Override
            protected void onPostExecute(String result) {
                // TODO Auto-generated method stub
                super.onPostExecute(result);
    
    
            }
    
        }
    

    调用Asynctask获取纬度和经度

相关问题