设置坐标(如果存在) [英] set coordinates if exist google maps

查看:64
本文介绍了设置坐标(如果存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从URL中获取坐标,然后将其发送到Google地图,以便在该坐标中设置地图中心。
我设置了条件。如果坐标没有值,则可以使用,但如果坐标没有值,则地图不会设置中心。
这是我的代码。

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

                }
            };

我在做什么不好?

推荐答案

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

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(',');

然后

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

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

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

            }
        };

工作示例

这篇关于设置坐标(如果存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆