Javascript — Getting value of variable in another controller scope method
Working with the google maps api (geolocation).
Here is the code in question.
var lat;
var lng;
var moveLat;
var moveLng;
var moveMarker;
var marker;
$scope.initMap = () =>
// TODO:Privilege Check!
var checkPrivilege = false;
console.log("Clicked Generate Map Pin");
var x = document.getElementById("demo");
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(function (position)
lat = parseFloat(position.coords.latitude);
lng = parseFloat(position.coords.longitude);
console.log("Lat " + lat + " Lng " + lng);
// The location of current user
var CMU_SV = lat: parseFloat(lat), lng: parseFloat(lng);
// The map, centered at current user
var map = new google.maps.Map(
document.getElementById('map'), zoom: 4, center: CMU_SV);
map.setOptions( minZoom: 15, maxZoom: 20 );
// True for admin user
if (checkPrivilege)
// The marker, positioned at current user's position
moveMarker = new google.maps.Marker(position: CMU_SV, flat: false, map: map, draggable: true);
// False for regular user
else
//var moveMarker = new google.maps.Marker(position: CMU_SV, flat: false, map: map, draggable: true);
marker = new google.maps.Marker(position: CMU_SV, map: map);
);
else
x.innerHTML = "Geolocation is not supported by this browser.";
;
$scope.sendLocationInfo = () =>
var checkPrivilege = false;
if (checkPrivilege)
moveLat = parseFloat(moveMarker.getPosition().lat);
moveLng = parseFloat(moveMarker.getPosition().lng);
console.log(moveLat, moveLng);
$scope.LocationInfo = moveLat + "," + moveLng;
$scope.hasLocationInfo = true;
else
lat = parseFloat(marker.getPosition().lat);
lng = parseFloat(marker.getPosition().lat);
console.log(lat, lng);
$scope.LocationInfo = lat + "," + lng;
$scope.hasLocationInfo = true;
var currentUser = $cookies.get('userName');
$scope.message = currentUser + " has shared their location!";
client.sendLocationInfo(currentUser, $scope.LocationInfo, $scope.hasLocationInfo)
.then(() =>
$scope.LocationInfo = "";
);
client.sendPublicWallMessage($scope.message)
.then(() =>
$scope.message = '';
)
;
As you can see, I initialize 6 var's at the beginning -- outside of initMap and sendLocationInfo.
There are also two map markers (moveMarker and marker). The moveMarker can be moved by the admin user if so desired.
In the second function, I am trying to call each map marker to get the latitude and longitude information -- assign them to their respective variables and then save these coordinates in my DB.
However, for console.log(moveLat, moveLng) and console.log(lat, lng) -- I am getting two NaN values instead of the desired coordinates.
Any way to fix this?
javascript
add a comment |
Working with the google maps api (geolocation).
Here is the code in question.
var lat;
var lng;
var moveLat;
var moveLng;
var moveMarker;
var marker;
$scope.initMap = () =>
// TODO:Privilege Check!
var checkPrivilege = false;
console.log("Clicked Generate Map Pin");
var x = document.getElementById("demo");
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(function (position)
lat = parseFloat(position.coords.latitude);
lng = parseFloat(position.coords.longitude);
console.log("Lat " + lat + " Lng " + lng);
// The location of current user
var CMU_SV = lat: parseFloat(lat), lng: parseFloat(lng);
// The map, centered at current user
var map = new google.maps.Map(
document.getElementById('map'), zoom: 4, center: CMU_SV);
map.setOptions( minZoom: 15, maxZoom: 20 );
// True for admin user
if (checkPrivilege)
// The marker, positioned at current user's position
moveMarker = new google.maps.Marker(position: CMU_SV, flat: false, map: map, draggable: true);
// False for regular user
else
//var moveMarker = new google.maps.Marker(position: CMU_SV, flat: false, map: map, draggable: true);
marker = new google.maps.Marker(position: CMU_SV, map: map);
);
else
x.innerHTML = "Geolocation is not supported by this browser.";
;
$scope.sendLocationInfo = () =>
var checkPrivilege = false;
if (checkPrivilege)
moveLat = parseFloat(moveMarker.getPosition().lat);
moveLng = parseFloat(moveMarker.getPosition().lng);
console.log(moveLat, moveLng);
$scope.LocationInfo = moveLat + "," + moveLng;
$scope.hasLocationInfo = true;
else
lat = parseFloat(marker.getPosition().lat);
lng = parseFloat(marker.getPosition().lat);
console.log(lat, lng);
$scope.LocationInfo = lat + "," + lng;
$scope.hasLocationInfo = true;
var currentUser = $cookies.get('userName');
$scope.message = currentUser + " has shared their location!";
client.sendLocationInfo(currentUser, $scope.LocationInfo, $scope.hasLocationInfo)
.then(() =>
$scope.LocationInfo = "";
);
client.sendPublicWallMessage($scope.message)
.then(() =>
$scope.message = '';
)
;
As you can see, I initialize 6 var's at the beginning -- outside of initMap and sendLocationInfo.
There are also two map markers (moveMarker and marker). The moveMarker can be moved by the admin user if so desired.
In the second function, I am trying to call each map marker to get the latitude and longitude information -- assign them to their respective variables and then save these coordinates in my DB.
However, for console.log(moveLat, moveLng) and console.log(lat, lng) -- I am getting two NaN values instead of the desired coordinates.
Any way to fix this?
javascript
2
CheckmoveMarker.getPosition().lat/lngon the console first and look at what it spits out. There could be letters in there whichparseFloat()couldn't properly evaluate, outputting NaN
– Abana Clara
Nov 15 '18 at 1:20
Thanks, that put me on the right track and I found it.
– cjawahar
Nov 15 '18 at 2:27
add a comment |
Working with the google maps api (geolocation).
Here is the code in question.
var lat;
var lng;
var moveLat;
var moveLng;
var moveMarker;
var marker;
$scope.initMap = () =>
// TODO:Privilege Check!
var checkPrivilege = false;
console.log("Clicked Generate Map Pin");
var x = document.getElementById("demo");
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(function (position)
lat = parseFloat(position.coords.latitude);
lng = parseFloat(position.coords.longitude);
console.log("Lat " + lat + " Lng " + lng);
// The location of current user
var CMU_SV = lat: parseFloat(lat), lng: parseFloat(lng);
// The map, centered at current user
var map = new google.maps.Map(
document.getElementById('map'), zoom: 4, center: CMU_SV);
map.setOptions( minZoom: 15, maxZoom: 20 );
// True for admin user
if (checkPrivilege)
// The marker, positioned at current user's position
moveMarker = new google.maps.Marker(position: CMU_SV, flat: false, map: map, draggable: true);
// False for regular user
else
//var moveMarker = new google.maps.Marker(position: CMU_SV, flat: false, map: map, draggable: true);
marker = new google.maps.Marker(position: CMU_SV, map: map);
);
else
x.innerHTML = "Geolocation is not supported by this browser.";
;
$scope.sendLocationInfo = () =>
var checkPrivilege = false;
if (checkPrivilege)
moveLat = parseFloat(moveMarker.getPosition().lat);
moveLng = parseFloat(moveMarker.getPosition().lng);
console.log(moveLat, moveLng);
$scope.LocationInfo = moveLat + "," + moveLng;
$scope.hasLocationInfo = true;
else
lat = parseFloat(marker.getPosition().lat);
lng = parseFloat(marker.getPosition().lat);
console.log(lat, lng);
$scope.LocationInfo = lat + "," + lng;
$scope.hasLocationInfo = true;
var currentUser = $cookies.get('userName');
$scope.message = currentUser + " has shared their location!";
client.sendLocationInfo(currentUser, $scope.LocationInfo, $scope.hasLocationInfo)
.then(() =>
$scope.LocationInfo = "";
);
client.sendPublicWallMessage($scope.message)
.then(() =>
$scope.message = '';
)
;
As you can see, I initialize 6 var's at the beginning -- outside of initMap and sendLocationInfo.
There are also two map markers (moveMarker and marker). The moveMarker can be moved by the admin user if so desired.
In the second function, I am trying to call each map marker to get the latitude and longitude information -- assign them to their respective variables and then save these coordinates in my DB.
However, for console.log(moveLat, moveLng) and console.log(lat, lng) -- I am getting two NaN values instead of the desired coordinates.
Any way to fix this?
javascript
Working with the google maps api (geolocation).
Here is the code in question.
var lat;
var lng;
var moveLat;
var moveLng;
var moveMarker;
var marker;
$scope.initMap = () =>
// TODO:Privilege Check!
var checkPrivilege = false;
console.log("Clicked Generate Map Pin");
var x = document.getElementById("demo");
if (navigator.geolocation)
navigator.geolocation.getCurrentPosition(function (position)
lat = parseFloat(position.coords.latitude);
lng = parseFloat(position.coords.longitude);
console.log("Lat " + lat + " Lng " + lng);
// The location of current user
var CMU_SV = lat: parseFloat(lat), lng: parseFloat(lng);
// The map, centered at current user
var map = new google.maps.Map(
document.getElementById('map'), zoom: 4, center: CMU_SV);
map.setOptions( minZoom: 15, maxZoom: 20 );
// True for admin user
if (checkPrivilege)
// The marker, positioned at current user's position
moveMarker = new google.maps.Marker(position: CMU_SV, flat: false, map: map, draggable: true);
// False for regular user
else
//var moveMarker = new google.maps.Marker(position: CMU_SV, flat: false, map: map, draggable: true);
marker = new google.maps.Marker(position: CMU_SV, map: map);
);
else
x.innerHTML = "Geolocation is not supported by this browser.";
;
$scope.sendLocationInfo = () =>
var checkPrivilege = false;
if (checkPrivilege)
moveLat = parseFloat(moveMarker.getPosition().lat);
moveLng = parseFloat(moveMarker.getPosition().lng);
console.log(moveLat, moveLng);
$scope.LocationInfo = moveLat + "," + moveLng;
$scope.hasLocationInfo = true;
else
lat = parseFloat(marker.getPosition().lat);
lng = parseFloat(marker.getPosition().lat);
console.log(lat, lng);
$scope.LocationInfo = lat + "," + lng;
$scope.hasLocationInfo = true;
var currentUser = $cookies.get('userName');
$scope.message = currentUser + " has shared their location!";
client.sendLocationInfo(currentUser, $scope.LocationInfo, $scope.hasLocationInfo)
.then(() =>
$scope.LocationInfo = "";
);
client.sendPublicWallMessage($scope.message)
.then(() =>
$scope.message = '';
)
;
As you can see, I initialize 6 var's at the beginning -- outside of initMap and sendLocationInfo.
There are also two map markers (moveMarker and marker). The moveMarker can be moved by the admin user if so desired.
In the second function, I am trying to call each map marker to get the latitude and longitude information -- assign them to their respective variables and then save these coordinates in my DB.
However, for console.log(moveLat, moveLng) and console.log(lat, lng) -- I am getting two NaN values instead of the desired coordinates.
Any way to fix this?
javascript
javascript
asked Nov 15 '18 at 1:15
cjawaharcjawahar
145
145
2
CheckmoveMarker.getPosition().lat/lngon the console first and look at what it spits out. There could be letters in there whichparseFloat()couldn't properly evaluate, outputting NaN
– Abana Clara
Nov 15 '18 at 1:20
Thanks, that put me on the right track and I found it.
– cjawahar
Nov 15 '18 at 2:27
add a comment |
2
CheckmoveMarker.getPosition().lat/lngon the console first and look at what it spits out. There could be letters in there whichparseFloat()couldn't properly evaluate, outputting NaN
– Abana Clara
Nov 15 '18 at 1:20
Thanks, that put me on the right track and I found it.
– cjawahar
Nov 15 '18 at 2:27
2
2
Check
moveMarker.getPosition().lat/lng on the console first and look at what it spits out. There could be letters in there which parseFloat() couldn't properly evaluate, outputting NaN– Abana Clara
Nov 15 '18 at 1:20
Check
moveMarker.getPosition().lat/lng on the console first and look at what it spits out. There could be letters in there which parseFloat() couldn't properly evaluate, outputting NaN– Abana Clara
Nov 15 '18 at 1:20
Thanks, that put me on the right track and I found it.
– cjawahar
Nov 15 '18 at 2:27
Thanks, that put me on the right track and I found it.
– cjawahar
Nov 15 '18 at 2:27
add a comment |
1 Answer
1
active
oldest
votes
Your code is correct. But the value being returned to parseFloat in moveMarker.getPosition().lat may be a string or other value that can't be parsed as a float value.
1
Thanks for the tip. I found the error. It should be getPosition().lat() Found it in the reference. developers.google.com/maps/documentation/javascript/reference/…
– cjawahar
Nov 15 '18 at 2:26
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f53311090%2fjavascript-getting-value-of-variable-in-another-controller-scope-method%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
Your code is correct. But the value being returned to parseFloat in moveMarker.getPosition().lat may be a string or other value that can't be parsed as a float value.
1
Thanks for the tip. I found the error. It should be getPosition().lat() Found it in the reference. developers.google.com/maps/documentation/javascript/reference/…
– cjawahar
Nov 15 '18 at 2:26
add a comment |
Your code is correct. But the value being returned to parseFloat in moveMarker.getPosition().lat may be a string or other value that can't be parsed as a float value.
1
Thanks for the tip. I found the error. It should be getPosition().lat() Found it in the reference. developers.google.com/maps/documentation/javascript/reference/…
– cjawahar
Nov 15 '18 at 2:26
add a comment |
Your code is correct. But the value being returned to parseFloat in moveMarker.getPosition().lat may be a string or other value that can't be parsed as a float value.
Your code is correct. But the value being returned to parseFloat in moveMarker.getPosition().lat may be a string or other value that can't be parsed as a float value.
answered Nov 15 '18 at 1:23
GeuisGeuis
20k45122195
20k45122195
1
Thanks for the tip. I found the error. It should be getPosition().lat() Found it in the reference. developers.google.com/maps/documentation/javascript/reference/…
– cjawahar
Nov 15 '18 at 2:26
add a comment |
1
Thanks for the tip. I found the error. It should be getPosition().lat() Found it in the reference. developers.google.com/maps/documentation/javascript/reference/…
– cjawahar
Nov 15 '18 at 2:26
1
1
Thanks for the tip. I found the error. It should be getPosition().lat() Found it in the reference. developers.google.com/maps/documentation/javascript/reference/…
– cjawahar
Nov 15 '18 at 2:26
Thanks for the tip. I found the error. It should be getPosition().lat() Found it in the reference. developers.google.com/maps/documentation/javascript/reference/…
– cjawahar
Nov 15 '18 at 2:26
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.
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%2f53311090%2fjavascript-getting-value-of-variable-in-another-controller-scope-method%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
2
Check
moveMarker.getPosition().lat/lngon the console first and look at what it spits out. There could be letters in there whichparseFloat()couldn't properly evaluate, outputting NaN– Abana Clara
Nov 15 '18 at 1:20
Thanks, that put me on the right track and I found it.
– cjawahar
Nov 15 '18 at 2:27