How to fix Uncaught InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number?
JavascriptGoogle Maps-Api-3Javascript Problem Overview
I'm trying to port my googlemaps v2 functions to v3.
But somehow i stuck in a strange error and i could not find, what i'm doing wrong. >Error : Uncaught InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number %7Bmain,adsense,geometry,zombie%7D.js:25
Here my map initialisation:
var map = new google.maps.Map(document.getElementById("map"),{
zoom:4
size:new google.maps.Size(580,440),
mapTypeControl: true,
mapTypeControlOptions: {
style:google.maps.MapTypeControlStyle.VERTICAL_BAR,
position: google.maps.ControlPosition.BOTTOM_CENTER,
},
panControl: true,
panControlOptions: {
position: google.maps.ControlPosition.TOP_LEFT,
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.LEFT_CENTER,
},
scaleConrol: true,
scaleControlOptions:{
position: google.maps.ControlPosition.TOP_LEFT ,
},
});
map.setCenter(new google.maps.LatLng(49.477643, 9.316406));
and here the part with my error:
function drawTraders(map, options) {
var traders_uri = options.traders_uri || '';
if ('' == traders_uri) {
return false;
}
// get the informations
$.getJSON(traders_uri, function(data) {
// look through the information
$.each(data.traders, function(i, trader) {
var icon = options.icon_image_uri;
var markerIcon = new google.maps.MarkerImage(icon,
// This marker is 20 pixels wide by 32 pixels tall.
new google.maps.Size(25, 25),
// The origin for this image is 0,0.
new google.maps.Point(0,0),
// The anchor for this image is the base of the flagpole at 0,32.
new google.maps.Point(0, 32)
);
var html = 'test';
/*Information from chromium debugger
trader: Object
geo: Object
lat: "49.014821"
lon: "10.985072"
*/
var myLatlng = new google.maps.LatLng(trader.geo.lat,trader.geo.lon);
/*here is the error in new google.maps.Marker()*/
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
icon : markerIcon,
});
var infowindow = new google.maps.InfoWindow({
content: html
});
}
Edit: the call of drawTraders
drawTraders(map, {
traders_uri: "getTraders.php",
icon_image_uri: "icon-cms.gif",
});
I was using this example Info Window
Edit:
Javascript Solutions
Solution 1 - Javascript
> Uncaught InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number
Means you are not passing numbers into the google.maps.LatLng constructor. Per your comment:
/*Information from chromium debugger
trader: Object
geo: Object
lat: "49.014821"
lon: "10.985072"
*/
trader.geo.lat and trader.geo.lon are strings, not numbers. Use parseFloat to convert them to numbers:
var myLatlng = new google.maps.LatLng(parseFloat(trader.geo.lat),parseFloat(trader.geo.lon));
Solution 2 - Javascript
I had the same problem with exactly the same error message. In the end the error was, that I still called the maps v2 javascript. I had to replace:
<script src="http://maps.google.com/maps?file=api&v=2&key=####################" type="text/javascript"></script>
with
<script src="http://maps.googleapis.com/maps/api/js?key=####################&sensor=false" type="text/javascript"></script>
after this, it worked fine. took me a while ;-)
Solution 3 - Javascript
you can use this way
map = new google.maps.Map(document.getElementById('map'), {
zoom: 16,
center: { lat: parseFloat(lat), lng: parseFloat(lng) },
mapTypeId: 'terrain',
disableDefaultUI: true
});
EX : center: { lat: parseFloat(lat), lng: parseFloat(lng) },
Solution 4 - Javascript
I was having the same problem, the fact is that the input of lat and long should be String. Only then did I manage.
for example:
Controller.
ViewBag.Lat = object.Lat.ToString().Replace(",", ".");
ViewBag.Lng = object.Lng.ToString().Replace(",", ".");
View - function javascript
<script>
function initMap() {
var myLatLng = { lat: @ViewBag.Lat, lng: @ViewBag.Lng};
// Create a map object and specify the DOM element for display.
var map = new window.google.maps.Map(document.getElementById('map'),
{
center: myLatLng,
scrollwheel: false,
zoom: 16
});
// Create a marker and set its position.
var marker = new window.google.maps.Marker({
map: map,
position: myLatLng
//title: "Blue"
});
}
</script>
I convert the double value to string and do a Replace in the ',' to '.' And so everything works normally.
Solution 5 - Javascript
I had the same problem when setting the center of the map with map.setCenter()
. Using Number()
solved for me. Had to use parseFloat
to truncate the data.
code snippet:
var centerLat = parseFloat(data.lat).toFixed(0);
var centerLng = parseFloat(data.long).toFixed(0);
map.setCenter({
lat: Number(centerLat),
lng: Number(centerLng)
});
Solution 6 - Javascript
Using angular-google-maps
$scope.bounds = new google.maps.LatLngBounds();
for (var i = $scope.markers.length - 1; i >= 0; i--) {
$scope.bounds.extend(new google.maps.LatLng($scope.markers[i].coords.latitude, $scope.markers[i].coords.longitude));
};
$scope.control.getGMap().fitBounds($scope.bounds);
$scope.control.getGMap().setCenter($scope.bounds.getCenter());
Solution 7 - Javascript
What about casting to Number in Javascript using the Unary (+) Operator
lat: 12.23
lat: +12.23
Depends on the use case. Here is a fullchart what works and what not: https://stackoverflow.com/questions/17106681/parseint-vs-unary-plus-when-to-use-which/17106702#17106702
Solution 8 - Javascript
You're probably passing null
value if you're loading the coordinates dynamically, set a check before you call the map loader ie: if(mapCords){loadMap}
Solution 9 - Javascript
I had the same problem, and resolved it. In my case it was error due to the non-proper format. Please check the first and last coordinates array in geometry coordinates they must be same then and only then it will work. Hope it may help you!