TietjeDK
TietjeDK

Reputation: 1207

Google maps LatLng not a number

I'm trying to create polygon markers on a Google map by converting a string containing lat/lng positions into an array. I'm running into the following error:

mc {message: "at index 0: not a LatLng or LatLngLiteral: in property lat: not a number", name: "InvalidValueError"

When i inspect the array in the browser console it looks fine (index[0]):

lat:"12.3394541791301"
lng:"55.59020913559151"

The string has the following structure:

'12.3394541791301,55.59020913559151, 12.3444451791347,55.5930451355941, 12.3808361791686,55.6099911356099,

So first i replace every second comma with ;

When i use split(";") to create my array, which i when loop and push into a new array with the right keys:

s = s.replace(/([^,]+,[^,]+),/g,'$1;');
s = s.split(";");

for (var i = 0; i < s.length; i++) {
        var lat = s[i].split(',')[0];
        var lng = s[i].split(',')[1];
        // Push to array which is used by Google Maps
        denmarkLatLong.push({lat: lat, lng: lng})
}

Any suggestions to what could cause the problem?

Upvotes: 1

Views: 5416

Answers (2)

Samuel Toh
Samuel Toh

Reputation: 19328

I'm not sure what a polygon marker is but I suspect you meant marker.

If you look at the latlng documentation the value for key lat and lng are supposed to be Number not String. So to fix your problem you can use the api Number(...) to cast the string coordinates into numbers.

Example:

s = s.replace(/([^,]+,[^,]+),/g,'$1;');
s = s.split(";");

for (var i = 0; i < s.length; i++) {
        var lat = s[i].split(',')[0];
        var lng = s[i].split(',')[1];
        // Push to array which is used by Google Maps
        denmarkLatLong.push({lat: Number(lat), lng: Number(lng) })
}

References:

LatLng: https://developers.google.com/maps/documentation/javascript/examples/map-latlng-literal

Upvotes: 4

Constantine
Constantine

Reputation: 554

Try to do this way:

These pluses will convert your string values of lat, lng to numeric. Or try parseFloat()

 denmarkLatLong.push({lat: +lat, lng: +lng})

OR

denmarkLatLong.push({lat: parseFloat(lat), lng: parseFloat(lng)})    

Problem that you have strings instead of numbers. Just convert, say if it helped.

lat:"12.3394541791301" // strings, convert to numbers
lng:"55.59020913559151"

Upvotes: 6

Related Questions