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.
String.split() returns an array, it doesn't modify the string to somehow make it into an array. You want
var parts = newString.split(",");
var newLoc = new google.maps.LatLng(parseFloat(parts[0]),parseFloat(parts[1]));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.