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!
javascript google-places-api google-places uncaught-typeerror
add a comment |
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!
javascript google-places-api google-places uncaught-typeerror
So, you already have the two property valueslatitude
andlongitude
, 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
add a comment |
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!
javascript google-places-api google-places uncaught-typeerror
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
javascript google-places-api google-places uncaught-typeerror
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 valueslatitude
andlongitude
, 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
add a comment |
So, you already have the two property valueslatitude
andlongitude
, 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
add a comment |
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
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 getlatitude
andlongitude
values when user doesn't submit the request.
– Tân Nguyễn
Nov 11 at 6:44
add a comment |
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
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 getlatitude
andlongitude
values when user doesn't submit the request.
– Tân Nguyễn
Nov 11 at 6:44
add a comment |
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
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 getlatitude
andlongitude
values when user doesn't submit the request.
– Tân Nguyễn
Nov 11 at 6:44
add a comment |
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
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
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 getlatitude
andlongitude
values when user doesn't submit the request.
– Tân Nguyễn
Nov 11 at 6:44
add a comment |
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 getlatitude
andlongitude
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
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
So, you already have the two property values
latitude
andlongitude
, 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