0

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?

user592376
  • 21
  • 4

1 Answers1

0

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

geocodezip
  • 158,664
  • 13
  • 220
  • 245