扩展 google.maps.Marker

发布于 2024-10-06 17:28:21 字数 396 浏览 9 评论 0原文

我发现了这个关于在 v2 google 地图中扩展 GMarkers 的精彩教程:http://www .googlemapsbook.com/2007/01/22/extending-gmarker/

不幸的是,在 v3 中,标记的设置非常不同(例如,您必须传入地图,它将作为参数添加到其中) ,因此不能有一个独立的 google.maps.Marker 对象作为原型,或者至少在地图初始化之后)。

有谁知道如何扩展谷歌地图 v3 标记?

*编辑 - 事实证明我错误地将地图作为必需参数。我稍后会将我的 v3 扩展作为答案发布,并将其标记为社区问题

I found this great tutorial for extending GMarkers in v2 google maps: http://www.googlemapsbook.com/2007/01/22/extending-gmarker/

Unfortunately, in v3 the set-up of a marker is very different (eg you have to pass in the map it will be added to as a parameter, so can't have a standalone google.maps.Marker object to act as a prototype, or at least not until after your map has initialised).

Does anyone know how to extend a google maps v3 marker?

*edit - turns out I was wrong about the map being a required parameter. I'll post my v3 extension as an answer later and mark this as a community question

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心不设防 2024-10-13 17:28:21

下面是我最终使用的内容(我已经删除了用于自定义标记的大量代码,只留下了简单的框架,所以我可能在编辑中犯了错误)。 newObj() 是一个函数(基于 Douglas Crockford 的代码),用于从原型生成新对象,而不是使用构造函数。

function newObj(o) {
    var params = Array.prototype.slice.call(arguments,1);
    function F() {}
    F.prototype = o;
    var obj = new F();
    if(params.length) {
        obj.init.apply(obj,params);
    }
    return obj;
}

var MyMarkerProto = function() {
    var proto  = new google.maps.Marker(new google.maps.LatLng(0, 0));

    proto.init = function (data) {
        this.setPosition(new google.maps.LatLng(parseFloat(data.lat), parseFloat(data.lng)));
    }
    return proto;

}();

var myMarker = newObj(MyMarkerProto, {
    lat: 51,
    lng: 48,
    otherData: "some other value"
});

Below is what I eventually used (I've stripped out a lot of code that I used for my custom marker to leave just the bare bones so I may have made a mistake in the editing). newObj() is a function (based on Douglas Crockford's code) for generating a new object from a prototype, rather than using a constructor function.

function newObj(o) {
    var params = Array.prototype.slice.call(arguments,1);
    function F() {}
    F.prototype = o;
    var obj = new F();
    if(params.length) {
        obj.init.apply(obj,params);
    }
    return obj;
}

var MyMarkerProto = function() {
    var proto  = new google.maps.Marker(new google.maps.LatLng(0, 0));

    proto.init = function (data) {
        this.setPosition(new google.maps.LatLng(parseFloat(data.lat), parseFloat(data.lng)));
    }
    return proto;

}();

var myMarker = newObj(MyMarkerProto, {
    lat: 51,
    lng: 48,
    otherData: "some other value"
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文