Get latitude and longitude based on location name with Google Autocomplete API

前端 未结 7 1546
终归单人心
终归单人心 2020-11-30 20:49

I have a textbox in my page which gets a location name and button with text getLat&Long. Now on clicking my button I have to show an alert of latitude

相关标签:
7条回答
  • 2020-11-30 21:36

    Enter the location by Autocomplete and rest of all the fields: latitude and Longititude values get automatically filled.
    Replace API KEY with your Google API key

    <html>
    <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
    
    <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
    </head>
    
    <body>
    <textarea placeholder="Enter Area name to populate Latitude and Longitude" name="address" onFocus="initializeAutocomplete()" id="locality" ></textarea><br>
    
    <input type="text" name="city" id="city" placeholder="City" value="" ><br>
    <input type="text" name="latitude" id="latitude" placeholder="Latitude" value="" ><br>
    <input type="text" name="longitude" id="longitude" placeholder="Longitude" value="" ><br>
    <input type="text" name="place_id" id="location_id" placeholder="Location Ids" value="" ><br>
    
    <script type="text/javascript">
      function initializeAutocomplete(){
        var input = document.getElementById('locality');
        // var options = {
        //   types: ['(regions)'],
        //   componentRestrictions: {country: "IN"}
        // };
        var options = {}
    
        var autocomplete = new google.maps.places.Autocomplete(input, options);
    
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
          var place = autocomplete.getPlace();
          var lat = place.geometry.location.lat();
          var lng = place.geometry.location.lng();
          var placeId = place.place_id;
          // to set city name, using the locality param
          var componentForm = {
            locality: 'short_name',
          };
          for (var i = 0; i < place.address_components.length; i++) {
            var addressType = place.address_components[i].types[0];
            if (componentForm[addressType]) {
              var val = place.address_components[i][componentForm[addressType]];
              document.getElementById("city").value = val;
            }
          }
          document.getElementById("latitude").value = lat;
          document.getElementById("longitude").value = lng;
          document.getElementById("location_id").value = placeId;
        });
      }
    </script>
    </body>
    </html>
    
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="//maps.googleapis.com/maps/api/js?libraries=places&key=API KEY"></script>
    
    
    <script src="https://fonts.googleapis.com/css?family=Roboto:300,400,500></script>
    
    0 讨论(0)
提交回复
热议问题