๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
IT/Java - Spring Boot

[Spring Boot] ์นด์นด์˜ค ์ง€๋„ api ํ™œ์šฉํ•˜์—ฌ ์ง€๋„ ์ถœ๋ ฅํ•˜๊ธฐ

by ITyranno 2023. 11. 1.
728x90
๋ฐ˜์‘ํ˜•

 

 

 

 

ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์„ธ๊ณ„๋ฅผ ํƒ๊ตฌํ•ฉ์‹œ๋‹ค.

 

 

 

 

Spring Boot์—์„œ ์นด์นด์˜ค ์ง€๋„ api๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํŽ˜์ด์ง€์— ์ง€๋„๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

๊ตฌํ˜„ ํ™”๋ฉด

 

 

 

์ถฉ์ „์†Œ ์ฐพ๊ธฐ๋กœ ๋“ค์–ด์˜ค๋ฉด ์™ผ์ชฝ ์‚ฌ์ด๋“œ๋ฐ”๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง€์ •ํ•œ ๋ฐ˜๊ฒฝ ์ด๋‚ด์˜ ์ „๊ธฐ์ฐจ ์ถฉ์ „์†Œ ์œ„์น˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ ,  ์ „๊ธฐ์ฐจ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ํ•ญ๋ชฉ์„ ์„ ํƒํ•˜์—ฌ ์ „๊ธฐ์ฐจ ์œ„์น˜๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์นดํ…Œ๊ณ ๋ฆฌ๋Š” ์ง€์—ญ๊ตฌ, ์šด์˜๊ธฐ๊ด€, ์ถฉ์ „๊ธฐํƒ€์ž…, ์‹œ์„ค๊ตฌ๋ถ„์ด ์žˆ์œผ๋ฉฐ ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„๋กœ ์ง€๋„์— ๋‚˜ํƒ€๋‚˜๋Š” ํ•€ ์ด๋ฏธ์ง€๊ฐ€ ๋‹ค๋ฅด๋ฏ€๋กœ ๋™์‹œ์— ์—ฌ๋Ÿฌ ์ •๋ณด๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

์ถฉ์ „์†Œ ์ฐพ๊ธฐ๋กœ ๋“ค์–ด์˜ค๋ฉด ์™ผ์ชฝ ์‚ฌ์ด๋“œ๋ฐ”๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง€์ •ํ•œ ๋ฐ˜๊ฒฝ ์ด๋‚ด์˜ ์ „๊ธฐ์ฐจ ์ถฉ์ „์†Œ ์œ„์น˜๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ ,  ์ „๊ธฐ์ฐจ ์นดํ…Œ๊ณ ๋ฆฌ๋ณ„๋กœ ํ•ญ๋ชฉ์„ ์„ ํƒํ•˜์—ฌ ์ „๊ธฐ์ฐจ ์œ„์น˜๋ฅผ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์นดํ…Œ๊ณ ๋ฆฌ๋Š” ์ง€์—ญ๊ตฌ, ์šด์˜๊ธฐ๊ด€, ์ถฉ์ „๊ธฐํƒ€์ž…, ์‹œ์„ค๊ตฌ๋ถ„์ด ์žˆ์œผ๋ฉฐ ์นดํ…Œ๊ณ ๋ฆฌ ๋ณ„๋กœ ์ง€๋„์— ๋‚˜ํƒ€๋‚˜๋Š” ํ•€ ์ด๋ฏธ์ง€๊ฐ€ ๋‹ค๋ฅด๋ฏ€๋กœ ๋™์‹œ์— ์—ฌ๋Ÿฌ ์ •๋ณด๋ฅผ ์‹๋ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋Š” h2 database ํ™œ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

 

https://www.h2database.com/html/main.html

 

H2 Database Engine

H2 Database Engine Welcome to H2, the Java SQL database. The main features of H2 are: Very fast, open source, JDBC API Embedded and server modes; in-memory databases Browser based Console application Small footprint: around 2.5 MB jar file size     Supp

www.h2database.com

 

 

 

์ž‘์„ฑ ์ฝ”๋“œ

 

 

MapController.java

 

package com.example.board.controller;

import java.time.LocalDate;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.board.model.Station;
import com.example.board.model.Store;
import com.example.board.model.User;
import com.example.board.model.Restaurant;
import com.example.board.model.Coffee;
import com.example.board.model.Coupon;
import com.example.board.repository.StationRepository;
import com.example.board.repository.StoreRepository;
import com.example.board.repository.CoffeeRepository;
import com.example.board.repository.CouponRepository;
import com.example.board.repository.RestaurantRepository;

@Controller
public class MapController {
    
    @Autowired
    StationRepository stationRepository;
    @Autowired
    CoffeeRepository coffeeRepository;
    @Autowired
    StoreRepository storeRepository;
    @Autowired
    RestaurantRepository restaurantRepository;
    @Autowired
    CouponRepository couponRepository;
    @Autowired
    HttpSession session;
    

    @GetMapping("/tetest/data")
    @ResponseBody
    public List<Station> tetestData(){       
       List<Station> excels = stationRepository.findAll();
        return excels;
    }

    @GetMapping("/tetest")
    public String tetest(Model model){
       
       List<Restaurant> excels = restaurantRepository.findAll();
       model.addAttribute("excels", excels);
        return "tetest";
    }
    
   @PostMapping("/tetest")
   public String tetestPost(@ModelAttribute Station excel) {
    //   stationRepository.save(excel);
      return "redirect:/tetest";
   }

   @GetMapping("/map")
   public String map(Model model) {
      List<Station> excel14s = stationRepository.findAll();
      model.addAttribute("excel14s", excel14s);
      return "/map/map";
   }

   @PostMapping("/map")
   public String mapPost(@ModelAttribute Station excel14) {
      stationRepository.save(excel14);
      return "redirect:map/map";
   }
    @GetMapping("/coffee/data")
    @ResponseBody
    public List<Coffee> coffeeData(){       
       List<Coffee> shops = coffeeRepository.findAll();
        return shops;
    }
    @GetMapping("/store/data")
    @ResponseBody
    public List<Store> storeData(){       
       List<Store> shops = storeRepository.findAll();
        return shops;
    }
    @GetMapping("/restaurant/data")
    @ResponseBody
    public List<Restaurant> restaurantData(){       
       List<Restaurant> shops = restaurantRepository.findAll();
        return shops;
    }
   @GetMapping("/map/coffee")
   public String coffee(Model model, @ModelAttribute Coupon coupon){
      List<Coffee> coffeeshop = coffeeRepository.findAll();
      model.addAttribute("coffeeshop", coffeeshop);
      List<Store> storeshop = storeRepository.findAll();
      model.addAttribute("storeshop", storeshop);
      List<Restaurant> restaurant = restaurantRepository.findAll();
      model.addAttribute("restaurant", restaurant);
      // couponRepository.save(coupon);
      return"map/coffee";
   }
   @PostMapping("/couponsave")
   @ResponseBody
   public String saveCoupon(@RequestBody Coupon coupon){
      
      User user =(User) session.getAttribute("user_info");
      
      // ์‚ฌ์šฉ์ž๊ฐ€ ์ด๋ฏธ ๋‹ค์šด๋กœ๋“œํ•œ ์ฟ ํฐ์˜ ์ˆ˜๋ฅผ ํ™•์ธ
      List<Coupon> userCoupons = couponRepository.findByUser(user);

      int totalCouponCount = 0;
      for(Coupon singleCoupon : userCoupons) {
         totalCouponCount += singleCoupon.getCount();
      }

      if (totalCouponCount > 3) {
        return "์ฟ ํฐ ๋‹ค์šด๋กœ๋“œ ํšŸ์ˆ˜๋ฅผ ์ดˆ๊ณผํ•˜์˜€์Šต๋‹ˆ๋‹ค.";
      } else {
         Coupon newCoupon = new Coupon();
         newCoupon.setUser(user);

         String uniqueCode = Long.toString(Math.abs(new Random().nextLong()),36).substring(0,12);
         newCoupon.setCode(uniqueCode);
         totalCouponCount += 1;
         newCoupon.setCount(totalCouponCount);
         newCoupon.setName(coupon.getName());
         newCoupon.setStartDate(LocalDate.now());
         newCoupon.setEndDate(LocalDate.now().plusMonths(1));

         couponRepository.save(newCoupon);
      
      }

      return "์ €์žฅ์™„๋ฃŒ";
   }
   
   @GetMapping("/where")
   public String where(){
      return"where";
   }
   @GetMapping("/chargingSearch")
   public String chargingSearch() {
      return "map/chargingSearch";
   }

}

 

 

 

ChargerStatus.java

 

package com.example.board.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import lombok.Data;

@Entity
@Data
public class ChargerStatus {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  private long quantity;

  @ManyToOne
  ChargerStatusYear chargerStatusYear;

  @ManyToOne
  ChargerStatusRegion chargerStatusRegion;
}

 

 

ChargerStatusRegion.java

 

package com.example.board.model;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import lombok.Data;

@Entity
@Data
public class ChargerStatusRegion {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  private String region;

  @OneToMany(mappedBy = "chargerStatusRegion")
  List<ChargerStatus> chargerStatus2 = new ArrayList<>();
}

 

 

ChargerStatusYear.java

 

package com.example.board.model;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import lombok.Data;

@Entity
@Data
public class ChargerStatusYear {

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private long id;

  private String year;

  @OneToMany(mappedBy = "chargerStatusYear")
  List<ChargerStatus> chargerStatus = new ArrayList<>();
}

 

Chargings.java

 

package com.example.board.model;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import lombok.Data;

@Entity
@Data
public class Chargings implements Serializable {
    @Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private long id;
	private String city;
	private String location;
	private String address;
	private int fastch;
    private int slowch;
	private double latitude;
	private double longitude;

}

 

 

ChargerStatusRegionRepository.java

 

package com.example.board.repository;

import com.example.board.model.ChargerStatusRegion;
import com.example.board.model.ChargerStatusYear;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ChargerStatusRegionRepsoitory
  extends JpaRepository<ChargerStatusRegion, Long> {
  ChargerStatusRegion findByRegion(String region);
}

 

 

ChargerStatusRepository.java

 

package com.example.board.repository;

import com.example.board.model.ChargerStatus;
import com.example.board.model.ChargerStatusRegion;
import com.example.board.model.ChargerStatusYear;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;

public interface ChargerStatusRepository
  extends JpaRepository<ChargerStatus, Long> {
  Optional<ChargerStatus> findById(Long id);
  Optional<ChargerStatus> findByChargerStatusRegionAndChargerStatusYear(ChargerStatusRegion chargerStatusRegion, ChargerStatusYear chargerStatusYear);

  // Long getQuantityByChargerStatusYearAndChargerStatusRegion(
  //   ChargerStatusYear chargerStatusYear,
  //   ChargerStatusRegion chargerStatusRegion
  // );
}

 

 

ChargerStatusYearRepository.java

 

package com.example.board.repository;

import com.example.board.model.ChargerStatus;
import com.example.board.model.ChargerStatusYear;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ChargerStatusYearRepsoitory
  extends JpaRepository<ChargerStatusYear, Long> {
  ChargerStatusYear findByYear(String year);
}

 

 

ChargingsRepository.java

 

package com.example.board.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.example.board.model.Chargings;

public interface ChargingsRepository extends JpaRepository<Chargings, Long> {
    Optional<Chargings> findById(int id);
}

 

 

chargingSearch.html

 

<!DOCTYPE html>
<html lang="en">

<style>
    .btn-primary {
        background-color: #efc19d;
        border-color: #efc19d;
        color: black;
    }
</style>

<body>
    <div class="container mt-5">
        <div>
            <h2>Search Charging Stations</h2>

            <div id='map' style='width:500px;height:400px;'></div>

            <script type="text/javascript"
                src="//dapi.kakao.com/v2/maps/sdk.js?appkey=๊ฐœ์ธ appkey"></script>

            <script src="https://unpkg.com/"></script>



            <script type='text/javascript'>
                let map;
                let markers =[];
                navigator.geolocation.getCurrentPosition(function (position) {
                    const lat = position.coords.latitude;
                    const lng = position.coords.longitude;
                    createMap(lat, lng);
                    getData();
                    createMarker2(lat,lng);
                });
                function createMap(lat, lng) {
                    const container = document.getElementById('map');
                    const options = {
                        center: new kakao.maps.LatLng(lat, lng),
                        level: 6
                    };
                    map = new kakao.maps.Map(container, options);
                }

                function getData(){
                    navigator.geolocation.getCurrentPosition(function (position) {
                    const lat = position.coords.latitude;
                    const lng = position.coords.longitude;
                    fetch('/tetest/data')
                    .then(response => response.json())
                    .then(data =>{
                        data.forEach(v=> {
                            const stationlat = v.latitude;
                            const stationlng = v.longitude;
                            
                            
                            let distancesArray = [];
                            let distanceInKm = getDistanceFromLatLonInKm( lng,lat, stationlng, stationlat);
                            
                            if (distanceInKm <= 2) {
                                createMarker(stationlat,stationlng);
                                }
                            });
                        }) 
                            
                    }
                    );
                    }
                    function getDistanceFromLatLonInKm(lat1, lon1, lat2, lon2) {
                        var R = 6371; // Radius of the earth in km
                        var dLat = deg2rad(lat2 - lat1);  // deg2rad below
                        var dLon = deg2rad(lon2 - lon1);
                        var a =
                            Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                            Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
                            Math.sin(dLon / 2) * Math.sin(dLon / 2)
                            ;
                        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
                        var d = R * c; // Distance in km
                        return d;
                    }
                    function deg2rad(deg) {
                        return deg * (Math.PI / 180)
                    }

            function createMarker(lat,lng){
                const markerPosition = new kakao.maps.LatLng(lat,lng);
                var imageSrc = 'https://cdn-icons-png.flaticon.com/512/7512/7512521.png', // ๋งˆ์ปค์ด๋ฏธ์ง€์˜ ์ฃผ์†Œ์ž…๋‹ˆ๋‹ค    
                imageSize = new kakao.maps.Size(50, 52), // ๋งˆ์ปค์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค
                imageOption = {offset: new kakao.maps.Point(27, 69)}; // ๋งˆ์ปค์ด๋ฏธ์ง€์˜ ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ๋งˆ์ปค์˜ ์ขŒํ‘œ์™€ ์ผ์น˜์‹œํ‚ฌ ์ด๋ฏธ์ง€ ์•ˆ์—์„œ์˜ ์ขŒํ‘œ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
                // ๋งˆ์ปค์˜ ์ด๋ฏธ์ง€์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋งˆ์ปค์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค
                var markerImage = new kakao.maps.MarkerImage(imageSrc, imageSize, imageOption);
                const marker = new kakao.maps.Marker({
                position: markerPosition,
                image: markerImage
            });
            markers.push(marker);
            marker.setMap(map);
            }
            function createMarker2(lat,lng){
                
                const markerPosition = new kakao.maps.LatLng(lat,lng);
                var imageSrc = "mini.png", // ๋งˆ์ปค์ด๋ฏธ์ง€์˜ ์ฃผ์†Œ์ž…๋‹ˆ๋‹ค    
                imageSize = new kakao.maps.Size(50, 52), // ๋งˆ์ปค์ด๋ฏธ์ง€์˜ ํฌ๊ธฐ์ž…๋‹ˆ๋‹ค
                imageOption = {offset: new kakao.maps.Point(27, 69)}; // ๋งˆ์ปค์ด๋ฏธ์ง€์˜ ์˜ต์…˜์ž…๋‹ˆ๋‹ค. ๋งˆ์ปค์˜ ์ขŒํ‘œ์™€ ์ผ์น˜์‹œํ‚ฌ ์ด๋ฏธ์ง€ ์•ˆ์—์„œ์˜ ์ขŒํ‘œ๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
                // ๋งˆ์ปค์˜ ์ด๋ฏธ์ง€์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๋งˆ์ปค์ด๋ฏธ์ง€๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค
                var markerImage = new kakao.maps.MarkerImage(imageSrc, imageSize, imageOption);
                const marker = new kakao.maps.Marker({
                position: markerPosition,
                image: markerImage
            });
            markers.push(marker);
            marker.setMap(map);
            }
                
            </script>
        </div>
    </div>

</body>

</html>

 

 

search.html

 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

</html>
<html lang="en">

<head th:replace="common/head">

</head>
<style>
    @font-face {
        font-family: 'TheJamsil5Bold';
        src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2302_01@1.0/TheJamsil5Bold.woff2') format('woff2');
        font-weight: 500;
        font-style: normal;
    }

    body {
        font-family: 'TheJamsil5Bold';
        height: 100vh;
        /* ํ™”๋ฉด ๋†’์ด์˜ 100%๋กœ ์„ค์ • */
        background-color: #000;
        background-size: cover;
        /* ์ด๋ฏธ์ง€๋ฅผ ํ™”๋ฉด์— ๋งž๊ฒŒ ์กฐ์ ˆ */
        background-repeat: no-repeat;
        /* ์ด๋ฏธ์ง€ ๋ฐ˜๋ณต์„ ์ค‘์ง€ */
    }

    .btn {
        background-color: rgb(228, 179, 93);
        border-color: rgb(228, 179, 93);
    }

    .btn:hover {
        background-color: rgb(228, 179, 93);
        border-color: rgb(228, 179, 93);
    }
</style>

<body>

    <div th:replace="common/header">

    </div>

    <nav th:replace="common/nav">

    </nav>

    <div class="container mt-5">

        <div class="row">
            <div id="map" style="width:500px; height:400px; margin:0 auto;"></div>
        </div>
        <br>
        <table class="table table-hover">
            <thead>
                <tr>

                    <th>์ƒํ˜ธ๋ช…</th>
                    <th>์†Œ์žฌ์ง€</th>
                    <th>์—ฐ๋ฝ์ฒ˜</th>
                    <th>์ด๋™</th>


                </tr>
            </thead>
            <tbody>
                <tr>
                    <br>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                    <td></td>
                </tr>
            </tbody>
        </table>




    </div>
    <script type="text/javascript"
        src="//dapi.kakao.com/v2/maps/sdk.js?appkey=๊ฐœ์ธappkey"></script>

    <script src="https://unpkg.com/"></script>

    <script>



        navigator.geolocation.getCurrentPosition(function (position) {
            const lat = position.coords.latitude;
            const lng = position.coords.longitude;
            createMap(lat, lng);
        });

        function createMap(lat, lng) {
            const container = document.getElementById('map');
            const options = {
                center: new kakao.maps.LatLng(lat, lng),
                level: 9
            }
            map = new kakao.maps.Map(container, options);
            openAPI();
        }

        async function openAPI() {
            const data = await fetch('https://api.odcloud.kr/๊ฐœ์ธapiํ‚ค');
            const res = await data.json();
            const data_list = res.data;
            let code = '';
            data_list.forEach((v) => {
                code += `<tr><td>${v['์ƒ ํ˜ธ']}</td><td>${v['์†Œ์žฌ์ง€']}</td><td>${v['์—ฐ๋ฝ์ฒ˜']}</td><td><button class="btn btn-primary btn-block" onclick = "moveLatLon()">์ด๋™</button></td></tr>`


                // kakao.maps.load(function(){

                const geocoder = new kakao.maps.services.Geocoder();
                const address = v.์†Œ์žฌ์ง€;
                geocoder.addressSearch(address, function (result, status) {
                    if (status === kakao.maps.services.Status.OK) {
                        const coords = new kakao.maps.LatLng(result[0].y, result[0].x);
                        const marker = new kakao.maps.Marker({
                            map: map,
                            position: coords
                        });
                        const infowindow = new kakao.maps.InfoWindow({
                            content: `<div style="width:150px;text-align:center;padding:6px 0;">"${v['์ƒ ํ˜ธ']}"</div>`
                        });
                        infowindow.open(map, marker);
                        map.setCenter(coords);
                    }
                });

                // });
            })

            const table = document.querySelector('table tbody tr:last-child');
            table.insertAdjacentHTML("afterend", code);
        }



    </script>

    <script>
        function moveLatLon(lat, lng) {
            map.setLevel(3);
            const moveLatlng = new kakao.maps.LatLng(35.1490440067967, 126.917628214769)
            map.panTo(moveLatlng);
        }
    </script>

    <div th:replace="common/footer">
    </div>

</body>

</html>

 

 

 

where.html

 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

</html>
<html lang="en">

<head th:replace="common/head">
</head>
<style>
    @font-face {
        font-family: 'TheJamsil5Bold';
        src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_2302_01@1.0/TheJamsil5Bold.woff2') format('woff2');
        font-weight: 500;
        font-style: normal;
    }

    body {
        font-family: 'TheJamsil5Bold';
        height: 100vh;
        /* ํ™”๋ฉด ๋†’์ด์˜ 100%๋กœ ์„ค์ • */
        background-color: #000;
        background-size: cover;
        /* ์ด๋ฏธ์ง€๋ฅผ ํ™”๋ฉด์— ๋งž๊ฒŒ ์กฐ์ ˆ */
        background-repeat: no-repeat;
        /* ์ด๋ฏธ์ง€ ๋ฐ˜๋ณต์„ ์ค‘์ง€ */
    }

    .btn {
        background-color: rgb(228, 179, 93);
        border-color: rgb(228, 179, 93);
    }

    .btn:hover {
        background-color: rgb(228, 179, 93);
        border-color: rgb(228, 179, 93);
    }
</style>

<body>

    <div th:replace="common/header">

    </div>

    <nav th:replace="common/nav">

    </nav>

    <div class="container mt-5">
        <form th:action="@{/map/where}" method="get" onsubmit="searchAddress(); return false;">
            <input type="text" id="search" placeholder="๊ฒ€์ƒ‰์–ด ์ž…๋ ฅ">
            <button type="submit" class="btn btn-primary btn-block">๊ฒ€์ƒ‰</button>
            <button onclick="move()" class="btn btn-primary btn-block" id="write-btn">์žฅ์†Œ ๋“ฑ๋ก์ฐฝ์œผ๋กœ ์ด๋™</button>
        </form>
        <p class=search id="storeName">์žฅ์†Œ๋ช…:</p>
        <p class=latitude id="latitude">์œ„๋„:</p>
        <p class=longitude id="longitude">๊ฒฝ๋„:</p>
        <div id="map" style="width:500px; height:400px; margin:0 0;"></div>
    </div>

    <script type="text/javascript"
        src="//dapi.kakao.com/v2/maps/sdk.js?appkey=๊ฐœ์ธappkey"></script>

    <script>
        let map;
        let storeNameElement;
        let latitudeElement;
        let longitudeElement;
        // ๊ฒ€์ƒ‰์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์ง€๋„์— ๋งˆ์ปค๋ฅผ ํ‘œ์‹œํ•˜๋Š” ํ•จ์ˆ˜
        function searchAddress() {
            const search = document.getElementById('search').value; // ๊ฒ€์ƒ‰์–ด ์ž…๋ ฅ๋ž€์—์„œ ๊ฒ€์ƒ‰์–ด ๊ฐ€์ ธ์˜ค๊ธฐ
            const container = document.getElementById('map');
            let storeNameElement = document.getElementById('storeName');
            let latitudeElement = document.getElementById('latitude');
            let longitudeElement = document.getElementById('longitude');
            const options = {
                level: 9
            };

            // ์นด์นด์˜ค ๋งต ๋กœ๋“œ ํ›„ ์ง€๋„ ์ƒ์„ฑ
            kakao.maps.load(function () {
                const geocoder = new kakao.maps.services.Geocoder();
                geocoder.addressSearch(search, function (result, status) {
                    if (status === kakao.maps.services.Status.OK) {
                        const coords = new kakao.maps.LatLng(result[0].y, result[0].x);
                        const marker = new kakao.maps.Marker({
                            map: map,
                            position: coords
                        });
                        const storeName = search;
                        storeNameElement.textContent = '์žฅ์†Œ๋ช…: ' + storeName;
                        document.getElementById('latitude').textContent = '์œ„๋„: ' + result[0].y;
                        document.getElementById('longitude').textContent = '๊ฒฝ๋„: ' + result[0].x;
                        const infowindow = new kakao.maps.InfoWindow({
                            content: `<div style="width:150px;text-align:center;padding:6px 0;">"${storeName}"</div>`
                        });
                        infowindow.open(map, marker);
                        map.setCenter(coords);
                    }
                });

            });


        }

        navigator.geolocation.getCurrentPosition(function (position) {
            const lat = position.coords.latitude;
            const lng = position.coords.longitude;
            createMap(lat, lng);
        });

        function createMap(lat, lng) {
            const container = document.getElementById('map');
            const options = {
                center: new kakao.maps.LatLng(lat, lng),
                level: 9
            }
            map = new kakao.maps.Map(container, options);
        }


    </script>
    <script>
        function move() {
            location = '/map';
        }
    </script>
    <div th:replace="common/footer">
    </div>

</body>

</html>
728x90
๋ฐ˜์‘ํ˜•

loading