0

I am trying to call Google map api where it will auto complete address in location field. Now, I have 2 location field where I want to auto field it.

I have wrote following JavaScript code.

var options = {
componentRestrictions: { country: 'usa' }
};

google.maps.event.addDomListener(window, 'load', function () {

if (document.getElementById("address")) {

    var places = new google.maps.places.Autocomplete(document.getElementById('address'), options);

    google.maps.event.addListener(places, 'place_changed', function () {
        var place = places.getPlace();
        var address = place.formatted_address;
        var latitude = place.geometry.location.lat();
        var longitude = place.geometry.location.lng();
        document.getElementById("Latitude").value = latitude;
        document.getElementById("Longitude").value = longitude;
    });
}

if (document.getElementById("addressModal")) {

    var placesModal = new google.maps.places.Autocomplete(document.getElementById('addressModal'), options);

    google.maps.event.addListener(placesModal, 'place_changed', function () {

        var placeModal = placesModal.getPlace();
        var latitudeModal = placeModal.geometry.location.lat();
        var longitudeModal = placeModal.geometry.location.lng();

        document.getElementById("Latitude").value = latitudeModal;
        document.getElementById("Longitude").value = longitudeModal;
    });
}});

It is auto completing for 1st location field (id = address) but it is not working for 2nd location field (id = addressModal) Please help. Thanks in advance.

rovy
  • 2,481
  • 5
  • 18
  • 26
  • related question [Google Maps - Autocomplete & Directions API - trigger onchange() for dropdown list?](http://stackoverflow.com/questions/18690444/google-maps-autocomplete-directions-api-trigger-onchange-for-dropdown-li) – geocodezip Feb 06 '15 at 01:19
  • possibly similar question [Google Maps V3 autocomplete input jquery Mobile ](http://stackoverflow.com/questions/24252188/google-maps-v3-autocomplete-input-jquery-mobile) – geocodezip Feb 06 '15 at 01:25
  • 1
    I get a javascript error with your code `options` is not defined, if [I fix that both fields work (with the error, neither works)](http://jsfiddle.net/3aLqbbz1/1/). – geocodezip Feb 06 '15 at 02:05
  • Hey. @geocodezip thanks for reply. I have edited question. I forgot to add options variable. – rovy Feb 06 '15 at 17:53
  • 1
    You are doing something you aren't telling us. [Your code (still) works for me if I put it in an onload function](http://jsfiddle.net/3aLqbbz1/3/). What javascript errors are you getting? Are you including the places library (and the Google Maps Javascript API v3)? – geocodezip Feb 06 '15 at 19:23

0 Answers0