开发者

A very strange bug with Google Maps API v3?

In google maps api v3, the following line works:

var myLatlng = new google.maps.LatLng(50.082243,24.302628);

But, the following doesn't:

var gpsPos = '50.082243,24.302628';
var myLatlng = new google.maps.LatLng(gpsPos);

How strange is that, or better yet, how to fix this?


Here's the full code:

$("document").ready(function(){
   var script = document.createElement("script");
   script.type = "text/javascript";
   script.src = "http://maps.google.com/maps/api/js?sensor=false&region=SK&callback=initialize";
   document.b开发者_StackOverflow中文版ody.appendChild(script);
});

function initialize(){
   var gpsPos = '50.082243,24.302628';
   var myLatlng = new google.maps.LatLng(gpsPos);

   var myOptions = {
      zoom: 7,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      panControl: false,
      zoomControl: true,
      mapTypeControl: true,
      scaleControl: false,
      streetViewControl: false,
      scrollwheel: false
   }

   var map = new google.maps.Map(document.getElementById("otvorena-aukcia-mapa"), myOptions);

   var marker = new google.maps.Marker({
      position: myLatlng, 
      map: map
   });
}


According to the docs you cannot pass a string at all.

You'd have to explicitly split out the two parts and pass them as numbers:

var gpsPos = '50.082243,24.302628';
var splitted = gpsPos.split(",");
var myLatlng = new google.maps.LatLng(splitted[0] - 0, splitted[1] - 0); 
// '- 0' will automatically make it a number
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜