【问题标题】:Read Lan ,Long from Json URL and assign the lat and long to map 's markers?从 Json URL 读取 Lan ,Long 并将 lat 和 long 分配给 map 的标记?
【发布时间】:2017-12-03 03:15:54
【问题描述】:

我是 android 新手,我正在尝试从 URL 读取 json 文件,并获取 lat 和 long 以及 将值关联到标记并在地图中显示,我的问题是如何在阅读 json 的内容后获得 lan,long ? 这是 json url 数据 URL上的json数据

{"properties":{"1":{"name":"Villa For 
Sale","lat":"35.2474962142","lng":"-91.3480163353","price":"$100,000","image1":"http:\/\/wpl28.realtyna.com\/theme28\/wp-content\/uploads\/WPL\/91\/thimg_12_800x420.jpg","image2":"http:\/\/wpl28.realtyna.com\/theme28\/wp-content\/uploads\/WPL\/85\/thimg_2_800x420.jpg"},"2":{"name":"Apartment For Rent","lat":"35.9542249162","lng":"-101.21893164","price":"$40,000","image1":"ht

在 android 中,我想获取 lat、long 并将每个值分配给市场 地图

      //this method should get json data from onPostExecute(String s)
      public void the_received_data(String received_data){

       }

      class Read_JSON extends AsyncTask<String ,String ,String >{
       MainActivity mainActivity;
       // class to handle reading json file
        @Override
        protected String doInBackground(String... params) {
        HttpURLConnection connection =null;
        BufferedReader data =null;
        InputStream json_data;
        try {
           URL url = new URL(params[0]);
            connection =(HttpURLConnection)url.openConnection();
            connection.connect();
            //holding the data into stream
            json_data =connection.getInputStream();
            data = new BufferedReader(new InputStreamReader(json_data));
            StringBuffer stringBuffer = new StringBuffer();
            //read the data from the bufferedredaer line by line
            String line ="";
            while((line =data.readLine()) != null){
                stringBuffer.append(line);
            }
            return stringBuffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            if(connection !=null){connection.disconnect();}
            try {
                if(data != null){
                    data.close();}
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
      return null; }

       @Override
       protected void onPostExecute(String s) {
         super.onPostExecute(s);
         mainActivity.the_received_data(s);
     }
 }

【问题讨论】:

    标签: android json google-maps


    【解决方案1】:

    获得字符串响应后,只需将其解析为 JSONObject,如下例所示,然后您就可以获得纬度和经度。

    JSONObject jsonObj = new JSONObject("{"name":"myName","address":"myAddress"}");
    String my_name = jsonObj.getString("myName");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多