p_loc.html 7.58 KB
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=<sc>, initial-scale=1.0">
    <link rel="Stylesheet" href="../../assets/css/p_loc.css">
    <title>Photo classification by location</title>
    <style></style>      
</head>
<body>
    <div class='all'>
        <div class='top'>
            <div class='title'>
                <img src="../../assets/img/photo_loc.png" onclick="history.go(-1)" class="float_left" title="Back to photo menu">
            </div>
        </div>
        <div class='content'>
            <div id="main">
                <h1 style="margin-top:250px; color:dimgray; text-align: center;">지도의 마크를 클릭해 해당 사진과 인근 위치에서 찍힌 사진을 확인하세요.</h1>
            </div>
            <div id="explanation">

            </div>
            <div id="map"></div> 
        </div>
    </div>
     

    <script type=text/javascript>

    var mysql = require('mysql');
    //mysql 연동
    var connection = mysql.createConnection({
        user:'root',
        password:'1234',
        database : 'dataextraction'
    })

    connection.connect();
    var list;
    function initMap() {
        //위치 데이터 존재하는 모든 사진 select
        connection.query('SELECT * FROM photo where latitude is not null', function(error, results, fields){
        if(error){
        }
        else{
            list = results;
             var first_p = {lat: Number(list[0].latitude), lng: Number(list[0].longitude)};
             //첫 사진을 중심으로 맵 생성
            var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 8,
            center: first_p
            });
  
            list.forEach(element => {
                var latlng = {lat : Number(element.latitude), lng : Number(element.longitude)};
                
                var time = new Date(Number(element.date_added)*1000);
                var path = new String();
                path = element.path;
                var num = path.indexOf("DCIM");
                var folder_path = new String();
                folder_path = "../../photos/" + path.substring(num);
                var address = new String();
                var geocoder = new google.maps.Geocoder;
                //lattide, longitude로 주소 획득(변환)
                geocoder.geocode({'location' : latlng}, function(results, status){
                    var marker = new google.maps.Marker({
                        position: latlng,
                        map: map,
                        title: element.display_name
                    });
                    //마커 생성
                    if(status == 'OK'){
                        //주소 획득 시 
                        if(results[0]){
                            address = results[0].formatted_address; 
                            var contentString = '<div id="content">'+
                                '<div id="siteNotice">'+
                                '</div>'+
                                '<h1 id="firstHeading" class="firstHeading">'+element.title+'</h1>'+
                                '<div id="bodyContent">'+
                                '<p><b>'+element.display_name+'</b></p><p><b>'+time+'</b </p><p>'+
                                results[0].formatted_address+'</p></div>'+
                                '</div>';  
                            var infowindow = new google.maps.InfoWindow({
                                content: contentString,
                                maxWidth: 300
                            });
                            marker.addListener('click', function() {
                                infowindow.open(map, marker);
                                p_group(element);
                            }); 
                        }
                        else{
                            var contentString = '<div id="content">'+
                                '<div id="siteNotice">'+
                                '</div>'+
                                '<h1 id="firstHeading" class="firstHeading">'+element.title+'</h1>'+
                                '<div id="bodyContent">'+
                                '<p><b>'+element.display_name+'</b></p><p><b>'+time+'</b </p><p>('+
                                latlng.lat+','+latlng.lng+'</p></div>'+
                                '</div>';  
                            var infowindow = new google.maps.InfoWindow({
                                content: contentString,
                                maxWidth: 300
                            });
                            marker.addListener('click', function() {
                                infowindow.open(map, marker);
                                p_group(element);
                            }); 
                        }
                    }
                    else{
                        var contentString = '<div id="content">'+
                            '<div id="siteNotice">'+
                            '</div>'+
                            '<h1 id="firstHeading" class="firstHeading">'+element.title+'</h1>'+
                            '<div id="bodyContent">'+
                            '<p><b>'+element.display_name+'</b></p><p><b>'+time+'</b </p><p>('+
                            latlng.lat+','+latlng.lng+')</p></div>'+
                            '</div>';  
                        var infowindow = new google.maps.InfoWindow({
                            content: contentString,
                            maxWidth: 300
                        });
                        //마커 클릭 시 사진 그룹화 함수 실행
                        marker.addListener('click', function() {
                            infowindow.open(map, marker);
                            p_group(element);
                        }); 
                    }
                });
                                        
            });
            }
        })    
    }
    //사진 그룹화
    function p_group(photo){
        const main = document.getElementById('main');
        var path = new String();
        path = photo.path;
        var num = path.indexOf("DCIM");
        var folder_path = new String();
        folder_path = "../../photos/" + path.substring(num);
        var myHTML = '';
        myHTML += '<div class="p"><img src="'+folder_path+'"></div>';
        list.forEach(element=>{
            if(element.title != photo.title){
                //위도 경도 차이를 km단위로 변환
                var hor = photo.latitude - Number(element.latitude);
                hor *=111;
                var ver = photo.longitude - Number(element.longitude);
                ver *=88;
                //선택된 사진 과의 거리 5km이내일 경우
                if(((hor*hor) + (ver*ver))<=25){
                    main.innerHTML = "<p>Bye</p>"
                    var path2 = new String();
                    path2 = element.path;
                    var num2 = path2.indexOf("DCIM");
                    var folder_path2 = new String();
                    folder_path2 = "../../photos/" + path2.substring(num2);
                    myHTML += '<div class="ex"><img id="ex" src="'+folder_path2+'"></div>';
                    
                }
            }
            
        });
        main.innerHTML = myHTML;
        document.getElementById('explanation').innerHTML = "<h1>[ "+photo.display_name+" ]</h1><h1>5km 인근 사진 모음</h1>"
    }
      </script>   
<script async defer 
src="http://maps.google.com/maps/api/js?key=API_KEY&callback=initMap&sensor=true">//API_KEY 생성한 API_KEY 입력
</script>
</body>
</html>