我正在制作這個表單,用戶輸入他們的郵政編碼,它會自動完成google.maps.places 下拉串列中的完整地址。唯一缺少的是我打算為其制作隱藏欄位的緯度和經度,然后在適當的欄位中將帖子提交到我的谷歌表格電子表格。
我堅持的是將Geocoding API call與 Autocomplete Javascript 函式結合起來。我想要做的是添加一個帶有自動完成功能的地理編碼 API 呼叫,以便 Lat. 并且 Lng 被發送到隱藏欄位,我已經嘗試了 3 天,我想知道是否有人有解決方案。
這是我的代碼:
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
function initAutocomplete() {
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
{types: ['geocode']});
// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', fillInAddress);
}
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i ) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
}
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
<form id="myform" method="POST"
action="https://script.google.com/macros/s/AKfycbzF_r-4ENEXSMD36Ry_gQ9iSQ5dqfpZiw3VytR7LmpSAwxbEcRelxhGJroi8QCBavEI/exec"
autocomplete="off" class="form" role="form">
<input autocomplete="false" name="hidden" type="text" style="display:none;">
<div class="form-group row">
<label>Business address</label>
<div class="col-sm-4">
<input id="autocomplete" autocomplete="off" autofill="off" placeholder="Postcode" onFocus="geolocate()" type="text"
class="form-control">
</div>
</div>
<br>
<div class="form-group row">
<label>Number</label>
<div class="col-sm-4">
<input name="No." class="form-control" id="street_number" disabled="false" required>
</div>
<label>Street</label>
<div class="col-sm-4">
<input name="Street" class="form-control" id="route" disabled="true" required>
</div>
</div>
<br>
<div class="form-group row">
<label>City</label>
<div class="col-sm-4">
<input name="City" class="form-control field" id="locality" disabled="true" required>
</div>
<label>State</label>
<div class="col-sm-4">
<input name="State" class="form-control" id="administrative_area_level_1" disabled="true">
</div>
</div>
<br>
<div class="form-group row">
<label>Postal/zipcode</label>
<div class="col-sm-4">
<input name="Postcode" class="form-control" id="postal_code" disabled="true" required>
</div>
<label>Country</label>
<div class="col-sm-4">
<input name="Country" class="form-control" id="country" disabled="true">
</div>
</div>
<div class="form-group row">
<label>Lat</label>
<div class="col-sm-4">
<input name="Lat" class="form-control" id="Lat" disabled="true" required>
</div>
<label>Lng</label>
<div class="col-sm-4">
<input name="Lng" class="form-control" id="Lng" disabled="true">
</div>
</div>
</div>
<div class="form-group row">
<div id="submit">
<button type="submit" value="Submit" >Submit</button>
</div>
</div>
</form>
<script
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB_MrpX85obMpsk_eEdfE-iPIt06qbHyt0&libraries=places&callback=initAutocomplete"
async defer></script>
現在我也有這個代碼片段來呼叫谷歌地理編碼 API
document.getElementById('myform').addEventListener('submit', function(e){
e.preventDefault(); //prevent form submit
const place = autocomplete.getPlace(); //get place from autocomplete
if (!place || !place.geometry) { //check if valid location
swal("You have provided an Invalid address","Enter a valid Address", "warning");
return;
}
// It is valid, proceed to geocode!
else {
// Listen for form submit
document.getElementById('myForm').addEventListener('submit', geocode);
function geocode(e){
// Prevent actual submit
e.preventDefault();
var location = document.getElementById('location-input').value; //* I think this is where I might have made a mistake
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
params:{
address: location,
key: 'AIzaSyB_MrpX85obMpsk_eEdfE-iPIt06qbHyt0'
}
})
.then(function(response){
// Log full response
console.log(response);
// Formatted Address
var formattedAddress = response.data.results[0].formatted_address;
// Address Components
var addressComponents = response.data.results[0].address_components;
// Get values from the input fields
var veg_planted = getInputVal('veg_planted');
// Get geometry
var lat = response.data.results[0].geometry.location.lat;
var lng = response.data.results[0].geometry.location.lng;
var coords= (formattedAddress ": " lat "," lng);
console.log(coords);
// Save messages
saveMessage(veg_planted, coords);
})
.catch(function(error){
console.log(error);
});
}
}
});
這個功能可以與自動完成功能集成嗎?我必須讓表格填寫緯度。和液化天然氣。價值觀?如果那里有人有解決方案,那將對我有很大幫助。謝謝你。
uj5u.com熱心網友回復:
您不需要使用地理編碼器。地點 API 在其回應中回傳 lat/lng 坐標(如果有的話)。
PlaceResult.geometry
geometry可選
型別:PlaceGeometry可選
Place 的幾何相關資訊。
PlaceGeometry 介面
定義有關地方幾何的資訊。
屬性
location optional
型別: LatLng可選
地點的位置。
document.getElementById("Lat").value = place.geometry.location.lat();
document.getElementById("Lng").value = place.geometry.location.lng();
作業代碼片段:
var placeSearch, autocomplete;
var componentForm = {
street_number: 'short_name',
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
country: 'long_name',
postal_code: 'short_name'
};
function initAutocomplete() {
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */
(document.getElementById('autocomplete')), {
types: ['geocode']
});
// When the user selects an address from the dropdown, populate the address
// fields in the form.
autocomplete.addListener('place_changed', fillInAddress);
}
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
document.getElementById("Lat").value = place.geometry.location.lat();
document.getElementById("Lng").value = place.geometry.location.lng();
// Get each component of the address from the place details
// and fill the corresponding field on the form.
for (var i = 0; i < place.address_components.length; i ) {
var addressType = place.address_components[i].types[0];
if (componentForm[addressType]) {
var val = place.address_components[i][componentForm[addressType]];
document.getElementById(addressType).value = val;
}
}
}
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
<form id="myform" method="POST" action="https://script.google.com/macros/s/AKfycbzF_r-4ENEXSMD36Ry_gQ9iSQ5dqfpZiw3VytR7LmpSAwxbEcRelxhGJroi8QCBavEI/exec" autocomplete="off" class="form" role="form">
<input autocomplete="false" name="hidden" type="text" style="display:none;">
<div class="form-group row">
<label>Business address</label>
<div class="col-sm-4">
<input id="autocomplete" autocomplete="off" autofill="off" placeholder="Postcode" onFocus="geolocate()" type="text" class="form-control">
</div>
</div>
<br>
<div class="form-group row">
<label>Number</label>
<div class="col-sm-4">
<input name="No." class="form-control" id="street_number" disabled="false" required>
</div>
<label>Street</label>
<div class="col-sm-4">
<input name="Street" class="form-control" id="route" disabled="true" required>
</div>
</div>
<br>
<div class="form-group row">
<label>City</label>
<div class="col-sm-4">
<input name="City" class="form-control field" id="locality" disabled="true" required>
</div>
<label>State</label>
<div class="col-sm-4">
<input name="State" class="form-control" id="administrative_area_level_1" disabled="true">
</div>
</div>
<br>
<div class="form-group row">
<label>Postal/zipcode</label>
<div class="col-sm-4">
<input name="Postcode" class="form-control" id="postal_code" disabled="true" required>
</div>
<label>Country</label>
<div class="col-sm-4">
<input name="Country" class="form-control" id="country" disabled="true">
</div>
</div>
<div class="form-group row">
<label>Lat</label>
<div class="col-sm-4">
<input name="Lat" class="form-control" id="Lat" disabled="true" required>
</div>
<label>Lng</label>
<div class="col-sm-4">
<input name="Lng" class="form-control" id="Lng" disabled="true">
</div>
</div>
</div>
<div class="form-group row">
<div id="submit">
<button type="submit" value="Submit">Submit</button>
</div>
</div>
</form>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyB_MrpX85obMpsk_eEdfE-iPIt06qbHyt0&libraries=places&callback=initAutocomplete" async defer></script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/482984.html
標籤:javascript 形式 谷歌地图 api-3 自动完成 地理编码
上一篇:轉移輸入值
