Uncaught TypeError: Cannot read property 'geometry' of undefined









up vote
1
down vote

favorite












I have gotten google auto-complete to work, but just in case the user doesn't select the autocomplete, I am trying to submit the address to Google and retrieve the latitude and longitude myself.
The issue I am having is when I submit without using autocomplete I am getting Uncaught




TypeError:



Cannot read property 'geometry' of undefined and Uncaught
TypeError: Cannot read property 'location' of undefined (none of my
console.logs are triggered).




If I submit with autocomplete I still get Uncaught




TypeError: Cannot read property 'geometry' of undefined




even though the autocomplete did work to get the address with the lat and long.



My Html



<form method="GET" id="my-form" action="/search" onsubmit="check()">
<input class="form-control" id="getaddy" type="text" placeholder="Search..." name="term" onFocus="geo()">
<input id="latitude" type="hidden" name="latitude">
<input id="longitude" type="hidden" name="longitude">
</form>


I call geo() autocompleting, but in case they don't use the autocomplete, I try to catch it on submit with my check() function. Here is my JS



function check() 
let latitude = document.getElementById("latitude").value;
if (latitude)
console.log(latitude);
else

let term = $("#getaddy").val();

$.ajax(
url: "https://maps.googleapis.com/maps/api/geocode/json?address="+encodeURIComponent(term) +"&key=GOOGLEKEY",
type: 'get',
success: function(data)
if (data.status === 'OK')
// Get the lat/lng from the response
let lat = data.results[0].geometry.location.lat;
let lng = data.results[0].geometry.location.lng;

console.log("pass");


,
error: function(msg)
console.log("fail");

);
return true;




function geo()

autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('getaddy')),
types: ['geocode']
);
autocomplete.addListener('place_changed', addlatlong);

function addlatlong()

var place = autocomplete.getPlace();

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

document.getElementById("latitude").value = latitude;
document.getElementById("longitude").value = longitude;



Thank you so much for any help!










share|improve this question























  • So, you already have the two property values latitude and longitude, and you want to use the two values to get address but getting error message like that?
    – Tân Nguyễn
    Nov 11 at 6:13











  • OMG, the update edit...
    – Tân Nguyễn
    Nov 11 at 6:14










  • If they use the autocomplete in my input <input class="form-control" id="getaddy" type="text" placeholder="Search..." name="term" onFocus="geo()"> then yes I get the latitude and longitude. But if they just enter an address into the field without selecting from the autocomplete list, then I don't get the latitude and longitude. That is why I have the check() function that looks to see if I got the latitude. If I didn't then I am trying to submit the address to google to get the latitude and longitude back.
    – Chris Grim
    Nov 11 at 6:15














up vote
1
down vote

favorite












I have gotten google auto-complete to work, but just in case the user doesn't select the autocomplete, I am trying to submit the address to Google and retrieve the latitude and longitude myself.
The issue I am having is when I submit without using autocomplete I am getting Uncaught




TypeError:



Cannot read property 'geometry' of undefined and Uncaught
TypeError: Cannot read property 'location' of undefined (none of my
console.logs are triggered).




If I submit with autocomplete I still get Uncaught




TypeError: Cannot read property 'geometry' of undefined




even though the autocomplete did work to get the address with the lat and long.



My Html



<form method="GET" id="my-form" action="/search" onsubmit="check()">
<input class="form-control" id="getaddy" type="text" placeholder="Search..." name="term" onFocus="geo()">
<input id="latitude" type="hidden" name="latitude">
<input id="longitude" type="hidden" name="longitude">
</form>


I call geo() autocompleting, but in case they don't use the autocomplete, I try to catch it on submit with my check() function. Here is my JS



function check() 
let latitude = document.getElementById("latitude").value;
if (latitude)
console.log(latitude);
else

let term = $("#getaddy").val();

$.ajax(
url: "https://maps.googleapis.com/maps/api/geocode/json?address="+encodeURIComponent(term) +"&key=GOOGLEKEY",
type: 'get',
success: function(data)
if (data.status === 'OK')
// Get the lat/lng from the response
let lat = data.results[0].geometry.location.lat;
let lng = data.results[0].geometry.location.lng;

console.log("pass");


,
error: function(msg)
console.log("fail");

);
return true;




function geo()

autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('getaddy')),
types: ['geocode']
);
autocomplete.addListener('place_changed', addlatlong);

function addlatlong()

var place = autocomplete.getPlace();

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

document.getElementById("latitude").value = latitude;
document.getElementById("longitude").value = longitude;



Thank you so much for any help!










share|improve this question























  • So, you already have the two property values latitude and longitude, and you want to use the two values to get address but getting error message like that?
    – Tân Nguyễn
    Nov 11 at 6:13











  • OMG, the update edit...
    – Tân Nguyễn
    Nov 11 at 6:14










  • If they use the autocomplete in my input <input class="form-control" id="getaddy" type="text" placeholder="Search..." name="term" onFocus="geo()"> then yes I get the latitude and longitude. But if they just enter an address into the field without selecting from the autocomplete list, then I don't get the latitude and longitude. That is why I have the check() function that looks to see if I got the latitude. If I didn't then I am trying to submit the address to google to get the latitude and longitude back.
    – Chris Grim
    Nov 11 at 6:15












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have gotten google auto-complete to work, but just in case the user doesn't select the autocomplete, I am trying to submit the address to Google and retrieve the latitude and longitude myself.
The issue I am having is when I submit without using autocomplete I am getting Uncaught




TypeError:



Cannot read property 'geometry' of undefined and Uncaught
TypeError: Cannot read property 'location' of undefined (none of my
console.logs are triggered).




If I submit with autocomplete I still get Uncaught




TypeError: Cannot read property 'geometry' of undefined




even though the autocomplete did work to get the address with the lat and long.



My Html



<form method="GET" id="my-form" action="/search" onsubmit="check()">
<input class="form-control" id="getaddy" type="text" placeholder="Search..." name="term" onFocus="geo()">
<input id="latitude" type="hidden" name="latitude">
<input id="longitude" type="hidden" name="longitude">
</form>


I call geo() autocompleting, but in case they don't use the autocomplete, I try to catch it on submit with my check() function. Here is my JS



function check() 
let latitude = document.getElementById("latitude").value;
if (latitude)
console.log(latitude);
else

let term = $("#getaddy").val();

$.ajax(
url: "https://maps.googleapis.com/maps/api/geocode/json?address="+encodeURIComponent(term) +"&key=GOOGLEKEY",
type: 'get',
success: function(data)
if (data.status === 'OK')
// Get the lat/lng from the response
let lat = data.results[0].geometry.location.lat;
let lng = data.results[0].geometry.location.lng;

console.log("pass");


,
error: function(msg)
console.log("fail");

);
return true;




function geo()

autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('getaddy')),
types: ['geocode']
);
autocomplete.addListener('place_changed', addlatlong);

function addlatlong()

var place = autocomplete.getPlace();

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

document.getElementById("latitude").value = latitude;
document.getElementById("longitude").value = longitude;



Thank you so much for any help!










share|improve this question















I have gotten google auto-complete to work, but just in case the user doesn't select the autocomplete, I am trying to submit the address to Google and retrieve the latitude and longitude myself.
The issue I am having is when I submit without using autocomplete I am getting Uncaught




TypeError:



Cannot read property 'geometry' of undefined and Uncaught
TypeError: Cannot read property 'location' of undefined (none of my
console.logs are triggered).




If I submit with autocomplete I still get Uncaught




TypeError: Cannot read property 'geometry' of undefined




even though the autocomplete did work to get the address with the lat and long.



My Html



<form method="GET" id="my-form" action="/search" onsubmit="check()">
<input class="form-control" id="getaddy" type="text" placeholder="Search..." name="term" onFocus="geo()">
<input id="latitude" type="hidden" name="latitude">
<input id="longitude" type="hidden" name="longitude">
</form>


I call geo() autocompleting, but in case they don't use the autocomplete, I try to catch it on submit with my check() function. Here is my JS



function check() 
let latitude = document.getElementById("latitude").value;
if (latitude)
console.log(latitude);
else

let term = $("#getaddy").val();

$.ajax(
url: "https://maps.googleapis.com/maps/api/geocode/json?address="+encodeURIComponent(term) +"&key=GOOGLEKEY",
type: 'get',
success: function(data)
if (data.status === 'OK')
// Get the lat/lng from the response
let lat = data.results[0].geometry.location.lat;
let lng = data.results[0].geometry.location.lng;

console.log("pass");


,
error: function(msg)
console.log("fail");

);
return true;




function geo()

autocomplete = new google.maps.places.Autocomplete(
(document.getElementById('getaddy')),
types: ['geocode']
);
autocomplete.addListener('place_changed', addlatlong);

function addlatlong()

var place = autocomplete.getPlace();

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

document.getElementById("latitude").value = latitude;
document.getElementById("longitude").value = longitude;



Thank you so much for any help!







javascript google-places-api google-places uncaught-typeerror






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 6:13









Gihan Saranga Siriwardhana

532117




532117










asked Nov 11 at 6:05









Chris Grim

145




145











  • So, you already have the two property values latitude and longitude, and you want to use the two values to get address but getting error message like that?
    – Tân Nguyễn
    Nov 11 at 6:13











  • OMG, the update edit...
    – Tân Nguyễn
    Nov 11 at 6:14










  • If they use the autocomplete in my input <input class="form-control" id="getaddy" type="text" placeholder="Search..." name="term" onFocus="geo()"> then yes I get the latitude and longitude. But if they just enter an address into the field without selecting from the autocomplete list, then I don't get the latitude and longitude. That is why I have the check() function that looks to see if I got the latitude. If I didn't then I am trying to submit the address to google to get the latitude and longitude back.
    – Chris Grim
    Nov 11 at 6:15
















  • So, you already have the two property values latitude and longitude, and you want to use the two values to get address but getting error message like that?
    – Tân Nguyễn
    Nov 11 at 6:13











  • OMG, the update edit...
    – Tân Nguyễn
    Nov 11 at 6:14










  • If they use the autocomplete in my input <input class="form-control" id="getaddy" type="text" placeholder="Search..." name="term" onFocus="geo()"> then yes I get the latitude and longitude. But if they just enter an address into the field without selecting from the autocomplete list, then I don't get the latitude and longitude. That is why I have the check() function that looks to see if I got the latitude. If I didn't then I am trying to submit the address to google to get the latitude and longitude back.
    – Chris Grim
    Nov 11 at 6:15















So, you already have the two property values latitude and longitude, and you want to use the two values to get address but getting error message like that?
– Tân Nguyễn
Nov 11 at 6:13





So, you already have the two property values latitude and longitude, and you want to use the two values to get address but getting error message like that?
– Tân Nguyễn
Nov 11 at 6:13













OMG, the update edit...
– Tân Nguyễn
Nov 11 at 6:14




OMG, the update edit...
– Tân Nguyễn
Nov 11 at 6:14












If they use the autocomplete in my input <input class="form-control" id="getaddy" type="text" placeholder="Search..." name="term" onFocus="geo()"> then yes I get the latitude and longitude. But if they just enter an address into the field without selecting from the autocomplete list, then I don't get the latitude and longitude. That is why I have the check() function that looks to see if I got the latitude. If I didn't then I am trying to submit the address to google to get the latitude and longitude back.
– Chris Grim
Nov 11 at 6:15




If they use the autocomplete in my input <input class="form-control" id="getaddy" type="text" placeholder="Search..." name="term" onFocus="geo()"> then yes I get the latitude and longitude. But if they just enter an address into the field without selecting from the autocomplete list, then I don't get the latitude and longitude. That is why I have the check() function that looks to see if I got the latitude. If I didn't then I am trying to submit the address to google to get the latitude and longitude back.
– Chris Grim
Nov 11 at 6:15












1 Answer
1






active

oldest

votes

















up vote
0
down vote













If you already have the two property values latitude and longitude, I suggest you to use this code (provided by Google) to get address (not using jquery ajax):



var geocoder = new google.maps.Geocoder;
var latlng = lat: latitude, lng: longitude ;

geocoder.geocode( 'location': latlng , function (results, status)
if (status === 'OK')
if (results[0])
var address = results[0].formatted_address);
console.log(address);
else
console.log('No results found');

else
console.log('Geocoder failed due to: ' + status);

);


Source: Reverse Geocoding (Address Lookup)



If they don't submit the request, you can try to use jquery to caugh the event when the searchBox lost the focus (focusout), then calling event places_changed to get the latitude and the longitude.



Source: Places Search Box



Source: Trigger places_changed in Google maps from submit button






share|improve this answer






















  • I only get the latitude and longitude if the user selects their address from the autocomplete list. If the user just enters an address by hand into the form, then I don't get the latitude or longitude.
    – Chris Grim
    Nov 11 at 6:34










  • @ChrisGrim I've updated the answer to get latitude and longitude values when user doesn't submit the request.
    – Tân Nguyễn
    Nov 11 at 6:44










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%2f53246280%2funcaught-typeerror-cannot-read-property-geometry-of-undefined%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













If you already have the two property values latitude and longitude, I suggest you to use this code (provided by Google) to get address (not using jquery ajax):



var geocoder = new google.maps.Geocoder;
var latlng = lat: latitude, lng: longitude ;

geocoder.geocode( 'location': latlng , function (results, status)
if (status === 'OK')
if (results[0])
var address = results[0].formatted_address);
console.log(address);
else
console.log('No results found');

else
console.log('Geocoder failed due to: ' + status);

);


Source: Reverse Geocoding (Address Lookup)



If they don't submit the request, you can try to use jquery to caugh the event when the searchBox lost the focus (focusout), then calling event places_changed to get the latitude and the longitude.



Source: Places Search Box



Source: Trigger places_changed in Google maps from submit button






share|improve this answer






















  • I only get the latitude and longitude if the user selects their address from the autocomplete list. If the user just enters an address by hand into the form, then I don't get the latitude or longitude.
    – Chris Grim
    Nov 11 at 6:34










  • @ChrisGrim I've updated the answer to get latitude and longitude values when user doesn't submit the request.
    – Tân Nguyễn
    Nov 11 at 6:44














up vote
0
down vote













If you already have the two property values latitude and longitude, I suggest you to use this code (provided by Google) to get address (not using jquery ajax):



var geocoder = new google.maps.Geocoder;
var latlng = lat: latitude, lng: longitude ;

geocoder.geocode( 'location': latlng , function (results, status)
if (status === 'OK')
if (results[0])
var address = results[0].formatted_address);
console.log(address);
else
console.log('No results found');

else
console.log('Geocoder failed due to: ' + status);

);


Source: Reverse Geocoding (Address Lookup)



If they don't submit the request, you can try to use jquery to caugh the event when the searchBox lost the focus (focusout), then calling event places_changed to get the latitude and the longitude.



Source: Places Search Box



Source: Trigger places_changed in Google maps from submit button






share|improve this answer






















  • I only get the latitude and longitude if the user selects their address from the autocomplete list. If the user just enters an address by hand into the form, then I don't get the latitude or longitude.
    – Chris Grim
    Nov 11 at 6:34










  • @ChrisGrim I've updated the answer to get latitude and longitude values when user doesn't submit the request.
    – Tân Nguyễn
    Nov 11 at 6:44












up vote
0
down vote










up vote
0
down vote









If you already have the two property values latitude and longitude, I suggest you to use this code (provided by Google) to get address (not using jquery ajax):



var geocoder = new google.maps.Geocoder;
var latlng = lat: latitude, lng: longitude ;

geocoder.geocode( 'location': latlng , function (results, status)
if (status === 'OK')
if (results[0])
var address = results[0].formatted_address);
console.log(address);
else
console.log('No results found');

else
console.log('Geocoder failed due to: ' + status);

);


Source: Reverse Geocoding (Address Lookup)



If they don't submit the request, you can try to use jquery to caugh the event when the searchBox lost the focus (focusout), then calling event places_changed to get the latitude and the longitude.



Source: Places Search Box



Source: Trigger places_changed in Google maps from submit button






share|improve this answer














If you already have the two property values latitude and longitude, I suggest you to use this code (provided by Google) to get address (not using jquery ajax):



var geocoder = new google.maps.Geocoder;
var latlng = lat: latitude, lng: longitude ;

geocoder.geocode( 'location': latlng , function (results, status)
if (status === 'OK')
if (results[0])
var address = results[0].formatted_address);
console.log(address);
else
console.log('No results found');

else
console.log('Geocoder failed due to: ' + status);

);


Source: Reverse Geocoding (Address Lookup)



If they don't submit the request, you can try to use jquery to caugh the event when the searchBox lost the focus (focusout), then calling event places_changed to get the latitude and the longitude.



Source: Places Search Box



Source: Trigger places_changed in Google maps from submit button







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 at 6:43

























answered Nov 11 at 6:21









Tân Nguyễn

1




1











  • I only get the latitude and longitude if the user selects their address from the autocomplete list. If the user just enters an address by hand into the form, then I don't get the latitude or longitude.
    – Chris Grim
    Nov 11 at 6:34










  • @ChrisGrim I've updated the answer to get latitude and longitude values when user doesn't submit the request.
    – Tân Nguyễn
    Nov 11 at 6:44
















  • I only get the latitude and longitude if the user selects their address from the autocomplete list. If the user just enters an address by hand into the form, then I don't get the latitude or longitude.
    – Chris Grim
    Nov 11 at 6:34










  • @ChrisGrim I've updated the answer to get latitude and longitude values when user doesn't submit the request.
    – Tân Nguyễn
    Nov 11 at 6:44















I only get the latitude and longitude if the user selects their address from the autocomplete list. If the user just enters an address by hand into the form, then I don't get the latitude or longitude.
– Chris Grim
Nov 11 at 6:34




I only get the latitude and longitude if the user selects their address from the autocomplete list. If the user just enters an address by hand into the form, then I don't get the latitude or longitude.
– Chris Grim
Nov 11 at 6:34












@ChrisGrim I've updated the answer to get latitude and longitude values when user doesn't submit the request.
– Tân Nguyễn
Nov 11 at 6:44




@ChrisGrim I've updated the answer to get latitude and longitude values when user doesn't submit the request.
– Tân Nguyễn
Nov 11 at 6:44

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246280%2funcaught-typeerror-cannot-read-property-geometry-of-undefined%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3