I am trying to make a page that gives a 3 day weather forecast with a map. I am using the Openweathermap API and some PHP to get the forecast. I am using Google's Maps JavaScript API to get my map. The page has a single input field. I can make it so that submitting input will give me the weather forecast. I can make it so that input will give me the map. But I cannot get both to happen at the same time.
Here is the HTML form
<form class="example" method="GET" action="index.php" style="margin:auto;max-width:300px">
<h3>Type In a Zipcode</h3>
<br><p>For Example 160-0005</p>
<!--test-->
<input id="address" type="textbox" value="" name="q" required>
<button id= "OK!" type="submit"><i class="fa fa-search"></i></button>
Here is the PHP
date_default_timezone_set($get['timezone']);
$city = $_GET['q'];
$string = "http://api.openweathermap.org/data/2.5/forecast?zip=".$city.",jp&units=metric&cnt=3&appid=[API Key]";
here is the JavaScript
<script>
"use strict";
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 15,
center: {
lat: -34.397,
lng: 150.644
}
});
const geocoder = new google.maps.Geocoder();
document.getElementById("OK!").addEventListener("click", () => {
geocodeAddress(geocoder, map);
});
}
function geocodeAddress(geocoder, resultsMap) {
const address = document.getElementById("address").value;
geocoder.geocode(
{
address: address
},
(results, status) => {
if (status === "OK") {
resultsMap.setCenter(results[0].geometry.location);
new google.maps.Marker({
map: resultsMap,
position: results[0].geometry.location
});
} else {
alert(
"Geocode was not successful for the following reason: " + status
);
}
}
);
}
</script>
In the HTML form code, if I change the button type to "button", the JavaScript works and displays the map. If I change it to type "submit", the PHP works and gives me the forecast. Not really sure what is going on there. What can I do to enter location information and get the map and the forecast at the same time?