How to get country name from autocomplete and dragend?











up vote
0
down vote

favorite












I tried the following



  google.maps.event.addListener(marker, 'dragend', function(marker) {
var latLng = marker.latLng;
currentLatitude = latLng.lat();
currentLongitude = latLng.lng();
var latlng = {
lat: currentLatitude,
lng: currentLongitude
};
var geocoder = new google.maps.Geocoder;
geocoder.geocode({
'location': latlng
}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
input.value = results[0].formatted_address;
var arrAddress = results[0].address_components;
$.each(arrAddress, function (i, address_component) {
if(arrAddress.types.includes('country')) {
console.log(address_component.long_name);
}
});
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
});

autocomplete.addListener('place_changed', function() {
var place = autocomplete.getPlace();
if (!place.geometry) {
return;
}
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
}
marker.setPosition(place.geometry.location);
currentLatitude = place.geometry.location.lat();
currentLongitude = place.geometry.location.lng();
var arrAddress = place.address_components;
$.each(arrAddress, function (i, address_component) {
if(arrAddress.types.includes('country')) {
console.log(address_component.long_name);
}
});
});
};


But I get




Uncaught TypeError: Cannot read property 'includes' of undefined











share|improve this question


























    up vote
    0
    down vote

    favorite












    I tried the following



      google.maps.event.addListener(marker, 'dragend', function(marker) {
    var latLng = marker.latLng;
    currentLatitude = latLng.lat();
    currentLongitude = latLng.lng();
    var latlng = {
    lat: currentLatitude,
    lng: currentLongitude
    };
    var geocoder = new google.maps.Geocoder;
    geocoder.geocode({
    'location': latlng
    }, function(results, status) {
    if (status === 'OK') {
    if (results[0]) {
    input.value = results[0].formatted_address;
    var arrAddress = results[0].address_components;
    $.each(arrAddress, function (i, address_component) {
    if(arrAddress.types.includes('country')) {
    console.log(address_component.long_name);
    }
    });
    } else {
    window.alert('No results found');
    }
    } else {
    window.alert('Geocoder failed due to: ' + status);
    }
    });
    });

    autocomplete.addListener('place_changed', function() {
    var place = autocomplete.getPlace();
    if (!place.geometry) {
    return;
    }
    if (place.geometry.viewport) {
    map.fitBounds(place.geometry.viewport);
    } else {
    map.setCenter(place.geometry.location);
    }
    marker.setPosition(place.geometry.location);
    currentLatitude = place.geometry.location.lat();
    currentLongitude = place.geometry.location.lng();
    var arrAddress = place.address_components;
    $.each(arrAddress, function (i, address_component) {
    if(arrAddress.types.includes('country')) {
    console.log(address_component.long_name);
    }
    });
    });
    };


    But I get




    Uncaught TypeError: Cannot read property 'includes' of undefined











    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I tried the following



        google.maps.event.addListener(marker, 'dragend', function(marker) {
      var latLng = marker.latLng;
      currentLatitude = latLng.lat();
      currentLongitude = latLng.lng();
      var latlng = {
      lat: currentLatitude,
      lng: currentLongitude
      };
      var geocoder = new google.maps.Geocoder;
      geocoder.geocode({
      'location': latlng
      }, function(results, status) {
      if (status === 'OK') {
      if (results[0]) {
      input.value = results[0].formatted_address;
      var arrAddress = results[0].address_components;
      $.each(arrAddress, function (i, address_component) {
      if(arrAddress.types.includes('country')) {
      console.log(address_component.long_name);
      }
      });
      } else {
      window.alert('No results found');
      }
      } else {
      window.alert('Geocoder failed due to: ' + status);
      }
      });
      });

      autocomplete.addListener('place_changed', function() {
      var place = autocomplete.getPlace();
      if (!place.geometry) {
      return;
      }
      if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
      } else {
      map.setCenter(place.geometry.location);
      }
      marker.setPosition(place.geometry.location);
      currentLatitude = place.geometry.location.lat();
      currentLongitude = place.geometry.location.lng();
      var arrAddress = place.address_components;
      $.each(arrAddress, function (i, address_component) {
      if(arrAddress.types.includes('country')) {
      console.log(address_component.long_name);
      }
      });
      });
      };


      But I get




      Uncaught TypeError: Cannot read property 'includes' of undefined











      share|improve this question













      I tried the following



        google.maps.event.addListener(marker, 'dragend', function(marker) {
      var latLng = marker.latLng;
      currentLatitude = latLng.lat();
      currentLongitude = latLng.lng();
      var latlng = {
      lat: currentLatitude,
      lng: currentLongitude
      };
      var geocoder = new google.maps.Geocoder;
      geocoder.geocode({
      'location': latlng
      }, function(results, status) {
      if (status === 'OK') {
      if (results[0]) {
      input.value = results[0].formatted_address;
      var arrAddress = results[0].address_components;
      $.each(arrAddress, function (i, address_component) {
      if(arrAddress.types.includes('country')) {
      console.log(address_component.long_name);
      }
      });
      } else {
      window.alert('No results found');
      }
      } else {
      window.alert('Geocoder failed due to: ' + status);
      }
      });
      });

      autocomplete.addListener('place_changed', function() {
      var place = autocomplete.getPlace();
      if (!place.geometry) {
      return;
      }
      if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
      } else {
      map.setCenter(place.geometry.location);
      }
      marker.setPosition(place.geometry.location);
      currentLatitude = place.geometry.location.lat();
      currentLongitude = place.geometry.location.lng();
      var arrAddress = place.address_components;
      $.each(arrAddress, function (i, address_component) {
      if(arrAddress.types.includes('country')) {
      console.log(address_component.long_name);
      }
      });
      });
      };


      But I get




      Uncaught TypeError: Cannot read property 'includes' of undefined








      javascript jquery google-maps






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 5 at 2:09









      rob.m

      3,538103680




      3,538103680
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Instead of $.each you can use filter



          results[0].address_components.filter(x => x.types.includes("country"))[0].long_name


          Here is Geoccoding response .






          share|improve this answer



















          • 1




            almost there, according to my code you should have done the following but thanks console.log(arrAddress.filter(x => x.types.includes("country"))[0].long_name);
            – rob.m
            Nov 5 at 2:21










          • Check if address_components is an array
            – Maksym Kozlenko
            Nov 5 at 2:21











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147437%2fhow-to-get-country-name-from-autocomplete-and-dragend%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          Instead of $.each you can use filter



          results[0].address_components.filter(x => x.types.includes("country"))[0].long_name


          Here is Geoccoding response .






          share|improve this answer



















          • 1




            almost there, according to my code you should have done the following but thanks console.log(arrAddress.filter(x => x.types.includes("country"))[0].long_name);
            – rob.m
            Nov 5 at 2:21










          • Check if address_components is an array
            – Maksym Kozlenko
            Nov 5 at 2:21















          up vote
          1
          down vote



          accepted










          Instead of $.each you can use filter



          results[0].address_components.filter(x => x.types.includes("country"))[0].long_name


          Here is Geoccoding response .






          share|improve this answer



















          • 1




            almost there, according to my code you should have done the following but thanks console.log(arrAddress.filter(x => x.types.includes("country"))[0].long_name);
            – rob.m
            Nov 5 at 2:21










          • Check if address_components is an array
            – Maksym Kozlenko
            Nov 5 at 2:21













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          Instead of $.each you can use filter



          results[0].address_components.filter(x => x.types.includes("country"))[0].long_name


          Here is Geoccoding response .






          share|improve this answer














          Instead of $.each you can use filter



          results[0].address_components.filter(x => x.types.includes("country"))[0].long_name


          Here is Geoccoding response .







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 5 at 2:20

























          answered Nov 5 at 2:17









          Maksym Kozlenko

          8,55415853




          8,55415853








          • 1




            almost there, according to my code you should have done the following but thanks console.log(arrAddress.filter(x => x.types.includes("country"))[0].long_name);
            – rob.m
            Nov 5 at 2:21










          • Check if address_components is an array
            – Maksym Kozlenko
            Nov 5 at 2:21














          • 1




            almost there, according to my code you should have done the following but thanks console.log(arrAddress.filter(x => x.types.includes("country"))[0].long_name);
            – rob.m
            Nov 5 at 2:21










          • Check if address_components is an array
            – Maksym Kozlenko
            Nov 5 at 2:21








          1




          1




          almost there, according to my code you should have done the following but thanks console.log(arrAddress.filter(x => x.types.includes("country"))[0].long_name);
          – rob.m
          Nov 5 at 2:21




          almost there, according to my code you should have done the following but thanks console.log(arrAddress.filter(x => x.types.includes("country"))[0].long_name);
          – rob.m
          Nov 5 at 2:21












          Check if address_components is an array
          – Maksym Kozlenko
          Nov 5 at 2:21




          Check if address_components is an array
          – Maksym Kozlenko
          Nov 5 at 2:21


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147437%2fhow-to-get-country-name-from-autocomplete-and-dragend%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()