0

This is the code I'm working from:

http://jsfiddle.net/njDvn/36/

The weird thing is, the autosuggest only seems to activate on the last input box after I click on the geocode button. This is the code im working from:

<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script>
function getLatLng() {
    var geocoder = new google.maps.Geocoder();
    var address = document.getElementById('address').value;

    var input = document.getElementById('searchTextField');
    var options = {
    types: [],
    };

var autocomplete = new google.maps.places.Autocomplete(input, options);

    geocoder.geocode({
        'address': address
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latLng = results[0].geometry.location;
            $('#lat').val(results[0].geometry.location.lat());
            $('#lng').val(results[0].geometry.location.lng());
        } else {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}
</script>
</head>
    <body>
        <div>
            <input id="address" type="textbox" value="Sydney, NSW">
            <input type="button" value="Geocode" onclick="getLatLng()">
            <input id="lat" type="textbox" value="lat">
            <input id="lng" type="textbox" value="lng">
            <input id="searchTextField" type="text" size="50" value="">

           </div>
      </body>
</html>

​What gives? I've never had this before, but this is the first script I have tried to put together.

2 Answers 2

1

This is due to the following line:

var autocomplete = new google.maps.places.Autocomplete(input, options);

input links it to:

var input = document.getElementById('searchTextField');

which is the last input box.

In addition, you have to click on the geocode button before Google autocomplete links because you have bound the getLatLng() call to the click of the Geocode button:

<input type="button" value="Geocode" onclick="getLatLng()">

Because the Google autocomplete activates from within this function, you cannot initialize it without pressing the button.

Sign up to request clarification or add additional context in comments.

2 Comments

But why do I have to click on geocode button before the searchTextField acts as an autosuggest
Ah I see, could you explain how I can put it in its own function so I dont have to click the button?
0

You are initializing the autocomplete within the getLatLng function so it isn;'t available until the button is clicked and the function gets called

3 Comments

Ah I see, could you explain how I can put it in its own function so I dont have to click the button?
you never call the function. Needs to be in an onload handler so the input exists when the function is called. BTW. Is against Google Maps TOS using geocoder without implementing a map
Ive been on their forums and checked it with them, its ok to show it on the next pages since that is where the user actually uses the geocode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.