JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html>
<head>
<title>Geocoding service</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<!-- 將map格式的高度改成0%就變成不會將地圖顯示在頁面上了 -->
<style>
html, body {
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 0%;
}
#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
<div id="floating-panel">
<input id="address" type="textbox" value="台中市南區興大路145號">
<input id="submit" type="button" value="Geocode">
</div>
-->
<div id="map"></div>
<!-- 多加了這區 -->
<div id="warnings_panel" style="width:100%;height:50%;text-align:center"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 14,
center: {lat: 24.1241841, lng: 120.6774165}
});
var geocoder = new google.maps.Geocoder();
// 註解掉addEventListener,直接將地址當作第三個參數傳遞
// document.getElementById('submit').addEventListener('click', function() {
geocodeAddress(geocoder, map, "台中市南區興大路145號");
// });
}
function geocodeAddress(geocoder, resultsMap, address) { // 增加第三個參數address
// 註解掉下列這行, 不是讓使用者在頁面上輸入地址, 而是直接接收自參數
// var address = document.getElementById('address').value;
geocoder.geocode({'address': address}, function(results, status) {
if (status === google.maps.GeocoderStatus.OK) {
// 註解掉下列動作, 因為地圖已經不顯示, 所以也沒需要設定中心位置
// resultsMap.setCenter(results[0].geometry.location);
// 多加了下列兩行之一, 把經緯度座標顯示在warnings_panel或傳回
document.getElementById('warnings_panel').innerHTML = results[0].geometry.location;
// return(results[0].geometry.location);
// 註解掉下列動作, 因為地圖已經不顯示, 所以也沒需要標示位置氣球
// var marker = new...