我使用此代码生成带有自动填充功能的google地图,某些字段会自动填充lat,lng,adress和town等信息。问题是我无法找到适合城镇的正确价值。
这是我的代码
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
.controls {
margin-top: 10px;
border: 1px solid transparent;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
height: 32px;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#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: 300px;
}
#pac-input:focus {
border-color: #4d90fe;
}
.pac-container {
font-family: Roboto;
}
#type-selector {
color: #fff;
background-color: #4d90fe;
padding: 5px 11px 0px 11px;
}
#type-selector label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
</style>
</head>
<body>
<input id="pac-input" class="controls" type="text"
placeholder="Enter a location">
<div id="map" style="width:500px; height:500px;"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -33.8688, lng: 151.2195},
zoom: 13
});
var options = {
componentRestrictions: {country: "it"}
};
var input = /** @type {!HTMLInputElement} */(
document.getElementById('pac-input'));
var autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.bindTo('bounds', map);
var infowindow = new google.maps.InfoWindow();
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) {
window.alert("Autocomplete's returned place contains no geometry");
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.setIcon(/** @type {google.maps.Icon} */({
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(35, 35)
}));
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(' ');
}
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.open(map, marker);
var adress_form = address;
document.getElementById('adress').value = adress_form
var lat_form= place.geometry.location.lat();
document.getElementById('lat').value = lat_form
var lng_form= place.geometry.location.lng();
document.getElementById('lng').value = lng_form
function extractFromAdress(components, type){
for (var i=0; i<components.length; i++)
for (var j=0; j<components[i].types.length; j++)
if (components[i].types[j]==type) return components[i].long_name;
return "";
}
var town = extractFromAdress(results[0].address_components, "locality");
document.getElementById('city').value = town
});
}
</script>
<form method="post" action="a.php">
<input placeholder="adress" type="text" id="adress" name="adress">
<input placeholder="city" type="text" id="city" name="city">
<input placeholder="lat" type="text" id="lat" name="lat">
<input placeholder="lng" type="text" id="lng" name="lng">
<input placeholder="latlng" type="text" id="latlng" name="latlng">
<input placeholder="name" type="text" id="name" name="name">
<input type="submit" value="inserisci">
<script src="https://maps.googleapis.com/maps/api/js?key=MYKEY&signed_in=true&libraries=places&callback=initMap"
async defer></script>
</form>
</body>
</html>
答案 0 :(得分:1)
我的代码出现了javascript错误:Uncaught ReferenceError: results is not defined
在此行:
var town = extractFromAdress(results[0].address_components, "locality");
那应该是您检索到的地方对象(您从中获取了其余信息):
var place = autocomplete.getPlace();
然后它起作用(如果结果具有“locality”类型的结果)
var town = extractFromAdress(place.address_components, "locality");
document.getElementById('city').value = town;
代码段
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: -33.8688,
lng: 151.2195
},
zoom: 13
});
var options = {
componentRestrictions: {
country: "it"
}
};
var input = /** @type {!HTMLInputElement} */ (
document.getElementById('pac-input'));
var autocomplete = new google.maps.places.Autocomplete(input, options);
autocomplete.bindTo('bounds', map);
var infowindow = new google.maps.InfoWindow();
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) {
window.alert("Autocomplete's returned place contains no geometry");
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.setIcon( /** @type {google.maps.Icon} */ ({
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(35, 35)
}));
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(' ');
}
infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
infowindow.open(map, marker);
var adress_form = address;
document.getElementById('adress').value = adress_form
var lat_form = place.geometry.location.lat();
document.getElementById('lat').value = lat_form
var lng_form = place.geometry.location.lng();
document.getElementById('lng').value = lng_form
function extractFromAdress(components, type) {
for (var i = 0; i < components.length; i++)
for (var j = 0; j < components[i].types.length; j++)
if (components[i].types[j] == type) return components[i].long_name;
return "";
}
var town = extractFromAdress(place.address_components, "locality");
document.getElementById('city').value = town;
});
}
google.maps.event.addDomListener(window, 'load', initMap);
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
.controls {
margin-top: 10px;
border: 1px solid transparent;
border-radius: 2px 0 0 2px;
box-sizing: border-box;
-moz-box-sizing: border-box;
height: 32px;
outline: none;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}
#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: 300px;
}
#pac-input:focus {
border-color: #4d90fe;
}
.pac-container {
font-family: Roboto;
}
#type-selector {
color: #fff;
background-color: #4d90fe;
padding: 5px 11px 0px 11px;
}
#type-selector label {
font-family: Roboto;
font-size: 13px;
font-weight: 300;
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
<input id="pac-input" class="controls" type="text" placeholder="Enter a location">
<div id="map" style="width:500px; height:500px;"></div>
<form method="post" action="a.php">
<input placeholder="adress" type="text" id="adress" name="adress">
<input placeholder="city" type="text" id="city" name="city">
<input placeholder="lat" type="text" id="lat" name="lat">
<input placeholder="lng" type="text" id="lng" name="lng">
<input placeholder="latlng" type="text" id="latlng" name="latlng">
<input placeholder="name" type="text" id="name" name="name">
<input type="submit" value="inserisci">
</form>