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
javascript jquery google-maps
add a comment |
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
javascript jquery google-maps
add a comment |
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
javascript jquery google-maps
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
javascript jquery google-maps
asked Nov 5 at 2:09
rob.m
3,538103680
3,538103680
add a comment |
add a comment |
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 .
1
almost there, according to my code you should have done the following but thanksconsole.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
add a comment |
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 .
1
almost there, according to my code you should have done the following but thanksconsole.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
add a comment |
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 .
1
almost there, according to my code you should have done the following but thanksconsole.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
add a comment |
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 .
Instead of $.each
you can use filter
results[0].address_components.filter(x => x.types.includes("country"))[0].long_name
Here is Geoccoding response .
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 thanksconsole.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
add a comment |
1
almost there, according to my code you should have done the following but thanksconsole.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
add a comment |
draft saved
draft discarded
draft saved
draft discarded
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password