Javascript 访问JS数组在二维Google地图上放置标记

Javascript 访问JS数组在二维Google地图上放置标记,javascript,google-maps,Javascript,Google Maps,可能重复: 我有一个数组: var CITIES = { "Buenos Aires": {latitude: -34.6084, longitude: -58.3732}, "Santiago": {latitude: -33.4254, longitude: -70.5665}, "Gaborone": {latitude: -24.6541, longitude: 25.9087}, ... } 我需要在二维谷歌地图上放置标记;我尝试使用此函数来实现这一点: /

可能重复:

我有一个数组:

var CITIES = {
  "Buenos Aires":
  {latitude: -34.6084, longitude: -58.3732},
  "Santiago":
  {latitude: -33.4254, longitude: -70.5665},
  "Gaborone":
  {latitude: -24.6541, longitude: 25.9087},
...
}
我需要在二维谷歌地图上放置标记;我尝试使用此函数来实现这一点:

/*
 * void
 * mark()
 *
 * Markes locations of study abroad programs all around the world map
 */

function mark()
{
    // mark programs
    for (var city in CITIES)
    {
        // plant cities on map
        new google.maps.Marker({
         icon: "http://google-maps-icons.googlecode.com/files/smallcity.png",
         map: map,
         position: new google.maps.LatLng(CITIES[city].latitude, CITIES[city].longitude),
         title: 'CITIES[city]'
        });
    }
}
控制台上没有错误

如何使标记出现?(错误是否与我引用城市[城市]有关?

Zoom 你放大得太远了吗?因为如果您处理以下问题(),这对我很有效:

map的问题
mark
函数中,使用变量
map
。它没有在您引用的任何代码中定义,它是在封闭范围中定义的吗?如果不是,那很可能就是问题所在

您在下面评论说它是一个全局变量,表示它是
var map=null。我不认为它可以是
null
,但是,根据需要,它应该是
Map
StreetViewPanorama
实例。例如,如以下示例所示:

城市[城市]的问题
如果要访问值
CITIES[city]
,而不是

title: 'CITIES[city]'
你想要

title: CITIES[city]
(没有引用)但我认为你更可能只想:

title: city
…因为
CITIES[city]
是一个对象,您可能需要城市名称,而不是对象的默认字符串化(将是
[object object]

(没有引用)。因此:


离题#1 我可能会缓存查找,尽管一个像样的JavaScript实现可以为您做到这一点(有很多不好的实现可能不会做到):


离题#2
城市
不是数组。它是一个非数组对象。但这很好,您使用的
for..in
循环无论如何都是用于循环的,这样就可以了。:-)


离题3 你需要一个
在您的
城市
声明末尾:

var CITIES = {
    ....
}; // <== here
var城市={
....
}; //  //默认纬度
var纬度=42.3745615030193

//默认经度 变量经度=-71.11803936751632

//全球地图参考 var映射

//加载谷歌地图API的第3版 load(“maps”,“3”,{other_参数:“sensor=false”})

/* *空虚 *加载() * *装载地图。 */

函数加载() { var latlng,myOptions

// Get the map
latlng = new google.maps.LatLng(LATITUDE, LONGITUDE);
myOptions = {
  zoom: 3,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
return new google.maps.Map(document.getElementById("map"),
    myOptions);
}

/* *空虚 *马克() * *在世界地图上标出留学项目的位置 */

函数标记() { var cityName,城市

for (cityName in CITIES)
{
city = CITIES[cityName];

// plant cities on map
new google.maps.Marker({
 icon: "http://google-maps-icons.googlecode.com/files/smallcity.png",
 map: map,
 position: new google.maps.LatLng(city.latitude, city.longitude),
 title: cityName
});
}
}


哈佛海外:地图


哈佛海外:地图
|
|
|
|
|
|


欢迎来到StackOverflow!在你的问题右侧,当你打字时,有一个标有“如何格式化”的框。值得一读。:-)实际上,这并不是OP上一个问题的重复。上一个问题是关于上面没有出现的语法错误。不要发布同一个问题两次。而是用新的信息更新你以前的问题@克劳德:是的,但实际问题是一样的。。。。在我看来,这两个问题太接近了。我为所有这些紧急情况道歉,我觉得这听起来像一个痛苦的孩子,但这确实是一个紧急情况。我需要让这些标记以某种方式出现,然后将此代码输出。谢谢!这确实有帮助,但它仍然不能使标记出现在地图上!我很抱歉听起来如此戏剧性和悲伤,但这确实是一个紧急情况。我需要把这个代码拿出来-标记就是不会出现@约翰:祝你好运。我更新了一点答案(大部分在顶部),可能会有帮助。//映射var map=null的全局引用;地图应该有不同的定义吗?这就是我现在拥有的。显示地图,而不是标记。我需要帮助!啊!约翰,请不要用答案来澄清你的问题。相反,编辑问题。(和问题一样,正确的格式也会有帮助。)嘿,T.J.,我很抱歉回到你的现场示例——我在地图上没有看到位置标记。这张地图看起来很好,即使对我来说也是如此。只是标记没有出现在地图上。你点击按钮了吗?我在那一点加上标记。对我来说,它工作得非常完美:加载时,地图就会出现。单击该按钮时,将显示标记。
var cityName, city;

for (cityName in CITIES)
{
    city = CITIES[cityName];

    // plant cities on map
    new google.maps.Marker({
     icon: "http://google-maps-icons.googlecode.com/files/smallcity.png",
     map: map,
     position: new google.maps.LatLng(city.latitude, city.longitude),
     title: cityName
    });
}
var CITIES = {
    ....
}; // <== here
// Get the map
latlng = new google.maps.LatLng(LATITUDE, LONGITUDE);
myOptions = {
  zoom: 3,
  center: latlng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
};
return new google.maps.Map(document.getElementById("map"),
    myOptions);
for (cityName in CITIES)
{
city = CITIES[cityName];

// plant cities on map
new google.maps.Marker({
 icon: "http://google-maps-icons.googlecode.com/files/smallcity.png",
 map: map,
 position: new google.maps.LatLng(city.latitude, city.longitude),
 title: cityName
});
}
  <div id="top">
    <a href="index.php"><img alt="Harvard Abroad" src="Images/logo.jpg"></a>
  </div>

  <div id="logo">
    Harvard Abroad: Mapped
  </div>

  <div id="button">
    <input onclick="mark();" type="button" value="Map">
  </div>

  <div id="map">
  </div>

  <div id="bottom">
  |
  <a href="index.php" style="font-weight: bold">index</a>
  |
  <a href="search.php" style="font-weight: bold">search</a>
  |
  <a href="comment.php" style="font-weight: bold">comment</a>
  |
  <a href="viewcomment.php" style="font-weight: bold">view comments</a>      
  |
  <a href="logout.php" style="font-weight: bold">log out</a>
  |
  </div>