티스토리 뷰

안녕하세요 강정호입니다. 오늘은 Google Map API에서 위치 자동완성 기능에 대해 알아볼게요


위치 자동완성은 Place Autocomplete이라고합니다.


Place Autocomplete 공식문서




<!DOCTYPE html>
<html>
 
<head>
   
<title>Place Autocomplete</title>
   
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
   
<meta charset="utf-8">
   
<style>
     
/* Always set the map height explicitly to define the size of the div
       * element that contains the map. */

     
#map {
       
height: 100%;
     
}
     
/* Optional: Makes the sample page fill the window. */
      html
, body {
       
height: 100%;
       
margin: 0;
       
padding: 0;
     
}
     
#description {
       
font-family: Roboto;
       
font-size: 15px;
       
font-weight: 300;
     
}

     
#infowindow-content .title {
       
font-weight: bold;
     
}

     
#infowindow-content {
       
display: none;
     
}

     
#map #infowindow-content {
       
display: inline;
     
}

     
.pac-card {
       
margin: 10px 10px 0 0;
       
border-radius: 2px 0 0 2px;
       
box-sizing: border-box;
       
-moz-box-sizing: border-box;
       
outline: none;
       
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
       
background-color: #fff;
       
font-family: Roboto;
     
}

     
#pac-container {
       
padding-bottom: 12px;
       
margin-right: 12px;
     
}

     
.pac-controls {
       
display: inline-block;
       
padding: 5px 11px;
     
}

     
.pac-controls label {
       
font-family: Roboto;
       
font-size: 13px;
       
font-weight: 300;
     
}

     
#pac-input {
       
background-color: #fff;
       
font-family: Roboto;
       
font-size: 15px;
       
font-weight: 300;
       
margin-left: 12px;
       
padding: 0 11px 0 13px;
       
text-overflow: ellipsis;
       
width: 400px;
     
}

     
#pac-input:focus {
       
border-color: #4d90fe;
     
}

     
#title {
       
color: #fff;
       
background-color: #4d90fe;
       
font-size: 25px;
       
font-weight: 500;
       
padding: 6px 12px;
     
}
   
</style>
 
</head>
 
<body>
   
<div class="pac-card" id="pac-card">
     
<div>
       
<div id="title">
          Autocomplete search
       
</div>
       
<div id="type-selector" class="pac-controls">
         
<input type="radio" name="type" id="changetype-all" checked="checked">
         
<label for="changetype-all">All</label>

         
<input type="radio" name="type" id="changetype-establishment">
         
<label for="changetype-establishment">Establishments</label>

         
<input type="radio" name="type" id="changetype-address">
         
<label for="changetype-address">Addresses</label>

         
<input type="radio" name="type" id="changetype-geocode">
         
<label for="changetype-geocode">Geocodes</label>
       
</div>
       
<div id="strict-bounds-selector" class="pac-controls">
         
<input type="checkbox" id="use-strict-bounds" value="">
         
<label for="use-strict-bounds">Strict Bounds</label>
       
</div>
     
</div>
     
<div id="pac-container">
       
<input id="pac-input" type="text"
           
placeholder="Enter a location">
     
</div>
   
</div>
   
<div id="map"></div>
   
<div id="infowindow-content">
     
<img src="" width="16" height="16" id="place-icon">
     
<span id="place-name"  class="title"></span><br>
     
<span id="place-address"></span>
   
</div>

   
<script>
     
// This example requires the Places library. Include the libraries=places
     
// parameter when you first load the API. For example:
     
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

     
function initMap() {
       
var map = new google.maps.Map(document.getElementById('map'), {
          center
: {lat: -33.8688, lng: 151.2195},
          zoom
: 13
       
});
       
var card = document.getElementById('pac-card');
       
var input = document.getElementById('pac-input');
       
var types = document.getElementById('type-selector');
       
var strictBounds = document.getElementById('strict-bounds-selector');

        map
.controls[google.maps.ControlPosition.TOP_RIGHT].push(card);

       
var autocomplete = new google.maps.places.Autocomplete(input);

       
// Bind the map's bounds (viewport) property to the autocomplete object,
       
// so that the autocomplete requests use the current map bounds for the
       
// bounds option in the request.
        autocomplete
.bindTo('bounds', map);

       
// Set the data fields to return when the user selects a place.
        autocomplete
.setFields(
           
['address_components', 'geometry', 'icon', 'name']);

       
var infowindow = new google.maps.InfoWindow();
       
var infowindowContent = document.getElementById('infowindow-content');
        infowindow
.setContent(infowindowContent);
       
var marker = new google.maps.Marker({
          map
: map,
          anchorPoint
: new google.maps.Point(0, -29)
       
});

        autocomplete
.addListener('place_changed', function() {
          infowindow
.close();
          marker
.setVisible(false);
         
var place = autocomplete.getPlace();
         
if (!place.geometry) {
           
// User entered the name of a Place that was not suggested and
           
// pressed the Enter key, or the Place Details request failed.
            window
.alert("No details available for input: '" + place.name + "'");
           
return;
         
}

         
// If the place has a geometry, then present it on a map.
         
if (place.geometry.viewport) {
            map
.fitBounds(place.geometry.viewport);
         
} else {
            map
.setCenter(place.geometry.location);
            map
.setZoom(17);  // Why 17? Because it looks good.
         
}
          marker
.setPosition(place.geometry.location);
          marker
.setVisible(true);

         
var address = '';
         
if (place.address_components) {
            address
= [
             
(place.address_components[0] && place.address_components[0].short_name || ''),
             
(place.address_components[1] && place.address_components[1].short_name || ''),
             
(place.address_components[2] && place.address_components[2].short_name || '')
           
].join(' ');
         
}

          infowindowContent
.children['place-icon'].src = place.icon;
          infowindowContent
.children['place-name'].textContent = place.name;
          infowindowContent
.children['place-address'].textContent = address;
          infowindow
.open(map, marker);
       
});

       
// Sets a listener on a radio button to change the filter type on Places
       
// Autocomplete.
       
function setupClickListener(id, types) {
         
var radioButton = document.getElementById(id);
          radioButton
.addEventListener('click', function() {
            autocomplete
.setTypes(types);
         
});
       
}

        setupClickListener
('changetype-all', []);
        setupClickListener
('changetype-address', ['address']);
        setupClickListener
('changetype-establishment', ['establishment']);
        setupClickListener
('changetype-geocode', ['geocode']);

        document
.getElementById('use-strict-bounds')
           
.addEventListener('click', function() {
              console
.log('Checkbox clicked! New state=' + this.checked);
              autocomplete
.setOptions({strictBounds: this.checked});
           
});
     
}
   
</script>
   
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"
       
async defer></script>
 
</body>
</html>
<!DOCTYPE html>
<html>
 
<head>
   
<title>Place Autocomplete</title>
   
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
   
<meta charset="utf-8">
   
<style>
     
/* Always set the map height explicitly to define the size of the div
       * element that contains the map. */

     
#map {
       
height: 100%;
     
}
     
/* Optional: Makes the sample page fill the window. */
      html
, body {
       
height: 100%;
       
margin: 0;
       
padding: 0;
     
}
     
#description {
       
font-family: Roboto;
       
font-size: 15px;
       
font-weight: 300;
     
}

     
#infowindow-content .title {
       
font-weight: bold;
     
}

     
#infowindow-content {
       
display: none;
     
}

     
#map #infowindow-content {
       
display: inline;
     
}

     
.pac-card {
       
margin: 10px 10px 0 0;
       
border-radius: 2px 0 0 2px;
       
box-sizing: border-box;
       
-moz-box-sizing: border-box;
       
outline: none;
       
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
       
background-color: #fff;
       
font-family: Roboto;
     
}

     
#pac-container {
       
padding-bottom: 12px;
       
margin-right: 12px;
     
}

     
.pac-controls {
       
display: inline-block;
       
padding: 5px 11px;
     
}

     
.pac-controls label {
       
font-family: Roboto;
       
font-size: 13px;
       
font-weight: 300;
     
}

     
#pac-input {
       
background-color: #fff;
       
font-family: Roboto;
       
font-size: 15px;
       
font-weight: 300;
       
margin-left: 12px;
       
padding: 0 11px 0 13px;
       
text-overflow: ellipsis;
       
width: 400px;
     
}

     
#pac-input:focus {
       
border-color: #4d90fe;
     
}

     
#title {
       
color: #fff;
       
background-color: #4d90fe;
       
font-size: 25px;
       
font-weight: 500;
       
padding: 6px 12px;
     
}
   
</style>
 
</head>
 
<body>
   
<div class="pac-card" id="pac-card">
     
<div>
       
<div id="title">
          Autocomplete search
       
</div>
       
<div id="type-selector" class="pac-controls">
         
<input type="radio" name="type" id="changetype-all" checked="checked">
         
<label for="changetype-all">All</label>

         
<input type="radio" name="type" id="changetype-establishment">
         
<label for="changetype-establishment">Establishments</label>

         
<input type="radio" name="type" id="changetype-address">
         
<label for="changetype-address">Addresses</label>

         
<input type="radio" name="type" id="changetype-geocode">
         
<label for="changetype-geocode">Geocodes</label>
       
</div>
       
<div id="strict-bounds-selector" class="pac-controls">
         
<input type="checkbox" id="use-strict-bounds" value="">
         
<label for="use-strict-bounds">Strict Bounds</label>
       
</div>
     
</div>
     
<div id="pac-container">
       
<input id="pac-input" type="text"
           
placeholder="Enter a location">
     
</div>
   
</div>
   
<div id="map"></div>
   
<div id="infowindow-content">
     
<img src="" width="16" height="16" id="place-icon">
     
<span id="place-name"  class="title"></span><br>
     
<span id="place-address"></span>
   
</div>

   
<script>
     
// This example requires the Places library. Include the libraries=places
     
// parameter when you first load the API. For example:
     
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

     
function initMap() {
       
var map = new google.maps.Map(document.getElementById('map'), {
          center
: {lat: -33.8688, lng: 151.2195},
          zoom
: 13
       
});
       
var card = document.getElementById('pac-card');
       
var input = document.getElementById('pac-input');
       
var types = document.getElementById('type-selector');
       
var strictBounds = document.getElementById('strict-bounds-selector');

        map
.controls[google.maps.ControlPosition.TOP_RIGHT].push(card);

       
var autocomplete = new google.maps.places.Autocomplete(input);

       
// Bind the map's bounds (viewport) property to the autocomplete object,
       
// so that the autocomplete requests use the current map bounds for the
       
// bounds option in the request.
        autocomplete
.bindTo('bounds', map);

       
// Set the data fields to return when the user selects a place.
        autocomplete
.setFields(
           
['address_components', 'geometry', 'icon', 'name']);

       
var infowindow = new google.maps.InfoWindow();
       
var infowindowContent = document.getElementById('infowindow-content');
        infowindow
.setContent(infowindowContent);
       
var marker = new google.maps.Marker({
          map
: map,
          anchorPoint
: new google.maps.Point(0, -29)
       
});

        autocomplete
.addListener('place_changed', function() {
          infowindow
.close();
          marker
.setVisible(false);
         
var place = autocomplete.getPlace();
         
if (!place.geometry) {
           
// User entered the name of a Place that was not suggested and
           
// pressed the Enter key, or the Place Details request failed.
            window
.alert("No details available for input: '" + place.name + "'");
           
return;
         
}

         
// If the place has a geometry, then present it on a map.
         
if (place.geometry.viewport) {
            map
.fitBounds(place.geometry.viewport);
         
} else {
            map
.setCenter(place.geometry.location);
            map
.setZoom(17);  // Why 17? Because it looks good.
         
}
          marker
.setPosition(place.geometry.location);
          marker
.setVisible(true);

         
var address = '';
         
if (place.address_components) {
            address
= [
             
(place.address_components[0] && place.address_components[0].short_name || ''),
             
(place.address_components[1] && place.address_components[1].short_name || ''),
             
(place.address_components[2] && place.address_components[2].short_name || '')
           
].join(' ');
         
}

          infowindowContent
.children['place-icon'].src = place.icon;
          infowindowContent
.children['place-name'].textContent = place.name;
          infowindowContent
.children['place-address'].textContent = address;
          infowindow
.open(map, marker);
       
});

       
// Sets a listener on a radio button to change the filter type on Places
       
// Autocomplete.
       
function setupClickListener(id, types) {
         
var radioButton = document.getElementById(id);
          radioButton
.addEventListener('click', function() {
            autocomplete
.setTypes(types);
         
});
       
}

        setupClickListener
('changetype-all', []);
        setupClickListener
('changetype-address', ['address']);
        setupClickListener
('changetype-establishment', ['establishment']);
        setupClickListener
('changetype-geocode', ['geocode']);

        document
.getElementById('use-strict-bounds')
           
.addEventListener('click', function() {
              console
.log('Checkbox clicked! New state=' + this.checked);
              autocomplete
.setOptions({strictBounds: this.checked});
           
});
     
}
   
</script>
   
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"
       
async defer></script>
 
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html>
<html>
  <head>
    <title>Place Autocomplete</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #description {
        font-family: Roboto;
        font-size: 15px;
        font-weight: 300;
      }
 
      #infowindow-content .title {
        font-weight: bold;
      }
 
      #infowindow-content {
        display: none;
      }
 
      #map #infowindow-content {
        display: inline;
      }
 
      .pac-card {
        margin: 10px 10px 0 0;
        border-radius: 2px 0 0 2px;
        box-sizing: border-box;
        -moz-box-sizing: border-box;
        outline: none;
        box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
        background-color: #fff;
        font-family: Roboto;
      }
 
      #pac-container {
        padding-bottom: 12px;
        margin-right: 12px;
      }
 
      .pac-controls {
        display: inline-block;
        padding: 5px 11px;
      }
 
      .pac-controls label {
        font-family: Roboto;
        font-size: 13px;
        font-weight: 300;
      }
 
      #pac-input {
        background-color: #fff;
        font-family: Roboto;
        font-size: 15px;
        font-weight: 300;
        margin-left: 12px;
        padding: 0 11px 0 13px;
        text-overflow: ellipsis;
        width: 400px;
      }
 
      #pac-input:focus {
        border-color: #4d90fe;
      }
 
      #title {
        color: #fff;
        background-color: #4d90fe;
        font-size: 25px;
        font-weight: 500;
        padding: 6px 12px;
      }
    </style>
  </head>
  <body>
    <div class="pac-card" id="pac-card">
      <div>
        <div id="title">
          Autocomplete search
        </div>
        <div id="type-selector" class="pac-controls">
          <input type="radio" name="type" id="changetype-all" checked="checked">
          <label for="changetype-all">All</label>
 
          <input type="radio" name="type" id="changetype-establishment">
          <label for="changetype-establishment">Establishments</label>
 
          <input type="radio" name="type" id="changetype-address">
          <label for="changetype-address">Addresses</label>
 
          <input type="radio" name="type" id="changetype-geocode">
          <label for="changetype-geocode">Geocodes</label>
        </div>
        <div id="strict-bounds-selector" class="pac-controls">
          <input type="checkbox" id="use-strict-bounds" value="">
          <label for="use-strict-bounds">Strict Bounds</label>
        </div>
      </div>
      <div id="pac-container">
        <input id="pac-input" type="text"
            placeholder="Enter a location">
      </div>
    </div>
    <div id="map"></div>
    <div id="infowindow-content">
      <img src="" width="16" height="16" id="place-icon">
      <span id="place-name"  class="title"></span><br>
      <span id="place-address"></span>
    </div>
 
    <script>
      // This example requires the Places library. Include the libraries=places
      // parameter when you first load the API. For example:
      // <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
 
      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -33.8688, lng: 151.2195},
          zoom: 13
        });
        var card = document.getElementById('pac-card');
        var input = document.getElementById('pac-input');
        var types = document.getElementById('type-selector');
        var strictBounds = document.getElementById('strict-bounds-selector');
 
        map.controls[google.maps.ControlPosition.TOP_RIGHT].push(card);
 
        var autocomplete = new google.maps.places.Autocomplete(input);
 
        // Bind the map's bounds (viewport) property to the autocomplete object,
        // so that the autocomplete requests use the current map bounds for the
        // bounds option in the request.
        autocomplete.bindTo('bounds', map);
 
        // Set the data fields to return when the user selects a place.
        autocomplete.setFields(
            ['address_components', 'geometry', 'icon', 'name']);
 
        var infowindow = new google.maps.InfoWindow();
        var infowindowContent = document.getElementById('infowindow-content');
        infowindow.setContent(infowindowContent);
        var marker = new google.maps.Marker({
          map: map,
          anchorPoint: new google.maps.Point(0, -29)
        });
 
        autocomplete.addListener('place_changed', function() {
          infowindow.close();
          marker.setVisible(false);
          var place = autocomplete.getPlace();
          if (!place.geometry) {
            // User entered the name of a Place that was not suggested and
            // pressed the Enter key, or the Place Details request failed.
            window.alert("No details available for input: '" + place.name + "'");
            return;
          }
 
          // If the place has a geometry, then present it on a map.
          if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
          } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17);  // Why 17? Because it looks good.
          }
          marker.setPosition(place.geometry.location);
          marker.setVisible(true);
 
          var address = '';
          if (place.address_components) {
            address = [
              (place.address_components[0] && place.address_components[0].short_name || ''),
              (place.address_components[1] && place.address_components[1].short_name || ''),
              (place.address_components[2] && place.address_components[2].short_name || '')
            ].join(' ');
          }
 
          infowindowContent.children['place-icon'].src = place.icon;
          infowindowContent.children['place-name'].textContent = place.name;
          infowindowContent.children['place-address'].textContent = address;
          infowindow.open(map, marker);
        });
 
        // Sets a listener on a radio button to change the filter type on Places
        // Autocomplete.
        function setupClickListener(id, types) {
          var radioButton = document.getElementById(id);
          radioButton.addEventListener('click', function() {
            autocomplete.setTypes(types);
          });
        }
 
        setupClickListener('changetype-all', []);
        setupClickListener('changetype-address', ['address']);
        setupClickListener('changetype-establishment', ['establishment']);
        setupClickListener('changetype-geocode', ['geocode']);
 
        document.getElementById('use-strict-bounds')
            .addEventListener('click', function() {
              console.log('Checkbox clicked! New state=' + this.checked);
              autocomplete.setOptions({strictBounds: this.checked});
            });
      }
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"
        async defer></script>
  </body>
</html>
cs
<!DOCTYPE html>
<html>
 
<head>
   
<title>Place Autocomplete</title>
   
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
   
<meta charset="utf-8">
   
<style>
     
/* Always set the map height explicitly to define the size of the div
       * element that contains the map. */

     
#map {
       
height: 100%;
     
}
     
/* Optional: Makes the sample page fill the window. */
      html
, body {
       
height: 100%;
       
margin: 0;
       
padding: 0;
     
}
     
#description {
       
font-family: Roboto;
       
font-size: 15px;
       
font-weight: 300;
     
}

     
#infowindow-content .title {
       
font-weight: bold;
     
}

     
#infowindow-content {
       
display: none;
     
}

     
#map #infowindow-content {
       
display: inline;
     
}

     
.pac-card {
       
margin: 10px 10px 0 0;
       
border-radius: 2px 0 0 2px;
       
box-sizing: border-box;
       
-moz-box-sizing: border-box;
       
outline: none;
       
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
       
background-color: #fff;
       
font-family: Roboto;
     
}

     
#pac-container {
       
padding-bottom: 12px;
       
margin-right: 12px;
     
}

     
.pac-controls {
       
display: inline-block;
       
padding: 5px 11px;
     
}

     
.pac-controls label {
       
font-family: Roboto;
       
font-size: 13px;
       
font-weight: 300;
     
}

     
#pac-input {
       
background-color: #fff;
       
font-family: Roboto;
       
font-size: 15px;
       
font-weight: 300;
       
margin-left: 12px;
       
padding: 0 11px 0 13px;
       
text-overflow: ellipsis;
       
width: 400px;
     
}

     
#pac-input:focus {
       
border-color: #4d90fe;
     
}

     
#title {
       
color: #fff;
       
background-color: #4d90fe;
       
font-size: 25px;
       
font-weight: 500;
       
padding: 6px 12px;
     
}
   
</style>
 
</head>
 
<body>
   
<div class="pac-card" id="pac-card">
     
<div>
       
<div id="title">
          Autocomplete search
       
</div>
       
<div id="type-selector" class="pac-controls">
         
<input type="radio" name="type" id="changetype-all" checked="checked">
         
<label for="changetype-all">All</label>

         
<input type="radio" name="type" id="changetype-establishment">
         
<label for="changetype-establishment">Establishments</label>

         
<input type="radio" name="type" id="changetype-address">
         
<label for="changetype-address">Addresses</label>

         
<input type="radio" name="type" id="changetype-geocode">
         
<label for="changetype-geocode">Geocodes</label>
       
</div>
       
<div id="strict-bounds-selector" class="pac-controls">
         
<input type="checkbox" id="use-strict-bounds" value="">
         
<label for="use-strict-bounds">Strict Bounds</label>
       
</div>
     
</div>
     
<div id="pac-container">
       
<input id="pac-input" type="text"
           
placeholder="Enter a location">
     
</div>
   
</div>
   
<div id="map"></div>
   
<div id="infowindow-content">
     
<img src="" width="16" height="16" id="place-icon">
     
<span id="place-name"  class="title"></span><br>
     
<span id="place-address"></span>
   
</div>

   
<script>
     
// This example requires the Places library. Include the libraries=places
     
// parameter when you first load the API. For example:
     
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">

     
function initMap() {
       
var map = new google.maps.Map(document.getElementById('map'), {
          center
: {lat: -33.8688, lng: 151.2195},
          zoom
: 13
       
});
       
var card = document.getElementById('pac-card');
       
var input = document.getElementById('pac-input');
       
var types = document.getElementById('type-selector');
       
var strictBounds = document.getElementById('strict-bounds-selector');

        map
.controls[google.maps.ControlPosition.TOP_RIGHT].push(card);

       
var autocomplete = new google.maps.places.Autocomplete(input);

       
// Bind the map's bounds (viewport) property to the autocomplete object,
       
// so that the autocomplete requests use the current map bounds for the
       
// bounds option in the request.
        autocomplete
.bindTo('bounds', map);

       
// Set the data fields to return when the user selects a place.
        autocomplete
.setFields(
           
['address_components', 'geometry', 'icon', 'name']);

       
var infowindow = new google.maps.InfoWindow();
       
var infowindowContent = document.getElementById('infowindow-content');
        infowindow
.setContent(infowindowContent);
       
var marker = new google.maps.Marker({
          map
: map,
          anchorPoint
: new google.maps.Point(0, -29)
       
});

        autocomplete
.addListener('place_changed', function() {
          infowindow
.close();
          marker
.setVisible(false);
         
var place = autocomplete.getPlace();
         
if (!place.geometry) {
           
// User entered the name of a Place that was not suggested and
           
// pressed the Enter key, or the Place Details request failed.
            window
.alert("No details available for input: '" + place.name + "'");
           
return;
         
}

         
// If the place has a geometry, then present it on a map.
         
if (place.geometry.viewport) {
            map
.fitBounds(place.geometry.viewport);
         
} else {
            map
.setCenter(place.geometry.location);
            map
.setZoom(17);  // Why 17? Because it looks good.
         
}
          marker
.setPosition(place.geometry.location);
          marker
.setVisible(true);

         
var address = '';
         
if (place.address_components) {
            address
= [
             
(place.address_components[0] && place.address_components[0].short_name || ''),
             
(place.address_components[1] && place.address_components[1].short_name || ''),
             
(place.address_components[2] && place.address_components[2].short_name || '')
           
].join(' ');
         
}

          infowindowContent
.children['place-icon'].src = place.icon;
          infowindowContent
.children['place-name'].textContent = place.name;
          infowindowContent
.children['place-address'].textContent = address;
          infowindow
.open(map, marker);
       
});

       
// Sets a listener on a radio button to change the filter type on Places
       
// Autocomplete.
       
function setupClickListener(id, types) {
         
var radioButton = document.getElementById(id);
          radioButton
.addEventListener('click', function() {
            autocomplete
.setTypes(types);
         
});
       
}

        setupClickListener
('changetype-all', []);
        setupClickListener
('changetype-address', ['address']);
        setupClickListener
('changetype-establishment', ['establishment']);
        setupClickListener
('changetype-geocode', ['geocode']);

        document
.getElementById('use-strict-bounds')
           
.addEventListener('click', function() {
              console
.log('Checkbox clicked! New state=' + this.checked);
              autocomplete
.setOptions({strictBounds: this.checked});
           
});
     
}
   
</script>
   
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"
       
async defer></script>
 
</body>
</html>



W3Doc에서 제공하는 튜토리얼

댓글