Google Maps JavaScript API에서 Place ID finder은 어떻습니까?
place ID은 장소를 고유하게 식별하는 텍스트 식별자입니다. 또한 기업, 명소, 공원 및 교차로를 포함한 대부분의 위치에서 사용할 수 있습니다. 이 ID는 안정적이므로 장소의 장소 ID를 확인한 후에 그 장소를 다음에 찾을 때 해당 값을 재사용 할 수 있습니다.
Google 지역 정보 API 및 여러 Google지도 API에서 동일한 장소 ID를 사용할 수 있습니다. 예를 들어, 장소 API에있는 장소의 Google지도 자바 스크립트 API의 Google지도 지오 코딩 API의 Google지도 삽입 API를과 Google지도 도로를 참조하기 위해 같은 장소 ID를 사용할 수 있습니다 API.
다음은 이에 대한 샘플 코드입니다.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -33.8688, lng: 151.2195},
zoom: 13
});
var input = document.getElementById('pac-input');
var autocomplete = new google.maps.places.Autocomplete(input);
autocomplete.bindTo('bounds', map);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
map: map
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
autocomplete.addListener('place_changed', function() {
infowindow.close();
var place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
// Set the position of the marker using the place ID and location.
marker.setPlace({
placeId: place.place_id,
location: place.geometry.location
});
marker.setVisible(true);
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
'Place ID: ' + place.place_id + '<br>' +
place.formatted_address);
infowindow.open(map, marker);
});
}
그냥이 예는 환경 라이브러리가 필요 있다는 사실을. API를 처음로드 할 때 libraries = places 매개 변수를 포함 시키십시오.
script src="https://maps.googleapis.com/maps/api/jskey=YOUR_API_KEY&libraries=places"
: 예를 들어