使用ajax和servlet在Map上插入标记

时间:2017-06-22 13:17:13

标签: mysql ajax servlets

我想通过ajax和servlet插入google地图中的任何标记,使用mysql数据库

AJAX_CODE

$(document).ready(function(){
        $('#invia').click(function(){                   

            $.ajax({                                    
            type:'post',                                
            data:{luogo: $('input.chk').prop('checked')},
            url:'FinderPlace2',                         
            dataType:"json",                            
            success:function(data){                      
                var receivedData = [];
                receivedData.push(data.Name);
                receivedData.push(data.Lat);
                receivedData.push(data.Lng);
                receivedData.push(data.Img);
                receivedData.push(data.Address);

                var marker1 = new google.maps.Marker({                                                  
                    position: new google.maps.LatLng(receivedData[1],receivedData[2]),
                    map: map,
                });
            }
            });
        });
    });

SERVLET

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try{

        response.setContentType("text/plain");

        String hotel= request.getParameter("luogo"); //uso getParameterValues, infatti ho un array contente le checkbox
        String distance = request.getParameter("raggio"); //prendo il raggio che seleziona l'utente

        String dbCat = null;
        Float dblat = null;
        Float dblng = null;

        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/PattiDiGiuseppe","root","");

            String sql = "select * from luoghi where categoria=? ";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,hotel);

            ResultSet rs = ps.executeQuery();

            JSONArray jsonArray = new JSONArray();
            while(rs.next()){  

                dbCat=rs.getString(6);
                dblat=rs.getFloat(4);
                dblng=rs.getFloat(5);

                        JSONObject json = new JSONObject();

                        json.put("cat", dbCat);
                        json.put("Lat", dblat);
                        json.put("Lng", dblng);

                        jsonArray.add(json);
            }

            response.setContentType("application/json;charset=utf-8");
             PrintWriter pw = response.getWriter(); 
             pw.print(jsonArray.toString());
             pw.close();
    }

JSP

/* <form id="form_search2" action="FinderPlace2" method="POST" >
<fieldset style="border:none">
<input id="radio1" type="checkbox" class="chk" name="luogo" value="hotel"/> Hotel
<input id="radio2" type="checkbox" class="chk" name="luogo" value="ristorante"/> Ristoranti
<input id="radio3" type="checkbox" class="chk" name="luogo" value="museo"/> Musei
</fieldset> 
<select id="raggio" name="raggio" >
        <option value="1" selected="selected"> 1 km  </option>
        <option value="5"> 5 km  </option>
        <option value="10"> 10 km </option>
</select>
<br>
<input id="invia" type="submit" name="invia" value="Cerca" />
</form>*/

0 个答案:

没有答案