谷歌地图标记不';不显示javascript
我正在与自定义地图上的标记进行斗争。地图的中心在正确的地方,但标记没有出现在那个地方。有人已经解决了同样的问题? 非常感谢你的帮助谷歌地图标记不';不显示javascript,javascript,google-maps,google-maps-api-3,maps,google-maps-markers,Javascript,Google Maps,Google Maps Api 3,Maps,Google Maps Markers,我正在与自定义地图上的标记进行斗争。地图的中心在正确的地方,但标记没有出现在那个地方。有人已经解决了同样的问题? 非常感谢你的帮助 function initialize() { var map_canvas = document.getElementById('map_canvas'); var map_options = { scrollwheel: false,
function initialize() {
var map_canvas = document.getElementById('map_canvas');
var map_options = {
scrollwheel: false,
zoom: 16,
center: new google.maps.LatLng(51.840164,4.33244),
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.TOP
},
panControl: false,
panControlOptions: {
position: google.maps.ControlPosition.TOP_RIGHT
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.LEFT_CENTER
},
scaleControl: false,
scaleControlOptions: {
position: google.maps.ControlPosition.TOP_LEFT
},
streetViewControl: false,
streetViewControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
}
}
var map = new google.maps.Map(map_canvas, map_options)
}
function addMarker(feature) {
var marker = new google.maps.Marker({
position: feature.position,
icon: 'locationpointer.png',
map: map_canvas
});
}
var features = [
{
position: new google.maps.LatLng(51.840164,4.33244),
}
];
for (var i = 0, feature; feature = features[i]; i++) {
addMarker(feature);
}
google.maps.event.addDomListener(window, 'load', initialize);
无法将标记添加到地图中。 可以使用marker.setMap(map),也可以通过marker属性传递它。 但是在您的代码中,map和map_canvas变量不在全局范围内,这意味着您的addMarker函数无法添加它,而map:map_canvas为空
var marker = new google.maps.Marker({
position: feature.position,
icon: 'locationpointer.png',
**map: map_canvas**
});
您必须稍微更改代码,才能将映射传递到addMarker函数。您需要在initialize函数中调用addMarker函数,以便在映射初始化之后而不是之前添加标记。您有两个选项用于初始化标记的map属性1。让它成为全球或2。将其传递给addMarker函数(如下所示)。还要注意,map_canvas是一个HTMLDOM元素,而不是google.maps.map,该变量的名称是“map”
谢谢你的帮助。但这对我不起作用<代码>var-map=new google.maps.map(地图画布,地图选项)var-point=new google.maps.LatLng(x,y);var marker=new google.maps.marker({位置:点,地图:地图,图标:'##',标题:“##.”})代码>这对我有用
function initialize() {
var map_canvas = document.getElementById('map_canvas');
var map_options = {
scrollwheel: false,
zoom: 16,
center: new google.maps.LatLng(51.840164,4.33244),
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.TOP
},
panControl: false,
panControlOptions: {
position: google.maps.ControlPosition.TOP_RIGHT
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.LEFT_CENTER
},
scaleControl: false,
scaleControlOptions: {
position: google.maps.ControlPosition.TOP_LEFT
},
streetViewControl: false,
streetViewControlOptions: {
position: google.maps.ControlPosition.LEFT_CENTER
}
}
var map = new google.maps.Map(map_canvas, map_options)
var features = [
{
position: new google.maps.LatLng(51.840164,4.33244),
}
];
for (var i = 0, feature; feature = features[i]; i++) {
addMarker(feature, map);
}
}
function addMarker(feature, map) {
var marker = new google.maps.Marker({
position: feature.position,
icon: 'locationpointer.png',
map: map
});
}
google.maps.event.addDomListener(window, 'load', initialize);