Baidu map address prompt api calls instances (attached the complete code)

Recently doing the project on-line recovery resources, which requires the user to fill in the address, so I intend to use third-party interfaces Baidu map, input prompts. Using java language background. First look at a rendering of it.
Write pictures described here
Write pictures described here
Look at the front-end code below:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
    <script type="text/javascript" src="js/jquery-3.2.1.min.js" ></script>
    <script>
    $(document).ready(function(){
          $("#query").keydown(function(){
              $(".addresstop").empty();
           $.post("Servletapi",
          {
            region:$("#hidden1_region").val(),
            query:$("#query").val()
          },
          function(result){

              var jsobject = JSON.parse(result);

              for (var i = 0; i < jsobject.result.length; i++) {

                    $(".addresstop").append("<div class='addressli'>"
                            +"<div class='addressli1'>"+jsobject.result[i].name+"</div>"
                            +"<div class='addressli2'>"+jsobject.result[i].city+""+jsobject.result[i].district+"</div></div>");
            }


          });
          });
    });
    </script>

<input type="text" id="query"><input type="text" value="北京" id="hidden1_region">
<div class="addresstop">

</div>
</body>
</html>

Background servlet

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        Url url = new Url();
        String query = URLEncoder.encode(request.getParameter("query"), "UTF-8");
        String region = URLEncoder.encode(request.getParameter("region"), "UTF-8");

        String requestUrl =  "http://api.map.baidu.com/place/v2/suggestion?region=REGION&query=QUERY&city_limit=true&output=json&ak=你的ak";

        requestUrl = requestUrl.replace("QUERY",query).replace("REGION",region);

        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST","");
        System.out.println(jsonObject);
         PrintWriter out = response.getWriter();
            out.print(jsonObject);
            out.close();

    }

Tools:

package until;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.alibaba.fastjson.JSONObject;
public class CommonUtil { 

    /*
     * 发送https请求
     * @param requestUrl 请求地址
     * @param requestMethod 请求方法
     * @param JSONObject 解析获取的json数据 
     * 
     * */
    public static JSONObject httpsRequest(String requestUrl,String requestMethod,String outputStr){
        JSONObject jsonObject = null;
        //创建SSLContext对象
         //--
        try {
            URL url = new URL(requestUrl);
            HttpURLConnection conn;
            try {
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setUseCaches(true);
                //设置请求方式
                conn.setRequestMethod(requestMethod);
                if(null!=outputStr){
                    OutputStream outputStream = conn.getOutputStream();
                    outputStream.write(outputStr.getBytes("UTF-8"));
                    outputStream.close();           
                }
                //从输入流读取返回内容
                InputStream inputStream = conn.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
                BufferedReader bufferedReadr = new BufferedReader(inputStreamReader);
                String str = null;
                StringBuffer buffer = new StringBuffer();
                while((str = bufferedReadr.readLine())!=null){
                    buffer.append(str);
                }
                bufferedReadr.close();
                inputStream.close();
                inputStreamReader.close();
                inputStream = null;
                conn.disconnect();
                jsonObject = JSONObject.parseObject(buffer.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
        return jsonObject;

    }

}

jquery find that package is better I do not say
fastjson that package can be downloaded from my another blog Download Links

The whole idea is through the address jq keydown methods continue to transmit the input to the background, and then query the data.
There is a bit confusing. Json string is reached when the leading end of the data extraction. We can own to try to try, it is also not difficult.

I've passed up a project package, you can download it.
But note that ak fill in your code.
Download csdn

Blue played cloud download

Published 48 original articles · won praise 34 · Views 230,000 +

Guess you like

Origin blog.csdn.net/lzx159951/article/details/79902888