구글 맵 API 사용 준비

  1. 구글에 로그인하고 구글 맵 플랫폼 사이트로 이동합니다. https://cloud.google.com/maps-platform/

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b59d2e79-706c-4280-b0dc-a9abb8251c59/Untitled.png

  2. 사이트 내의 시작하기 버튼을 누르세요. 그러면 GCP(Google Cloude Platform) 무료 평가판 이용을 위한 절차가 진행됩니다.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/356f15fe-6a7b-4c9d-bd55-68e3b29ed4af/Untitled.png

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ba50549f-836a-498b-9ac2-9684f64458cb/Untitled.png

  3. (2단계) 결제수단 등록까지 완료하면 90일 동안 Google Cloud Platform에서 제공하는 기능들을 300$ 내에 무료로 이용할 수 있습니다. 구글 안내에 따르면 평가판이 지나도 자동으로 결제되지는 않는다고 합니다.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/dbc11ae2-2305-45d9-82c5-d391c0732ae9/Untitled.png

  4. 간단한 설문이 진행됩니다. 본인이 사용하고자하는 목적에 맞게 설문하시면 됩니다. 설문 내용에 따라 기능을 추천해준다고 합니다.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/762560f4-0f2b-440d-af98-1901e390a846/Untitled.png

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/b572480c-8b41-4025-a179-74c5007d53f1/Untitled.png

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/fea99c3a-2ec1-4b82-9ad0-ecfc909eddcb/Untitled.png

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/1b2c5117-128d-4fc5-a741-352bee23ab23/Untitled.png

  5. 설문이 끝나면 API 키를 받습니다.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f0202979-9a1f-4bb9-bfcc-d3bddc64b4a4/Untitled.png

  6. My First Project라는 이름으로 첫 번째 프로젝트가 생성되었을 겁니다. 해당 프로젝트의 결제 계정을 처음 만들었던 결제 계정으로 설정합니다. 결제 계정을 설정하지 않으면 API를 사용할 수 없습니다.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/64d74279-a17f-49a3-a5ad-552167077823/Untitled.png

  7. 제품 및 리소스 검색에서 google maps platform을 검색합니다.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/f99e7552-30f2-413f-a4c2-329091ddcf88/Untitled.png

  8. Maps JavaScript API를 사용합니다.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5978817d-b9b0-421a-938b-b8bdb79c84a8/Untitled.png

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/af94583c-cfc4-4579-8c02-75b8bd2e98d4/Untitled.png

cafedetail에 구글맵 추가하기

  1. 구글 맵을 보여줄 cafedetail.html 에 다음 코드를 추가합니다. [사용자 키] 부분에 본인이 발급받은 API 키를 추가합니다.

    <script>
      function initMap() {
        var jeju = {lat: 33.3616658, lng: 126.5204118};
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 10,
          center: jeju
        });
    
        var marker = new google.maps.Marker({
          position: {lat: {{cafe.lat}}, lng: {{cafe.lng}}},
          map: map
        });
    
      }
    </script>
    

    <body> 태그 마지막 부분에 다음 코드를 추가합니다.

    <script async defer
        src="<https://maps.googleapis.com/maps/api/js?key=>[사용자 키]&callback=initMap">
    </script>
    
  2. 해당 페이지로 가면 이제 구글맵을 볼 수 있습니다.

    https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4514b52b-8ef4-496c-b0b6-4456ef72662c/Untitled.png

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/1343778c-0c57-467e-ae63-ffdc0ebb0ded/Untitled.png

구글맵에 마커 추가하기

구글 맵에는 개발자가 원하는 위치를 가리키는 마커를 삽입할 수 있습니다. 해당 카페의 위치를 마커로 표시해보겠습니다.

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/53742510-c9a1-4ba5-91b2-17170de77778/Untitled.png

마커를 삽입하기 위해서는 해당 위치의 위도와 경도를 알아야 합니다. Cafe 모델에 카페의 위도와 경도를 저장하는 lat, lng 필드를 정의합니다.

from django.db import models

class Cafe(models.Model):
    name = models.CharField(max_length=50)
    
    locations = [
        ('Hangyeong-myeon', '한경면'), 
        ('Hallim-eup', '한림읍'),
        ('Aewol-eup', '애월읍'), 
        ('Jeju-si', '제주시'), 
        ('Jocheon-eup', '조천읍'), 
        ('Gujwa-eup', '구좌읍'),
        ('Daejeong-eup', '대정읍'),
        ('Andeok-myeon', '안덕면'),
        ('Seogwipo-si', '서귀포시'),
        ('Namwon-eup', '남원읍'),
        ('Pyoseon-myeon', '표선면'),
        ('Seongsan-eup', '성산읍'),
        ('Udo-myeon', '우도면'),
    ]
    
    location = models.CharField(max_length=50, choices=locations)
    lat = models.FloatField(null=True)
    lng = models.FloatField(null=True)
    mainphoto = models.ImageField(blank=True, null=True)
    subphoto = models.ImageField(blank=True, null=True)
    published_date = models.DateTimeField(auto_now_add=True)
    modified_date = models.DateTimeField(auto_now=True)
    content = models.TextField()
    phone = models.CharField(max_length=20, null=True)
    insta = models.CharField(max_length=20, null=True)
    
    def __str__(self):
        return self.name