13

I am looping some markers and add a lat and long to them but I keep getting this error:

message
:
"not a LatLng or LatLngLiteral: not an Object"
name
:
"InvalidValueError"

And:

InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number

This is my loop:

var bedrijvenlijst = []; // new array
    $.each( bedrijven, function( key, value ) {
      // console.log( key + ": " + value.plaats );
      console.log(value.lng);
      bedrijvenlijst.push({
          title : value.title,
          image : 'bbvdw.jpg',
          address : value.straat + ' ' + value.plaats,
          position : {
              lat : value.lat,
              lng : value.lng
          },
          markerIcon : 'marker-green.png'
      }); // new item added in array
    });

When I console log the lng or lat is just shows as a normal number like:

4.23626

Why do I get this error? If I just type the coordinates myself in the loop it works fine, so what is different when using value.lat and value.lng ? In the console it looks exactly the same as if I would type it myself.

twan
  • 2,450
  • 10
  • 32
  • 92

2 Answers2

51

The error message points to the property 'lat'; you're passing something that is not a number, it might be the string "4.36xxxx", so check for that.

You could try:

 position : {
          lat : parseFloat( value.lat ),
          lng : parseFloat( value.lng )
      },
dev8080
  • 3,950
  • 1
  • 12
  • 18
2

I think you need a Google Maps LatLng. You cant just take normal numbers for the positions of a marker.

Like position = new google.maps.LatLng(lat,lng);

  • But when I type the latitude and longitude instead of value.lat or value.lng it works. So it should work with just the numbers. – twan Oct 25 '17 at 14:41
  • This is no longer correct. See also google.maps.LatLngLiteral interface https://developers.google.com/maps/documentation/javascript/reference/coordinates#LatLngLiteral – Shanimal Sep 20 '22 at 19:05