【问题标题】:Fetching an array of coordinates from sql server and plotting it on maps in android从 sql server 获取坐标数组并将其绘制在 android 的地图上
【发布时间】:2017-01-10 16:40:57
【问题描述】:

我正在尝试创建一个 android 应用程序,我想在其中使用 Web 服务从 SQL 服务器接收坐标数组。 假设 SQL 服务器中有一个包含 30 个坐标(纬度、经度)的数组,我希望使用 Web 服务获取这些坐标,并在我的应用程序中创建的地图上绘制标记。 请帮忙。谢谢!!

【问题讨论】:

标签: android arrays sql-server web-services google-maps


【解决方案1】:

嗨,希望对你有帮助

class MyTask extends AsyncTask<Void, Void, Void> {
    String msg = "";

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub
        try {


            URL url = new URL(
                    "url");

            InputStream isr = url.openStream();
            int i = isr.read();
            while (i != -1) {
                msg = msg + (char) i;
                i = isr.read();

            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result)

    {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
        Log.i("========>Message<=====",msg);
        try
        {
            JSONObject mainObject=new JSONObject(msg);
            //use this get Toast message of each object Toast.makeText(getActivity(), "hello1 "+mainObject, Toast.LENGTH_LONG).show();
            JSONObject maJsonObject = mainObject.getJSONObject("Response");
            //Toast.makeText(getActivity(), "hello2 "+maJsonObject, Toast.LENGTH_LONG).show();
            JSONArray jsonArray = maJsonObject.getJSONArray("Result");
            //Toast.makeText(getActivity(), "hello3 "+jsonArray, Toast.LENGTH_LONG).show();

              // Log.i("<======JSONARRAY==>",jsonArray.toString());
               for(int i=0;i<jsonArray.length();i++)
                   {
                    JSONObject subObject=jsonArray.getJSONObject(i);
                    String vocherId=subObject.getString("Voucher No");
                    tv1.setText("Voucher ID: "+vocherId);
                    String vocherAmount=subObject.getString("Voucher Amount");
                    tv2.setText(vocherAmount);
                    String store_name=subObject.getString("Store Name");
                    tv3.setText(store_name);
                    String location=subObject.getString("Location");
                    tv4.setText(location);
                    String recipient_name=subObject.getString("Recipient Name");
                    tv5.setText(recipient_name);
                    String Recipent_mobile=subObject.getString("Recipient Mobile");
                    tv6.setText(Recipent_mobile);

                    Toast.makeText(getActivity(), vocherId+"\n"+vocherAmount+"\n"+location+"\n", Toast.LENGTH_LONG).show();
                   Log.i("==vocherId==", vocherId);
                   }
              /*JSONObject  jsonRootObject = new JSONObject(msg);
              JSONArray jsonArray = jsonRootObject.optJSONArray("Response");
              for(int i=0; i < jsonArray.length(); i++)
              {
                  JSONObject jsonObject = jsonArray.getJSONObject(i);

                  String VoucherId = jsonObject.optString("voucherid").toString();
                    String Amount = jsonObject.optString("amount").toString();
                    String StoreName = jsonObject.optString("storename").toString();
                    String Location=jsonObject.optString("location").toString();
                    String Recipient_Name=jsonObject.optString("recipient_name").toString();
                    String Recepient_Mobile=jsonObject.optString("recepient_mobile").toString();

                    msg += "Node"+i+" : \n voucerId= "+ VoucherId +" \n Amount= "+ Amount +" \n StoreName= "+ StoreName +" \n Location="+Location+"\n Recipient_Name"+Recipient_Name+"\n"+Recepient_Mobile;

              }
              tv1.setText(msg);*/
        }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }


    }

}

【讨论】:

    【解决方案2】:

    我正在使用 volley 库进行网络调用,您可以进一步研究 volley 是如何在 android 中实现的

    当您点击YOUR_WEB_SERVICE_URL 作为响应时,它会在解析后发送 JSON 数组,您可以放置​​标记

    响应应该是这样的

    [
        {
            "id": "1",
            "lat": "21.3", 
            "lon": "23.55",
        }, {
            "id": "2",
            "lat": "21.3", 
            "lon": "23.55",
        }
        //...
        {
            "id": "30",
            "lat": "21.3", 
            "lon": "23.55"
        }
    ]
    
    JsonArrayRequest req = new JsonArrayRequest(YOUR_WEB_SERVICE_URL,
                new Response.Listener<JSONArray>() {
            @Override
    
            public void onResponse(JSONArray response) {
                Log.d(TAG, response.toString());
                try {
                    // Parsing json array response
                    // loop through each json object
                    jsonResponse = "";
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject location = (JSONObject)response.get(i);
                        String id =  location.getString("id");
                        String lat = location.getString("lat");
                        String lon = location.getString("lon"); //lat and lon are String you have to typecast it to double before using it
    
                        Latlon ll = new Latlon(lat, lon);    
                        placeYourMarkerFuction(ll);    
                    }
                }
                catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                                    "Error: " + e.getMessage(),
                                    Toast.LENGTH_LONG).show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                                error.getMessage(), Toast.LENGTH_SHORT).show();
    
            }
        });     
    
        // Add Request to queue       
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 2015-02-07
      • 1970-01-01
      • 1970-01-01
      • 2011-06-21
      相关资源
      最近更新 更多