标记未显示在Google地图上 [英] Markers not showing on google maps

查看:72
本文介绍了标记未显示在Google地图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在地图上显示标记.但没有显示它们.

I'm trying to show markers on my map. but it's not showing them.

这是我使用的代码:

var map;
var markers = new Array();

function initialize() {
    var mapOptions = {
        zoom: 12,
        center: new google.maps.LatLng(27.9881, 86.9253),
    };
    var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

    google.maps.event.addListener(map, 'click', addMarker);
}

function addMarker(event) {
    markers.push(event.latLng);

    marker = new google.maps.Marker({
        position: event.latLng,
        map: map
    });

    console.log(markers);
}

google.maps.event.addDomListener(window, 'load', initialize);

控制台每次单击都会记录一个新坐标,因此将调用该函数并传递坐标.而且我真的看不到标记代码中的任何问题.

the console is logging on every click an new coordinate, so the function is called, and coordinates are passed. And I can't really see any problems in the marker code.

那么任何人都可以看到问题吗?

So can anyone see the problem?

推荐答案

map 是局部变量,仅在 initialize 内部可见.

map is a local variable, only visible inside initialize.

设置标记的 map -属性时,请使用 this 而不是 map :

Use this instead of map when you set the map-property of the marker:

function addMarker(event) {
    markers.push(event.latLng);

    marker = new google.maps.Marker({
        position: event.latLng,
        map: this
    });
}

说明:

回调将在触发事件的对象范围内执行.这里是Maps-instance(因为单击侦听器已应用于地图), this 将在回调中引用此对象.

the callback will be executed in the scope of the object that triggers the event. Here it is the Maps-instance(because the click-listener has been applied to the map ), this will refer to this object inside the callback.

这篇关于标记未显示在Google地图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆