从外部访问Google地图 [英] Access Google Maps from outside

查看:135
本文介绍了从外部访问Google地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想从外部访问地图对象(另一个js文件)。

我有一些JavaScript代码,绘制了一个v3 GoogleMap。 b
$ b

有没有可能创建一个额外的全局变量来引用映射?

解决方案

单独的JS文件在同一个作用域中加载和执行,因此无论使用一个还是多个JS文件,都不应该有任何功能差异。



如果您控制两个脚本,您可以在脚本b中传入脚本a到脚本的映射的引用:

来自a.js:
onMapCreated(myMap); // myMap是对创建映射的引用



from b.js:
function onMapCreated map){
//用map
做一些事情}



很明显'namespacing'你的函数会是一个很好的想法。


I have some javascript code, that draws a v3 GoogleMap.

I want to access the map object from outside (another js file).

Is there a possibility without creating an additional global variable to reference to the map ?

解决方案

Separate JS files are loaded and executed in the same scope, so there shouldn't be any functional difference whether you use one or multiple JS files.

Providing you control both scripts, you could pass in a reference to the map from script a to a callback function in script b:

from a.js: onMapCreated(myMap); // myMap is a reference to the created map

from b.js: function onMapCreated(map) { // do something with map }

Obviously 'namespacing' your functions would be a good idea.

这篇关于从外部访问Google地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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