Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

javascript - How to fix Uncaught InvalidValueError: setPosition: not a LatLng or LatLngLiteral: in property lat: not a number?

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:

Fiddle which is not working

Fiddle which works

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...