0

我想从 URL 中获取坐标并将它们发送到谷歌地图,以便在该坐标中设置地图中心。我设定了一个条件。如果坐标没有值,它可以工作,但如果没有,则地图不会设置中心。这是我的代码。

function getUrlVars()
            {
                var vars = [], hash;
                var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
                for(var i = 0; i < hashes.length; i++)
                {
                    hash = hashes[i].split('=');
                    vars.push(hash[0]);
                    vars[hash[0]] = hash[1];
                }
                return vars;
            }
            var coordenadas = getUrlVars()["coordenadas"];


            var map;
            var infowindow;
            var geocoder;

            if (coordenadas){

                function init() {
                    alert ("coordenadas "+coordenadas);
                    geocoder = new google.maps.Geocoder();
                    map = new google.maps.Map(document.getElementById('map_canvas'), {
                        zoom: 5,
                        mapTypeId: google.maps.MapTypeId.ROADMAP,
                        center: new google.maps.LatLng(coordenadas)                 
                    });             
                    infoWindow = new google.maps.InfoWindow();
                    google.maps.event.addListener(map, 'click', clickedAddress);        

                }
            }
            else{
                function init() {
                    geocoder = new google.maps.Geocoder();
                    map = new google.maps.Map(document.getElementById('map_canvas'), {
                        zoom: 5,
                        mapTypeId: google.maps.MapTypeId.ROADMAP,
                        center: new google.maps.LatLng(40.346544, -3.848877)                    
                    });             
                    infoWindow = new google.maps.InfoWindow();
                    google.maps.event.addListener(map, 'click', clickedAddress);        

                }
            };

我在做什么坏事?

4

1 回答 1

0

google.maps.LatLng 将两个浮点数作为参数,而不是包含两个用逗号分隔的数字的字符串。

var coords=coordenadas.split(',');

然后

center: new google.maps.LatLng(parseFloat(coords[0]), parseFloat(coords[1]))

在“if”中创建多个“init”函数的方式也有些奇怪,这对我不起作用(至少在 IE 中)。这样做:

        if (!!coordenadas){

             init = function() {
                alert ("coordenadas "+coordenadas);
       var coords=coordenadas.split(',');
                map = new google.maps.Map(document.getElementById('map_canvas'), {
                    zoom: 5,
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
            center: new google.maps.LatLng(parseFloat(coords[0]), parseFloat(coords[1]))
                });             
                infoWindow = new google.maps.InfoWindow();
                //google.maps.event.addListener(map, 'click', clickedAddress);        

            }
        }
        else{
            init = function () {
                geocoder = new google.maps.Geocoder();
                map = new google.maps.Map(document.getElementById('map_canvas'), {
                    zoom: 5,
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    center: new google.maps.LatLng(40.346544, -3.848877)                    
                });             
                infoWindow = new google.maps.InfoWindow();
                // google.maps.event.addListener(map, 'click', clickedAddress);        

            }
        };

工作示例

于 2013-02-15T12:36:45.910 回答