Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to latlng google maps

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);
like image 853
OhSnap Avatar asked Jul 21 '11 14:07

OhSnap


People also ask

What does latlng mean in Google Maps?

google.maps.LatLng class. A LatLng is a point in geographical coordinates: latitude and longitude. Latitude ranges between -90 and 90 degrees, inclusive.

What is the default range of Lat in Google Maps?

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.

How do I use a latlng object literals?

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.

How to parse a string with latitude and longitude in Python?

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


1 Answers

Quick fix after var input. Looks like the '(' is messing up the lat conversion:

input = input.replace('(','');
like image 98
TreyA Avatar answered Oct 29 '22 17:10

TreyA