Javascript 如果地址、邮政编码和城市位于不同的输入字段中,如何进行地理定位

Javascript 如果地址、邮政编码和城市位于不同的输入字段中,如何进行地理定位,javascript,google-maps-api-3,geolocation,Javascript,Google Maps Api 3,Geolocation,我需要分开的地址,邮政编码和城市的输入。现在我想知道我怎样才能找到。在Google Devleloper上,只有一个输入字段。有人知道我如何用三个输入字段进行地理定位吗?只需将三个字段连接成一个字符串。。。 如果有一个有效的API_密钥,这种方法应该可以正常工作 根据您的请求,这里有3个字段。有地址、城市、邮政编码。我已将延迟更改为美国纽约布鲁克林 祝你好运 <!DOCTYPE html> <html> <head> <title>Ge

我需要分开的地址,邮政编码和城市的输入。现在我想知道我怎样才能找到。在Google Devleloper上,只有一个输入字段。有人知道我如何用三个输入字段进行地理定位吗?

只需将三个字段连接成一个字符串。。。 如果有一个有效的API_密钥,这种方法应该可以正常工作


根据您的请求,这里有3个字段。有地址、城市、邮政编码。我已将延迟更改为美国纽约布鲁克林

祝你好运

<!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>
<!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>