Google Maps API v3有一个非常奇怪的bug

A very strange bug with Google Maps API v3?

本文关键字:bug 非常 有一个 Maps API v3 Google      更新时间:2023-09-26

在google maps api v3中,以下行可以工作:

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

但是,下面的不是:

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

这有多奇怪,或者更好的是,如何解决这个问题?


完整代码:

$("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.body.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
   });
}

根据文档,你根本不能传递字符串。

你必须显式地将这两部分分开,并将它们作为数字传递:

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