Google maps 如果存在谷歌地图,则设置坐标
我想从URL中获取Cordinate并将其发送到google地图,以便在该坐标中设置地图中心。 我设定了一个条件。如果坐标没有值,它工作,但如果没有,地图不会设置中心。 这是我的密码Google maps 如果存在谷歌地图,则设置坐标,google-maps,url,conditional-statements,geturl,Google Maps,Url,Conditional Statements,Geturl,我想从URL中获取Cordinate并将其发送到google地图,以便在该坐标中设置地图中心。 我设定了一个条件。如果坐标没有值,它工作,但如果没有,地图不会设置中心。 这是我的密码 function getUrlVars() { var vars = [], hash; var hashes = window.location.href.slice(window.location.href.indexOf(
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);
}
};
函数getUrlVars()
{
var vars=[],散列;
var hashes=window.location.href.slice(window.location.href.indexOf('?')+1).split('&');
for(var i=0;i我做的不好吗?A google.maps.LatLng将两个浮点数作为参数,而不是包含两个用逗号分隔的数字的字符串
var coords=coordenadas.split(',');
然后
在“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);
}
};
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);
}
};