Javascript 谷歌地图上没有标记-奇怪的错误

Javascript 谷歌地图上没有标记-奇怪的错误,javascript,google-maps,maps,google-api,Javascript,Google Maps,Maps,Google Api,我试图让标记显示在一个样式化的谷歌地图上,使用谷歌api 如果我这样做: function initialize() { var mapOptions = { zoom: 8, center: new google.maps.LatLng(51.49079, -0.10746), mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new GMaps({

我试图让标记显示在一个样式化的谷歌地图上,使用谷歌api

如果我这样做:

 function initialize() {
    var mapOptions = {
      zoom: 8,
      center: new google.maps.LatLng(51.49079, -0.10746),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new GMaps({
                div: "#map1",
                lat: 41.895465,
                lng: 12.482324,
                zoom: 1, 
                zoomControl : true,
                zoomControlOpt: {
                    style : "SMALL",
                    position: "TOP_LEFT"
                },
                panControl : true,
                streetViewControl : false,
                mapTypeControl: false,
                overviewMapControl: false
            });


    var styles = [
                    {
                    featureType: "road",
                    stylers: [
                          { "hue": "#ff0000" },
                          { "lightness": -11 },
                          { "saturation": -5 }
                  ]
                }, {
                    featureType: "road",
                        stylers: [
                         { "saturation": -26 }
                  ]
                }
            ];

    map.addStyle({
                styledMapName:"Styled Map",
                styles: styles,
                mapTypeId: "map_style"  
            });
    map.setStyle("map_style");

    // Add 5 markers to the map at random locations
    var southWest = new google.maps.LatLng(-31.203405, 125.244141);
    var northEast = new google.maps.LatLng(-25.363882, 131.044922);

    //var bounds = new google.maps.LatLngBounds(southWest, northEast);
    //map.fitBounds(bounds);


    var cities = [
    {
      name: 'London',
      position: new google.maps.LatLng(51.49079,-0.10746),
      info: 'Bewohner: 7,556,900'
    },
    {
      name: 'Paris',
      position: new google.maps.LatLng(48.856667,2.350987),
      info: 'Bewohner: 2,193,031'
    },
    {
      name: 'Berlin',
      position: new google.maps.LatLng(52.523405,13.4114),
      info: 'Bewohner: 3,439,100'
    }        
  ];


  cities.forEach(function(element, index, array) {

    var marker = new google.maps.Marker({
      position: element.position,
      map: map[0],
      title: element.name
    });    

    var infoWindow = new google.maps.InfoWindow({
      content: element.info
    });

    google.maps.event.addListener(marker, 'click', function() {
      infoWindow.open(map, marker);
    });
  });




  }



  google.maps.event.addDomListener(window, 'load', initialize);
我在firebug中没有发现错误,但没有出现标记

如果我改变这一点:

var marker = new google.maps.Marker({
      position: element.position,
      map: map[0],

我收到一条错误消息,表示属性映射的值无效

我的代码中的错误在哪里


谢谢大家!

使用google.maps.Map来创建地图,而不是gmap 试试这个链接。这可能对你有帮助

编辑了你的代码

var marker;
  var map;
  function initialize() {
    var mapOptions = {
                      zoom: 4,
                      center: new google.maps.LatLng(51.49079, -0.10746),
                      mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);

    var styles = [
                {
                featureType: "road",
                stylers: [
                      { "hue": "#ff0000" },
                      { "lightness": -11 },
                      { "saturation": -5 }
              ]
            }, {
                featureType: "road",
                    stylers: [
                     { "saturation": -26 }
              ]
            }
        ];
    map.setOptions({styles: styles});

    var cities = [
                {
                  name: 'London',
                  position: new google.maps.LatLng(51.49079,-0.10746),
                  info: 'Bewohner: 7,556,900'
                },
                {
                  name: 'Paris',
                  position: new google.maps.LatLng(48.856667,2.350987),
                  info: 'Bewohner: 2,193,031'
                },
                {
                  name: 'Berlin',
                  position: new google.maps.LatLng(52.523405,13.4114),
                  info: 'Bewohner: 3,439,100'
                }        
              ];

    for(var i=0;i<cities.length;i++)
    {
        var element=cities[i];
        var marker = new google.maps.Marker({
          position: element.position,
          map: map,
          title: element.name
        });    

        var infoWindow = new google.maps.InfoWindow({
          content: element.info
        });

        google.maps.event.addListener(marker, 'click', function() {
          infoWindow.open(map, marker);
        });
    }

  }
var标记;
var映射;
函数初始化(){
变量映射选项={
缩放:4,
中心:新google.maps.LatLng(51.49079,-0.10746),
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById('map_canvas'),
地图选项);
变量样式=[
{
特色类型:“道路”,
样式:[
{“色调”:“#ff0000”},
{“亮度”:-11},
{“饱和”:-5}
]
}, {
特色类型:“道路”,
样式:[
{“饱和”:-26}
]
}
];
setOptions({styles:styles});
var城市=[
{
名称:“伦敦”,
位置:新google.maps.LatLng(51.49079,-0.10746),
信息:“贝沃纳:7556900”
},
{
名称:“巴黎”,
位置:新google.maps.LatLng(48.856667,2.350987),
信息:“贝沃纳:2193031”
},
{
名称:“柏林”,
位置:新google.maps.LatLng(52.523405,13.4114),
信息:“贝沃纳:3439100”
}        
];

对于(var i=0;i)您在地图中包含哪些外部javascript?Google Maps API v3?gmaps.js?您使用的是什么版本?
var marker;
  var map;
  function initialize() {
    var mapOptions = {
                      zoom: 4,
                      center: new google.maps.LatLng(51.49079, -0.10746),
                      mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
    map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);

    var styles = [
                {
                featureType: "road",
                stylers: [
                      { "hue": "#ff0000" },
                      { "lightness": -11 },
                      { "saturation": -5 }
              ]
            }, {
                featureType: "road",
                    stylers: [
                     { "saturation": -26 }
              ]
            }
        ];
    map.setOptions({styles: styles});

    var cities = [
                {
                  name: 'London',
                  position: new google.maps.LatLng(51.49079,-0.10746),
                  info: 'Bewohner: 7,556,900'
                },
                {
                  name: 'Paris',
                  position: new google.maps.LatLng(48.856667,2.350987),
                  info: 'Bewohner: 2,193,031'
                },
                {
                  name: 'Berlin',
                  position: new google.maps.LatLng(52.523405,13.4114),
                  info: 'Bewohner: 3,439,100'
                }        
              ];

    for(var i=0;i<cities.length;i++)
    {
        var element=cities[i];
        var marker = new google.maps.Marker({
          position: element.position,
          map: map,
          title: element.name
        });    

        var infoWindow = new google.maps.InfoWindow({
          content: element.info
        });

        google.maps.event.addListener(marker, 'click', function() {
          infoWindow.open(map, marker);
        });
    }

  }