Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
页面加载时触发两个javascript函数的问题_Javascript_Google Maps_Google Maps Api 3 - Fatal编程技术网

页面加载时触发两个javascript函数的问题

页面加载时触发两个javascript函数的问题,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,当我的页面加载时,我需要启动两个javascript函数 初始化Google地图的函数: function initialize() { var myLatLng = new google.maps.LatLng(52.015460,18.498087); var myOptions = { zoom: 6, center: myLatLng, mapTypeId: 'hybrid',

当我的页面加载时,我需要启动两个javascript函数

初始化Google地图的函数:

function initialize() {
    var myLatLng = new google.maps.LatLng(52.015460,18.498087);
        var myOptions = {
            zoom: 6,
            center: myLatLng,
            mapTypeId: 'hybrid',
            clickableIcons: false 
    };
    }
在地图初始化和页面加载时自动聚焦输入字段的函数:

function inputfocus() {
    document.getElementById("pac-input").focus();
}
要启动上述功能,我将使用以下代码:

function start() {
    initialize();
    inputfocus();
}
window.onload = start;
我面临的问题是,这些函数单独工作很好,但是按照上面的顺序,只有第一个函数启动(即,只有map加载)

我已经阅读并测试了stackoverflow和其他网站上的一些想法,但没有一个奏效

如果您对如何解决此问题有任何想法,请告诉我

示例如下(需要添加谷歌地图自己的密钥):


测试
#mapindex{
高度:400px;
宽度:100%;
}
函数初始化(){
var mylatng=new google.maps.LatLng(52.015460,18.498087);//地图在这里居中
变量myOptions={
缩放:6,
中心:myLatLng,
mapTypeId:'混合',
可单击图标:false
};
map=new google.maps.map(document.getElementById(“mapindex”),myOptions);
//创建搜索框并将其链接到UI元素。
var input=document.getElementById('pac-input');
var searchBox=newgoogle.maps.places.searchBox(输入);
map.controls[google.maps.ControlPosition.TOP\u CENTER].push(输入);
//将搜索框结果偏向当前地图的视口。
addListener('bounds_changed',function(){
searchBox.setBounds(map.getBounds());
});
var标记=[];
//侦听用户选择预测并检索时激发的事件
//关于那个地方的更多细节。
searchBox.addListener('places\u changed',function(){
var places=searchBox.getPlaces();
如果(places.length==0){
返回;
}
//清除旧的标记。
markers.forEach(函数(marker){
marker.setMap(空);
});
标记=[];
//对于每个位置,获取图标、名称和位置。
var bounds=new google.maps.LatLngBounds();
地点。forEach(功能(地点){
如果(!place.geometry){
log(“返回的位置不包含几何图形”);
返回;
}
变量图标={
url:place.icon,
大小:新谷歌地图大小(71,71),
来源:新google.maps.Point(0,0),
主播:新google.maps.Point(17,34),
scaledSize:new google.maps.Size(25,25)
};
//为每个地方创建一个标记。
var图像处理http://www.instead.com.pl/target2.png';
markers.push(新的google.maps.Marker)({
地图:地图,
图标:图像,
标题:place.name,
位置:place.geometry.location
}));
if(place.geometry.viewport){
//只有地理代码具有视口。
联合(place.geometry.viewport);
}否则{
扩展(place.geometry.location);
}
});
映射边界(bounds);
});
}
函数inputFocus(){
document.getElementById(“pac输入”).focus();
}
函数start(){
初始化();
inputFocus();
}
window.onload=开始;

如果我在setFocus上添加一个延迟(正如Sergii Vorobei在评论中所建议的那样),它就会工作

google.maps.event.addListenerOnce(map, 'idle', function(){
  setTimeout(inputFocus, 1000);
});
(您可能不需要“空闲”事件侦听器,但它可能更安全)

代码片段:

#mapindex{
高度:400px;
宽度:100%;
}

函数初始化(){
var mylatng=new google.maps.LatLng(52.015460,18.498087);//地图在这里居中
变量myOptions={
缩放:6,
中心:myLatLng,
mapTypeId:'混合',
可单击图标:false
};
函数inputFocus(){
document.getElementById(“pac输入”).focus();
}
map=new google.maps.map(document.getElementById(“mapindex”),myOptions);
//创建搜索框并将其链接到UI元素。
var input=document.getElementById('pac-input');
var searchBox=newgoogle.maps.places.searchBox(输入);
map.controls[google.maps.ControlPosition.TOP\u CENTER].push(输入);
//将搜索框结果偏向当前地图的视口。
addListener('bounds_changed',function(){
searchBox.setBounds(map.getBounds());
});
var标记=[];
//侦听用户选择预测并检索时激发的事件
//关于那个地方的更多细节。
searchBox.addListener('places\u changed',function(){
var places=searchBox.getPlaces();
如果(places.length==0){
返回;
}
//清除旧的标记。
markers.forEach(函数(marker){
marker.setMap(空);
});
标记=[];
//对于每个位置,获取图标、名称和位置。
var bounds=new google.maps.LatLngBounds();
地点。forEach(功能(地点){
如果(!place.geometry){
log(“返回的位置不包含几何图形”);
返回;
}
变量图标={
url:place.icon,
大小:新谷歌地图大小(71,71),
来源:新google.maps.Point(0,0),
主播:新google.maps.Point(17,34),
scaledSize:new google.maps.Size(25,25)
};
//为每个地方创建一个标记。
var图像处理http://www.instead.com.pl/target2.png';
markers.push(新的google.maps.Marker)({
地图:地图,
图标:图像,
标题:place.name,
位置:place.geometry.location
}));
如果(place.geo)
google.maps.event.addListenerOnce(map, 'idle', function(){
  setTimeout(inputFocus, 1000);
});