I want to convert a String with a pattern like "(53.324523, 43.252352)" into a latlng. I already found the right function:
var input = latlngArray[i];
var latlngStr = input.split(",",2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
latlngArray[i] = new google.maps.LatLng(lat, lng);
The problem is that I can't get a proper lat. When I use alert to display it it only says NaN, while the lng variable displays the right value.
Got it. Thx for the help.
var input = latlngArray[i].substring(1, latlngArray[i].length-1);
var latlngStr = input.split(",",2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
latlngArray[i] = new google.maps.LatLng(lat, lng);
google.maps.LatLng class. A LatLng is a point in geographical coordinates: latitude and longitude. Latitude ranges between -90 and 90 degrees, inclusive.
We round the lat/lng values to 6 decimal places by default. google.maps. LatLngLiteral interface Object literals are accepted in place of LatLng objects, as a convenience, in many places. These are converted to LatLng objects when the Maps API encounters them.
This example demonstrates using a LatLng object literal instead of a google.maps.LatLng object, to center the map and add a marker. LatLng object literals are a convenient way to add a LatLng coordinate and, in most cases, can be used in place of a google.maps.LatLng object.
You can split the string by comma and then parse the string to long String [] latlong = "-34.8799074,174.7565664".split (","); double latitude = Double.parseDouble (latlong [0]); double longitude = Double.parseDouble (latlong [1]); To constructs a LatLng with the given latitude and longitude coordinates
Quick fix after var input. Looks like the '(' is messing up the lat conversion:
input = input.replace('(','');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With