我需要分隔地址,郵編和城市的輸入。現在我想知道我可以如何凝膠化。在Google Devleloper documentation中,只有一個輸入字段。有人知道我可以如何用三個輸入字段進行地理定位嗎?如何在地址,郵政編碼和城市位於不同的輸入字段中進行地理定位
0
A
回答
0
正好連接三個字段連接成一個字符串...
像這樣的東西應該可以正常工作,提供有效的API_KEY
<label for="street">Street: </label><input type="text" id="street" /><br/>
<label for="city">City: </label><input type="text" id="city" /><br/>
<label for="zip">Zip code: </label><input type="text" id="zip" /><br/>
<label for="submit">Submit: </label><input type="submit" id="submit" /><br/>
<div id="map"></div>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&"></script>
<script>
window.addEventListener('load', initMap, false);
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {lat: -34.397, lng: 150.644}
});
var geocoder = new google.maps.Geocoder();
document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
}
function geocodeAddress(geocoder, resultsMap) {
// CONCATENATE ADDRESS HERE
var address = document.getElementById('street').value + ' ' +
document.getElementById('city').value + ' ' +
document.getElementById('zip').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
0
這是一個根據您的請求3場。地址,城市,郵政編碼。我已將美國的Brooklin New York的延遲時間改爲。
祝您好運
的<!DOCTYPE html>
<html>
<head>
<title>Geocoding service</title>
<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%;
}
#floating-panel {
position: absolute;
top: 10px;
left: 25%;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
text-align: center;
font-family: 'Roboto','sans-serif';
line-height: 30px;
padding-left: 10px;
}
</style>
</head>
<body>
<div id="floating-panel">
Address: <input id="address" type="textbox" value="United States">
City: <input id="city" type="textbox" value="New York">
Zip Code: <input id="zip" type="textbox" value="11206">
<input id="submit" type="button" value="Geocode">
</div>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {lat: 40.692129, lng: -73.5376278}
});
var geocoder = new google.maps.Geocoder();
document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map);
});
}
function geocodeAddress(geocoder, resultsMap) {
var address = document.getElementById('address').value;
var city = document.getElementById('city').value;
var zip = document.getElementById('zip').value;
geocoder.geocode({'address': address + ", " + city + " " + zip}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
resultsMap.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: ' + status);
}
});
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=ADD YOUR API KEY HERE&signed_in=true&callback=initMap"
async defer></script>
</body>
</html>
相關問題
- 1. 地理位置和從輸入地址獲取城市(Rails)
- 2. 如何獲得給定郵政編碼的州(城市和地址不需要)?
- 3. JavaScript谷歌地圖地理位置如何從中獲取郵政編碼,城市和州名?
- 4. 轉換地址(街道地址,城市,州,郵政編碼),以FIPS代碼
- 5. 使用地圖包和核心位置現在從郵政編碼(郵政編碼)或城市/州
- 6. 谷歌地圖:如何在多地址輸入中獲取城市+郵政編碼+州+國
- 7. 同一字段中的城市或郵政編碼搜索
- 8. 郵政編碼地理位置
- 9. Bing的地理定位地圖API - 墨西哥郵政編碼
- 10. 基於ASP.NET/SQL數據庫字段顯示Google地圖地址,城市,國家,郵政編碼?
- 11. jQuery驗證郵政編碼和城市
- 12. 從Google地圖中的地理位置獲取郵政編碼
- 13. 來自地址SQL的獨立城市,州和郵政編碼SQL
- 14. 尋找從地址簿中選擇的城市,國家,郵政編碼
- 15. Hardcode是用於地理編碼的郵政地址
- 16. 從郵政編碼獲取城市名稱谷歌地理編碼
- 17. 使用郵政編碼,城市或座標查找位置
- 18. onsubmit用於郵政編碼文本輸入。 XMLhttprequest:輸入zip,返回城市
- 19. 地理編碼JSON得到街道地址,城市,國家,郵編
- 20. 如何從英國郵政編碼獲取地理位置
- 21. 如何使用郵政編碼獲取地理位置
- 22. 地理位置:如何使用地址/城市/地點派生國家?
- 23. 美國郵政編碼位於2郵政編碼的中間?
- 24. 如何從街道地址獲取城市,州和郵編?
- 25. 我想將郵政編碼轉換爲大致位於該郵政編碼中心的地址
- 26. 郵政編碼/郵政編碼只屬於一個街道+城市
- 27. 拆分完整地址到街道,城市,州,國家,郵政編碼
- 28. 地理編碼 - 將多個地址分組到多個城市
- 29. 縮小地理編碼地址城市僅
- 30. C#獲取城市和郵編從地址字符串