我正在使用如下的地方自动完成 api:
let place: google.maps.places.PlaceResult = autocomplete.getPlace();
//verify result
if (place.geometry === undefined || place.geometry === null) {
return;
}
console.log("formated address:", place.formatted_address)
console.log("plus cordinates:", place.plus_code)
我可以看到 formatted_addresss 正在打印,但 plus_code 未定义。根据文档 https://developers.google.com/maps/documentation/javascript/reference/places-service#PlaceResult
我应该看到它。
物体的反应是
address_components: (8) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
adr_address: "<span class=\"street-address\">12112 Anderson Mill Rd</span>, <span class=\"locality\">Austin</span>, <span class=\"region\">TX</span> <span class=\"postal-code\">78726</span>, <span class=\"country-name\">USA</span>"
formatted_address: "12112 Anderson Mill Rd, Austin, TX 78726, USA"
geometry: {location: _.Ee, viewport: _.Cf}
html_attributions: []
icon: "https://maps.gstatic.com/mapfiles/place_api/icons/v1/png_71/geocode-71.png"
icon_background_color: "#7B9EB0"
icon_mask_base_uri: "https://maps.gstatic.com/mapfiles/place_api/icons/v2/generic_pinlet"
name: "12112 Anderson Mill Rd"
place_id: "ChIJU9d0CWEyW4YR-mTqmuuXqUY"
reference: "ChIJU9d0CWEyW4YR-mTqmuuXqUY"
types: ['premise']
url: "https://maps.google.com/?q=12112+Anderson+Mill+Rd,+Austin,+TX+78726,+USA&ftid=0x865b32610974d753:0x46a997eb9aea64fa"
utc_offset: (...)
utc_offset_minutes: -300
vicinity: "Austin"
根据文档,“plus_code”是optional:
plus_code可选
类型:PlacePlusCode 可选
为地点定义开放位置代码或“加号”。
这意味着它不能保证在那里。
您可以编写代码来处理不存在的情况。
代码片段:
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
center: {
lat: 30.307984,
lng: -97.753401
},
zoom: 13,
mapTypeControl: false,
});
google.maps.event.addListener(map, "bounds_changed", function() {
console.log("bounds=" + map.getBounds().toUrlValue(6));
});
google.maps.event.addListener(map, "zoom_changed", function() {
console.log("zoom=" + map.getZoom());
});
google.maps.event.addListener(map, "center_changed", function() {
console.log("center=" + map.getCenter().toUrlValue(6));
})
const card = document.getElementById("pac-card");
const input = document.getElementById("pac-input");
const biasInputElement = document.getElementById("use-location-bias");
const strictBoundsInputElement = document.getElementById("use-strict-bounds");
const options = {
// fields: ["formatted_address", "geometry", "name", "plus_code"],
strictBounds: false,
types: ["establishment"],
};
map.controls[google.maps.ControlPosition.TOP_LEFT].push(card);
const autocomplete = new google.maps.places.Autocomplete(input, options);
// 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);
const infowindow = new google.maps.InfoWindow();
const infowindowContent = document.getElementById("infowindow-content");
infowindow.setContent(infowindowContent);
const marker = new google.maps.Marker({
map,
anchorPoint: new google.maps.Point(0, -29),
});
autocomplete.addListener("place_changed", () => {
infowindow.close();
marker.setVisible(false);
const place = autocomplete.getPlace();
if (!place.geometry || !place.geometry.location) {
// 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;
}
console.log("place:" + JSON.stringify(place))
// 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);
}
marker.setPosition(place.geometry.location);
marker.setVisible(true);
document.getElementById("info").innerHTML = "name:" + place.name + "<br>" + place.formatted_address + "<br>";
if (place.plus_code && place.plus_code.compound_code)
document.getElementById("info").innerHTML += place.plus_code.compound_code;
infowindowContent.children["place-name"].textContent = place.name;
infowindowContent.children["place-address"].textContent =
place.formatted_address;
if (place.plus_code && place.plus_code.compound_code)
infowindowContent.children["place-pluscode"].textContent = place.plus_code.compound_code;
else
infowindowContent.children["place-pluscode"].textContent = "";
infowindow.open(map, marker);
});
// Sets a listener on a radio button to change the filter type on Places
// Autocomplete.
function setupClickListener(id, types) {
const radioButton = document.getElementById(id);
radioButton.addEventListener("click", () => {
autocomplete.setTypes(types);
input.value = "";
});
}
setupClickListener("changetype-all", []);
setupClickListener("changetype-address", ["address"]);
setupClickListener("changetype-establishment", ["establishment"]);
setupClickListener("changetype-geocode", ["geocode"]);
setupClickListener("changetype-cities", ["(cities)"]);
setupClickListener("changetype-regions", ["(regions)"]);
biasInputElement.addEventListener("change", () => {
if (biasInputElement.checked) {
autocomplete.bindTo("bounds", map);
} else {
// User wants to turn off location bias, so three things need to happen:
// 1. Unbind from map
// 2. Reset the bounds to whole world
// 3. Uncheck the strict bounds checkbox UI (which also disables strict bounds)
autocomplete.unbind("bounds");
autocomplete.setBounds({
east: 180,
west: -180,
north: 90,
south: -90
});
strictBoundsInputElement.checked = biasInputElement.checked;
}
input.value = "";
});
strictBoundsInputElement.addEventListener("change", () => {
autocomplete.setOptions({
strictBounds: strictBoundsInputElement.checked,
});
if (strictBoundsInputElement.checked) {
biasInputElement.checked = strictBoundsInputElement.checked;
autocomplete.bindTo("bounds", map);
}
input.value = "";
});
}
window.initMap = initMap;
#map {
height: 80%;
}
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 {
background-color: #fff;
border: 0;
border-radius: 2px;
box-shadow: 0 1px 4px -1px rgba(0, 0, 0, 0.3);
margin: 10px;
padding: 0 0.5em;
font: 400 18px Roboto, Arial, sans-serif;
overflow: hidden;
font-family: Roboto;
padding: 0;
}
#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;
}
<!DOCTYPE html>
<html>
<head>
<title>Place Autocomplete</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
</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">establishment</label>
<input type="radio" name="type" id="changetype-address" />
<label for="changetype-address">address</label>
<input type="radio" name="type" id="changetype-geocode" />
<label for="changetype-geocode">geocode</label>
<input type="radio" name="type" id="changetype-cities" />
<label for="changetype-cities">(cities)</label>
<input type="radio" name="type" id="changetype-regions" />
<label for="changetype-regions">(regions)</label>
</div>
<br />
<div id="strict-bounds-selector" class="pac-controls">
<input type="checkbox" id="use-location-bias" value="" />
<label for="use-location-bias">Bias to map viewport</label>
<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" value="12112 Anderson Mill Rd, Austin, Texas" />
</div>
</div>
<div id="map"></div>
<div id="info"></div>
<div id="infowindow-content">
<span id="place-name" class="title"></span><br />
<span id="place-address"></span><br />
<span id="place-pluscode"></span>
</div>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap&libraries=places&v=weekly" defer></script>
</body>
</html>