GoogleMap 요약

4796 단어 경위도googlemap
백엔드 코드, 주소가 경위도로 변환
public static Map getCoordinate(String addr) {
    Map latlenMap = new HashMap();
    String address = null;
    try {
        address = URLEncoder.encode(addr,"utf-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    //String key = "AIzaSyDR7jROD4G0n2O9SC3ljlr7cF7wz-N4_4Y"; //google Map Key
    //String url = "http://maps.google.com/maps/geo?q=%s&output=%s&key=%s",address,key);
    String url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + addr;

    URL googleMapURL = null;
    HttpURLConnection httpConn = null;
    //    
    try {
       googleMapURL = new URL(url);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }

    try {
        httpConn = (HttpURLConnection)googleMapURL.openConnection(new Proxy(Proxy.Type.SOCKS,new InetSocketAddress("127.0.0.1",1080)));
        httpConn.setRequestMethod("GET"); //      
        googleMapURL.openConnection().setDoOutput(true);//    URLConnection   doOutput     
        if (httpConn != null) {
            InputStreamReader insr = new InputStreamReader(httpConn.getInputStream(),"utf-8");
            BufferedReader br = new BufferedReader(insr);
            StringBuffer sb = new StringBuffer();
            String temp;
            while ((temp = br.readLine()) != null) {
                temp = temp.trim();
                if (temp !=null && temp.length()>0) {
                    sb.append(temp);
                }
            }
            br.close();
            insr.close();

            JSONObject llInfo = new JSONObject(sb.toString());

            String status = llInfo.getString("status");
            if ("OK".equals(status)) {
                JSONArray results = llInfo.getJSONArray("results");

                for (int i = 0; i<results.length(); i++) {
                    JSONObject result = results.getJSONObject(i);


                    //        
                    String locAddress = result.getJSONArray("address_components").getJSONObject(0).getString("long_name");
                    //   loc  ,     
                    JSONObject loc= result.getJSONObject("geometry").getJSONObject("location");
                    double y = loc.getDouble("lat");
                    double x = loc.getDouble("lng");

                    double[] latLng = new double[2];
                    latLng[0] = x;
                    latLng[1] = y;

                    latlenMap.put(locAddress,latLng);
                }

            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        //System.out.println("       ");
        log.error("       ");
    }
    return latlenMap;
}

프런트엔드 해결
<!-- lang: js -->
$.get("/item/getLatLen", {address: address}, function (data) {
                var datas = data.split(";")

                //          
                x = datas[0].split(",")[1];
                y = datas[0].split(",")[2];

                var mapOptions = {
                    center: new google.maps.LatLng(y, x),
                    zoom: 6,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                var map = new google.maps.Map(document.getElementById("map_canvas"),
                        mapOptions);

                for(var i = 0; i<datas.length-1; i++){
                    var latlens = datas[i].split(",")
                    var localAddr = latlens[0];
                    var position = new google.maps.LatLng(latlens[2],latlens[1]);

                    var marker = new google.maps.Marker({
                        position: position,
                        map: map,
                        localAddr: localAddr,
                        lat: latlens[1],
                        len: latlens[2]
                    })

                    showMessage(marker);
                }
                //     marker        
                function showMessage(marker) {
                    var infowindow = new google.maps.InfoWindow({
                        content: marker.get('localAddr')
                    });
                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.open(marker.get('map'), marker);
                        //         
                        x = marker.get('lat');
                        y = marker.get('len');
                        $(".x").val(x);
                        $(".y").val(y);
                    });
                }

좋은 웹페이지 즐겨찾기