从sql服务器获取坐标数组并将其绘制在android的地图上



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

嗨,希望这对你有帮助

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

    }
}

我正在使用凌空库进行网络调用,您可以进一步研究如何在 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       
}

最新更新