mardi 7 juillet 2020

How Do I Take An Input From A Textbox And Use It To Search For a Location On Google Maps?

I am working on a project and one of the goals is for the user to put a location's name into a textbox and press a button to search for a location on a Google map.

<h2>How to Use</h2>
<p>To use this program, enter a location then click the button to search for it.</p>

<h2>Search Location</h2>
Location: <input type="text" id="myText" value="">
<!-- This creates a textbox --->

<button onclick="searchFunction()">Search</button>
<!-- This creates a button --->

<p id="demo"></p>

<script>
  function searchFunction() {
    var location = document.getElementById("myText").value;
  }
</script>

<style>
  #map {
    height: 400px;
    width: 100%;
  }
</style>
</head>

<body>


  <h1>Google Map</h1>
  <div id="map"></div>
  <script>
    function initMap() {
      var options = {
        zoom: 12,
        center: {
          lat: 39.7684,
          lng: -86.1581
        }

      }
      var map = new google.maps.Map(document.getElementById('map'), options);
    }
  </script>
  <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBhrDQVoc_0FJx4Hy4tsFMKtmX78VNTUhw&callback=initMap">
  </script>

  <script>
    if (location == "Children's Museum") {
      function initMap() {
        var options = {
          zoom: 12,
          center: {
            lat: 39.810230,
            lng: -86.158882
          }

        }
        var map = new google.maps.Map(document.getElementById('map'), options);

        var locationTestPosition = {
          lat: 39.810230,
          lng: -86.158882
        };
        var marker = new google.maps.Marker({
          position: locationTestPosition,
          map: map
        });
      }
  </script>
  }

This is the code that I currently have. The idea is that the user will insert a place's name and then using an if/else statement, the code will place a marker on the map at the proper coordinates. I cannot seem to get it to work though. I am new to HTML and JavaScript, so this is a lot of learning for me.

Aucun commentaire:

Enregistrer un commentaire