TiandituSearch.java 2.05 KB
package com.pashanhoo.landsurvey.search;

import com.androidnetworking.AndroidNetworking;
import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.JSONObjectRequestListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class TiandituSearch {
    private static ArrayList<POISuggestion> poiSuggestionArrayList = new ArrayList<POISuggestion>();

    public static ArrayList<POISuggestion> getPoi(String name){

        String str = "{\"keyWord\":\"" + name + "\",\"level\":\"11\",\"mapBound\":" +
                "\"76.24832,30.1129,156.40458,49.97618\",\"queryType\":\"1\",\"count\":\"10\"" +
                ",\"start\":\"0\",\"queryTerminal\":\"1000\"}";

        AndroidNetworking.post("http://map.tianditu.com/query.shtml")
                .addBodyParameter("type", "query")
                .addBodyParameter("postStr", str)
                .setTag("test")
                .build()
                .getAsJSONObject(new JSONObjectRequestListener() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("pois");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject Jasonobject = jsonArray.getJSONObject(i);
                                poiSuggestionArrayList.add(new POISuggestion(Jasonobject.getString("name"),
                                        Jasonobject.getString("address"),
                                        Jasonobject.getString("lonlat")));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(ANError anError) {
                        poiSuggestionArrayList = null;
                    }
                });
        return poiSuggestionArrayList;
    }
}