samedi 18 avril 2015

Splitting string for LatLng object

I am stuck on a very annoying problem. Here is what I am trying to achieve. I read the latitude and longitude in two text boxes and then split each of the pair on a comma as that is what they are separated by. Then I need to parse them and create a LatLng object to create a Google marker. My problem for some reason is splitting the string. I know that all I need to do is use String.split() method to achieve it. Here is what I do:



Lets say the value in text box is 26.2338, 81.2336

//Reading the value in text boxes on HTML form
var sourceLocation =document.getElementById("source").value;

//Remove any spaces in between coordinates
var newString =sourceLocation.replace(/\s/g, '');

//Split the string on ,
newString.split(",");

//Creating latitude longitude objects of the source and destination
var newLoc =new google.maps.LatLng(parseFloat(newString[0]),parseFloat(newString[1]));


Now I am unable to understand as to why newString[0] just gives me 2 while it should give 26.2338. Similarly, newString[1] gives 6 instead of 81.2336. What am I doing wrong?? Any help will be greatly appreciated.


Aucun commentaire:

Enregistrer un commentaire