Javascript 谷歌地图API V3设置中心不工作

Javascript 谷歌地图API V3设置中心不工作,javascript,google-maps,Javascript,Google Maps,我在让setCenter工作时遇到问题。我的JS控制台告诉我: myMap is not defined myMap.setCenter(newLatLng); 它在historyMap()函数中有明确定义 我的JavaScript 44 function historyMap() { 45 var travelCoords = new Array(); 46 var allCoords = new Array(); 47 var my

我在让setCenter工作时遇到问题。我的JS控制台告诉我:

myMap is not defined

myMap.setCenter(newLatLng);
它在historyMap()函数中有明确定义

我的JavaScript

44     function historyMap() {
45         var travelCoords = new Array();
46         var allCoords = new Array();
47         var myOptions = {
48                 center: new google.maps.LatLng(<?php echo $places[$lastPlace]['lat']; ?>, <?php echo $places[$lastPlace]['long']; ?>),
49                 zoom: 8,
50                 mapTypeId: google.maps.MapTypeId.HYBRID
51         };
52         var myMap = new google.maps.Map(document.getElementById("map"), myOptions);
53         allCoords = [
54         <?php for($i = 0; $i < $numPlaces; $i++) { ?>
55                 new google.maps.LatLng(<?php echo $places[$i]['lat']; ?>, <?php echo $places[$i]['long'];?>),
56         <?php } ?>
57         ];
58         var travelPath = new google.maps.Polyline({
59                 path: allCoords,
60                 strokeColor: "#FF0000",
61                 strokeOpacity: 1.0,
62                 strokeWeight: 4
63         });
64         lastLatLng = new google.maps.LatLng(<?php echo $places[$lastPlace]   ['lat'];?>, <?php echo $places[$lastPlace]['long'];?>);
65         var marker = new google.maps.Marker({
66                 icon: 'img/<?php echo $icon; ?>',
67                 position: lastLatLng,
68                 map: myMap,
69                 title: '<?php echo $places[$numPlaces-1]['title'];?>'
70         });
71         travelPath.setMap(myMap);
72     }
73
74     function setMapCenter(lat,lng) {
75
76         var newLatLng = new google.maps.LatLng(lat,lng);
77         myMap.setCenter(newLatLng);
78
79     }
80
81     $(function() {
82         historyMap();
83     });
44函数历史映射(){
45 var travelCoords=新数组();
46 var allCoords=新数组();
47变量myOptions={
48中心:新的google.maps.LatLng(,),
49:8,
50 mapTypeId:google.maps.mapTypeId.HYBRID
51         };
52 var myMap=new google.maps.Map(document.getElementById(“Map”),myOptions);
53所有坐标=[
54
55新google.maps.LatLng(,),
56
57         ];
58 var travelPath=新的google.maps.Polyline({
59路径:allCoords,
60冲程颜色:“FF0000”,
61频闪不透明度:1.0,
62冲程重量:4
63         });
64 lastLatLng=新的google.maps.LatLng(,);
65 var marker=新的google.maps.marker({
66图标:“img/”,
67位置:lastLatLng,
68地图:我的地图,
69标题:“”
70         });
71 travelPath.setMap(myMap);
72     }
73
74功能设置映射中心(lat、lng){
75
76 var newLatLng=新的google.maps.LatLng(lat,lng);
77 myMap.setCenter(纽拉廷);
78
79     }
80
81$(函数(){
82历史地图();
83     });
调用setMapCenter函数的HTML:

<td><a href="javascript:void(0);" onClick="setMapCenter('49.261226','-123.113927')">Vancouver</a></td>


我非常感谢任何人提供的见解。

这是因为您的变量
myMap
是一个局部变量:它仅为函数historyMap()定义

初始化它,输出您的函数:

var myMap = {};
function historyMap() { ... }
// other functions

谢谢,效果很好。另外,我必须在historyMap()函数中从var myMap中删除var。