无法在google map api v3中调用方法getPosition [英] Cannot call method getPosition in google map api v3

查看:106
本文介绍了无法在google map api v3中调用方法getPosition的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在此代码中使用方法getPosition:

I want to use method getPosition in this code :

alert("Tap location in this map");
google.maps.event.addListener(map, 'click', function(event) {
    mArray[count] = new google.maps.Marker({
        position: event.latLng,
        map: map
    });
});
mArray[count].getPosition();

但我无法调用getPosition。

But I cannot call getPosition.

Uncaught TypeError: Cannot call method 'getPosition' of undefined

有人可以解释吗?谢谢

Can someone explain it ? Thanks

推荐答案

您的问题的方式是,mArray [count] .getPosition()在click事件运行之前执行它已被定义),该代码不会执行,直到点击发生。这应该工作(但不知道为什么你想这样做):

The way your question has it, mArray[count].getPosition() executes before the click event runs (but after it has been defined), that code doesn't execute until the 'click' happens. This should work (but not sure why you would want to do it this way):

alert("Tap location in this map");
google.maps.event.addListener(map, 'click', function(event) {
    mArray[count] = new google.maps.Marker({
        position: event.latLng,
        map: map
    });
    mArray[count].getPosition();
});

这篇关于无法在google map api v3中调用方法getPosition的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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