getPlaceList.js 5.82 KB
var pos;
var map;
var infowindow;
var service;

function initMap() {

   map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: -34.397, lng: 150.644},
        zoom: 17
    });
    infowindow = new google.maps.InfoWindow();

    // Try HTML5 geolocation.
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            pos = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
            };

            map.setCenter(pos);

            service = new google.maps.places.PlacesService(map);

            searchPlace('bar','food');
            searchPlace('cafe','food');
            searchPlace('meal_delivery','food');
            searchPlace('meal_takeaway','food');
            searchPlace('restaurant','food');
            searchPlace('bakery','food');

            searchPlace('department_store','entertainment');
            searchPlace('movie_theater','entertainment');
            searchPlace('museum','entertainment');
            searchPlace('night_club','entertainment');
            searchPlace('shopping_mall','entertainment');
            searchPlace('zoo','entertainment');

            searchPlace('lodging','room');
        });
    }
}


function searchPlace(str, placeType) {
  switch(placeType) {
    case 'food':
      service.nearbySearch({
          location: pos,
          radius: 500,
          type: [str]
      }, callback_foods);
      break;
    case 'entertainment':
      service.nearbySearch({
          location: pos,
          radius: 500,
          type: [str]
      }, callback_entertainment);
      break;
    case 'room':
      service.nearbySearch({
          location: pos,
          radius: 500,
          type: [str]
      }, callback_rooms);
      break;
    default:
    break;
  }
}


function callback_foods(results, status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
            putDataToDB(results[i], 'food')
            createMarker_foods(results[i]);
        }
    }
}

function callback_entertainment(results, status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
            putDataToDB(results[i], 'entertainment')
            createMarker_entertainment(results[i]);
        }
    }
}

function callback_rooms(results, status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
        for (var i = 0; i < results.length; i++) {
          putDataToDB(results[i], 'room')
          createMarker_rooms(results[i]);
        }
    }
}

function createMarker_foods(place) {
    var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location,
        icon : "./icons/restaurant-15.svg",
        //fillcolor : "#FF0000"
    });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(place.name);
        infowindow.open(map, this);
    });
}

function createMarker_entertainment(place) {
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location,
        icon : "./icons/gaming-15.svg"
    });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(place.name);
        infowindow.open(map, this);
    });
}

function createMarker_rooms(place) {
    var placeLoc = place.geometry.location;
    var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location,
        icon : "./icons/lodging-15.svg"
    });

    google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(place.name);
        infowindow.open(map, this);
    });
}

function putDataToDB(result, category1) {
  const id = result['id'];
  const place_id =result['place_id'];
  const name = result['name'];
  const address = result['vicinity'];
  let category_big = category1
  const category_small = result.types[0];
  const image = "default"
  const rating = result.rating;
  const lng = result.geometry.viewport.ea.j;
  const lat =result.geometry.viewport.la.j;

  if(rating == null) {
    rating = 0;
  }
  const QueryCheck = () => {
      if (!id || !place_id || !name || !address || !category_big || !category_small || !image || !rating || !lng || !lat) {
          return Promise.reject({
              message: 'Query Error'
          })
      }
      return Promise.resolve()
  }

  // 2. SQL Start
  const SQLStart = async (pool) => {
      try {
          let data = await pool.query('INSERT INTO PLACE(ID, PLACE_ID, NAME, ADDRESS, CATEGORY_BIG, CATEGORY_SMALL, IMAGE, RATING, LNG, LAT) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);', [id, place_id, name, address, category_big, category_small, image, rating, lng, lat])
          return Promise.resolve(pool)
      } catch(err) {
          return Promise.reject(err)
      }
  }
  // 3. Response
  const Response = (rows) => {
      return res.status(200).json(rows)
  }

  // 1. Query Check
  const FindQueryCheck = () => {
      if (!id) {
          return Promise.reject({
              message: 'Query Error'
          })
      }
      else return pool
  }

  // 2. SQL Start
  const FindSQLStart = (pool) => {
      return pool.query('SELECT * FROM PLACE WHERE ID LIKE '+id.toString())
  }

  // 3. Response
  const FindResponse = (rows) => {
      return res.status(200).json(rows)
  }

  if([] == FindQueryCheck()
      .then(FindSQLStart)
      .then(FindResponse)
      .catch(err => {
          if (err) {
              return res.status(500).json(err.message || err)
          }
      }))
    {
        QueryCheck()
            .then(SQLStart)
            .then(Response)
            .catch(err => {
                if (err) {
                    return res.status(500).json(err.message || err)
                }
            })
      }
}