混合谷歌地图自定义叠加层与骨干网查看 [英] Mixing Google Maps custom overlays with Backbone Views

查看:123
本文介绍了混合谷歌地图自定义叠加层与骨干网查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR

PinView.prototype = _.extend(PinView.prototype,google.maps.OverlayView.prototype)正确的方法有从另一个骨干查看继承下课?

Is PinView.prototype = _.extend(PinView.prototype, google.maps.OverlayView.prototype) the "proper" way to have a Backbone View inherit from another "class"?

龙读

我们正在重做骨干使用我们的网站和包括一些映射功能正在工作。

We're redoing our site using Backbone and are working on including some mapping functionality.

我已经得到了处理放置一个骨干视图< D​​IV> 取值到浏览器窗口中的特定点;这似乎是一个自然的事情,以便有谷歌的地图API将它们放在地理坐标扩展。

I've got a Backbone view that handles placing <div>s onto specific points within the browser window; this seems like a natural thing to extend in order have Google's Map API place them on geographical coordinates.

根据本谷歌的API 时,为了产生自定义覆盖你创建一个新的对象和原型为该对象设为google.maps.OverlayView的新实例。然后,实施该对象的前三位功能,使物体响应:

According to the Google API, in order to generate a custom overlay you create a new object and set the prototype for that object to a new instance of google.maps.OverlayView. You then implement three functions on top of that object so that the object responds to:

使用onAdd

onRemove

其中,使用onAdd 负责生成HTML,然后将它放在地图上。这随后调用哪些职位正确地根据您所提供的经纬度对和边界的元素。 onRemove 被调用,当你想摆脱你的层。

Where onAdd is responsible for generating the HTML and then applying it on top of the Map. This subsequently calls draw which positions the element correctly according to the LatLng pairs and bounds you've provided. onRemove gets called when you want to get rid of your layer.

所以我修改我的视图,包括这三种方法(这只是调用渲染和unrender并绑定到我的收藏)。然后让魔术发生我做的:

So I've modified my View to include these three methods (which just call render and unrender and are bound to my collection). And then to make "the magic happen" I'm doing:

PinView.prototype = _.extend(PinView.prototype,google.maps.OverlayView.prototype)

这是否正确?我可以张贴code的查看和它所基于的型号,但说实话,他们是无关的这个例子 - code ++工程,我能自定义地点 DIV s到地图上的骨干模型,视图和控制器组件生成没有问题,就是我要问我猜(也许这个问题是programmers.se更中肯,所以让我知道我会移动)。

Does this look right? I can post the code for the View and the Model on which it's based, but honestly, they're irrelevant to this example -- the code works and I'm able to place custom divs generated through Backbone model, view and controller components on the map without a issue, what I'm asking I guess (and maybe this question is more apropos for programmers.se, so let me know and I'll move it).

这似乎是让我PinView既有骨干查看和谷歌地图OverlayView的最简单的方法,但我不舒服的原型继承100%知道我在做什么错误或打破一些地方下山的路。

This seems to be the easiest way to make my PinView both a Backbone View and a Google Maps OverlayView, but I'm not 100% comfortable with prototypal inheritance to know if I'm doing something "wrong" or breaking something somewhere down the road.

推荐答案

好主意!我平时有点怀疑天气与否你是正确的当事情工作,所以如果你还没有碰到一个搅局者和叠加显示出来,并做什么the're应该做的我会说你是

Nice idea! I'm usually a bit sceptical about weather or not you're 'correct' when things work so if you haven't run into a showstopper and the overlays shows up and does what the're supposed to do I'd say you are.

一件事退房近,虽然:

这是不是(也不能)是真正的多重继承 - 这一概念是不是在一个基于原型的语言真的很重要:一个实现的方法将不可避免的双赢,并覆盖其他的实现,在至少使用 _时,扩展()

This isn't (and can't) be "real" multiple inheritance - that concept isn't really relevant in a prototype based language: one implementation of a method will inevitable "win" and overwrite the other implementation, at least when using _.extend()

这意味着,如果有成员或方法具有相同的名称在 Backbone.View google.maps.OverlayView 之一的最后的在 _。延长()通话将是一个接管。但是,当我检查他们使用Chrome的开发工具,我没有看到这样的任何明显的冲突。

This means that if there are members or methods with the same names in Backbone.View and google.maps.OverlayView the one last in your _.extend() call will be the one that takes over. But when I inspect them using Chrome's Developer Tools I didn't see any obvious collision of this kind.

所以,我的建议:继续使用这个,只是测试了很多。我很想看到一段时间这种技术的一个例子。

So my recommendation: continue using this, just test a lot. I'd love to see an example of this technique some time.

这篇关于混合谷歌地图自定义叠加层与骨干网查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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