set coordinates if exist google maps

穿精又带淫゛_ 提交于 2019-12-12 03:43:57

问题


I want to take cordinates from the URL and send them to a google map, in order to set the map center in that coordinates. I set a condition. If coordinates has not value it works, but if it has not the map doesn´t set center. This is my code.

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

                }
            };

What i´m doing bad?


回答1:


A google.maps.LatLng takes two floating point numbers as an argument, not a string containing two numbers separated by a comma.

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

then

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

There is also something weird with the way you are creating multiple "init" function inside the "if", that doesn't work for me (at least in IE). This does:

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

            }
        };

Working example



来源:https://stackoverflow.com/questions/14894076/set-coordinates-if-exist-google-maps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!