-1

I am trying to pass a latitude AND a longitude from one page to another after it has been calculated by the google API.

For a compass website. Want to pass the lat and long both to another page to be used on that page. I am trying to pass them via the javascript.

The Java passing the variables.

var lat = marker.getPosition().lat();
var long = marker.getPosition().lng();
window.location.href = 'compass.html' + '#' + lat +'#' + long;

The Java recieving the variables

var latD = window.location.hash.substring(1);
var longD = window.location.hash.substring(2);

Instead of being split up they and being displayed together on the other page with the hash included. Like this:

-41.2864603?#174.77623600000004, 41.2864603?#174.77623600000004

I would like it to be like this: -41.2864603 ,174.77623600000004

Sofo Gial
  • 697
  • 1
  • 9
  • 20
  • 3
    This is very odd looking Java, I don't think I've seen it before, what version is it? – Snow May 24 '19 at 08:32
  • java does'nt have window object ,you tagged wrongly – Shubham Dixit May 24 '19 at 08:33
  • It is for HTML. Sorry new to this. – Lovely Gecko May 24 '19 at 08:34
  • 1
    Instead of abusing anchor links (`#....`), why don't you take advantage of query parameters? i.e. `compass.html?lat=....&lng=....`. That way, your parameters will be much easier to parse, and you will also not have the issue you describe here. – nbokmans May 24 '19 at 08:34
  • I think I tried that and it didn't want to work. This is my first time attempting this sort of thing, so new process for me. – Lovely Gecko May 24 '19 at 08:37
  • Definitely use query parameters. There's plenty of code online about how to parse query parameters with javascript. @nbokmans advice is correct. – TKoL May 24 '19 at 08:39
  • Thanks for your help!! How would I set them as a variable in the page getting the parameters? – Lovely Gecko May 24 '19 at 08:44
  • If possible, could also go with `localStorage`. No QueryString-Parser required. – Lain May 24 '19 at 09:09

2 Answers2

0

it should be like this :

   var lat = marker.getPosition().lat();

   var lng = marker.getPosition().lng();

   window.location.href = 'compass.html?lat=' + '' + lat +'&longt=' + long;

To access these variables on that page,

   const queryString = window.location.search;
   console.log(queryString); // ?lat=19.234567&longt=73.23456

   const urlParams = new URLSearchParams(queryString);
   const lat = urlParams.get('lat')
   const lng = urlParams.get('longt')
   console.log(lat, lng);
0

Hash

The idea works, you just need to split the hash-string correctly.

//REM: On whatever.html
var lat = '-41.2864603';
var long = '174.77623600000004';
var tHref = 'compass.html' + '#' + lat +';' + long; //window.location.href = ..

//REM: On compass.html
var tHash = tHref.split('#').pop(); //window.location.hash.split('#').pop();
var tLat = tHash.split(';')[0];
var tLong = tHash.split(';')[1];

alert('Lat: ' + tLat + ', Long: ' + tLong);

QueryString

As another approach you could pass it as normal QueryString value like nbokmans recommended. Either separate or as stringified object. The difference is, that you need to implement your own js QueryString-Parser unless you get the values on the server. But you will find plenty of QueryString-Parsers on google.

Here is an example: How can I get query string values in JavaScript?

localStorage

localStorage sounds like the easiest solution for me. You do not need to implement your own QueryString-Parser for it aswell. Just set it on whatever.html and read it on compass.html.

Check the example below: https://developer.mozilla.org/de/docs/Web/API/Window/localStorage

//Set:
window.localStorage.setItem('whatever', JSON.stringify({lat: 1, long:2}));

//Get:
JSON.parse(window.localStorage.getItem('whatever'));
Lain
  • 3,657
  • 1
  • 20
  • 27
  • Thank you! This was very helpful and informative. Your time in writing such a good answer is appreciated! It worked – Lovely Gecko May 25 '19 at 04:04