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

前端 未结 7 1545
终归单人心
终归单人心 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:18

    http://maps.googleapis.com/maps/api/geocode/OUTPUT?address=YOUR_LOCATION&sensor=true

    OUTPUT = json or xml;

    for detail information about google map api go through url:

    http://code.google.com/apis/maps/documentation/geocoding/index.html

    Hope this will help

    0 讨论(0)
  • You can use the Google Geocoder service in the Google Maps API to convert from your location name to a latitude and longitude. So you need some code like:

    var geocoder = new google.maps.Geocoder();
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK)
      {
          // do something with the geocoded result
          //
          // results[0].geometry.location.latitude
          // results[0].geometry.location.longitude
      }
    });
    

    Update

    Are you including the v3 javascript API?

    <script type="text/javascript"
         src="http://maps.google.com/maps/api/js?sensor=set_to_true_or_false">
    </script> 
    
    0 讨论(0)
  • 2020-11-30 21:26

    The below is the code that i used. It's working perfectly.

    var geo = new google.maps.Geocoder;
    geo.geocode({'address':address},function(results, status){
        if (status == google.maps.GeocoderStatus.OK) {              
            var myLatLng = results[0].geometry.location;
    
            // Add some code to work with myLatLng              
    
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
    

    Hope this will help.

    0 讨论(0)
  • 2020-11-30 21:26

    I would suggest the following code, you can use this <script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script> to get the latitude and longitude of a location, although it may not be so accurate however it worked for me;

    code snippet below

    <!DOCTYPE html>
    <html>
    <head>
    <title>Using Javascript's Geolocation API</title>
    
    <script type="text/javascript" src="http://j.maxmind.com/app/geoip.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    </head>
    <body>
    <div id="mapContainer"></div> 
    
    <script type="text/javascript">
    
            var lat = geoip_latitude();
            var long = geoip_longitude();
            document.write("Latitude: "+lat+"</br>Longitude: "+long);
    
    </script> 
    </body>
    </html> 
    
    0 讨论(0)
  • 2020-11-30 21:28

    I hope this will be more useful for future scope contain auto complete Google API feature with latitude and longitude

    var latitude = place.geometry.location.lat();
    var longitude = place.geometry.location.lng();  
    

    Complete View

    <!DOCTYPE html>
    <html>
        <head>
        <title>Place Autocomplete With Latitude & Longitude </title>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <style>
    #pac-input {
        background-color: #fff;
        padding: 0 11px 0 13px;
        width: 400px;
        font-family: Roboto;
        font-size: 15px;
        font-weight: 300;
        text-overflow: ellipsis;
    }
    #pac-input:focus {
        border-color: #4d90fe;
        margin-left: -1px;
        padding-left: 14px;  /* Regular padding-left + 1. */
        width: 401px;
    }
    }
    </style>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
        <script>
    
    
      function initialize() {
            var address = (document.getElementById('pac-input'));
            var autocomplete = new google.maps.places.Autocomplete(address);
            autocomplete.setTypes(['geocode']);
            google.maps.event.addListener(autocomplete, 'place_changed', function() {
                var place = autocomplete.getPlace();
                if (!place.geometry) {
                    return;
                }
    
            var address = '';
            if (place.address_components) {
                address = [
                    (place.address_components[0] && place.address_components[0].short_name || ''),
                    (place.address_components[1] && place.address_components[1].short_name || ''),
                    (place.address_components[2] && place.address_components[2].short_name || '')
                    ].join(' ');
            }
            /*********************************************************************/
            /* var address contain your autocomplete address *********************/
            /* place.geometry.location.lat() && place.geometry.location.lat() ****/
            /* will be used for current address latitude and longitude************/
            /*********************************************************************/
            document.getElementById('lat').innerHTML = place.geometry.location.lat();
            document.getElementById('long').innerHTML = place.geometry.location.lng();
            });
      }
    
       google.maps.event.addDomListener(window, 'load', initialize);
    
        </script>
        </head>
        <body>
    <input id="pac-input" class="controls" type="text"
            placeholder="Enter a location">
    <div id="lat"></div>
    <div id="long"></div>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-30 21:36

    I hope this can help someone in the future.

    You can use the Google Geocoding API, as said before, I had to do some work with this recently, I hope this helps:

    <!DOCTYPE html>
    <html>
        <head>
            <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
            <script type="text/javascript">
            function initialize() {
            var address = (document.getElementById('my-address'));
            var autocomplete = new google.maps.places.Autocomplete(address);
            autocomplete.setTypes(['geocode']);
            google.maps.event.addListener(autocomplete, 'place_changed', function() {
                var place = autocomplete.getPlace();
                if (!place.geometry) {
                    return;
                }
    
            var address = '';
            if (place.address_components) {
                address = [
                    (place.address_components[0] && place.address_components[0].short_name || ''),
                    (place.address_components[1] && place.address_components[1].short_name || ''),
                    (place.address_components[2] && place.address_components[2].short_name || '')
                    ].join(' ');
            }
          });
    }
    function codeAddress() {
        geocoder = new google.maps.Geocoder();
        var address = document.getElementById("my-address").value;
        geocoder.geocode( { 'address': address}, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
    
          alert("Latitude: "+results[0].geometry.location.lat());
          alert("Longitude: "+results[0].geometry.location.lng());
          } 
    
          else {
            alert("Geocode was not successful for the following reason: " + status);
          }
        });
      }
    google.maps.event.addDomListener(window, 'load', initialize);
    
            </script>
        </head>
        <body>
            <input type="text" id="my-address">
            <button id="getCords" onClick="codeAddress();">getLat&Long</button>
        </body>
    </html>
    

    Now this has also an autocomlpete function which you can see in the code, it fetches the address from the input and gets auto completed by the API while typing.

    Once you have your address hit the button and you get your results via alert as required. Please also note this uses the latest API and it loads the 'places' library (when calling the API uses the 'libraries' parameter).

    Hope this helps, and read the documentation for more information, cheers.

    Edit #1: Fiddle

    0 讨论(0)
提交回复
热议问题