简体   繁体   English

扩展 Google Maps API v3 类的最佳方式

[英]Best way to extend Google Maps API v3 Classes

A number of the classes in Google Maps API v3 can be extended, specifically google.maps.MVCObject and google.maps.OverlayView .可以扩展 Google Maps API v3 中的许多类,特别是google.maps.MVCObjectgoogle.maps.OverlayView

In some of the examples, they will extend a class in the callback function initMap .在一些示例中,它们将在回调函数initMap扩展一个类。 My application is more robust than those examples and would prefer not to define a bunch of classes in the callback function.我的应用程序比那些示例更健壮,并且不想在回调函数中定义一堆类。

Is the solution to (A) include Google Maps API before my own script and not include a callback function? (A) 的解决方案是在我自己的脚本之前包含 Google Maps API 并且不包含回调函数吗? Or (B) do I just define everything in the callback function?或者(B)我是否只是在回调函数中定义所有内容? Or (C) some other approach.或者 (C) 一些其他方法。

Option A选项 A

<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY"></script>
<script src="./assets/js/main.js" type="module"></script>

Option B选项 B

<script src="./assets/js/main.js" type="module"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&callback=initMap"></script>

Where initMap is in main.js and looks something like this: initMapmain.js如下所示:

function initMap() {

  class Alpha extends google.maps.MVCObject {}
  class Bravo extends google.maps.MVCObject {}
  class Charlie extends google.maps.MVCObject {}
  // More classes.
  class Zulu extends google.maps.MVCObject {}

  // Rest of code.

}

Option C选项 C

Some other approach.一些其他的方法。

The documentation describes the following way top extend maps classes:该文档描述了顶部扩展映射类的以下方式:

The MVCObject constructor is guaranteed to be an empty function, and so you may inherit from MVCObject by simply writing MySubclass.prototype = new google.maps.MVCObject(); MVCObject 构造函数保证是一个空函数,因此您可以通过简单地编写 MySubclass.prototype = new google.maps.MVCObject(); 来继承 MVCObject。

And并且

Inherit from this class by setting your overlay's prototype: MyOverlay.prototype = new google.maps.OverlayView();.通过设置叠加层的原型从此类继承:MyOverlay.prototype = new google.maps.OverlayView();。 The OverlayView constructor is guaranteed to be an empty function. OverlayView 构造函数保证是一个空函数。

So the (one possible) option C would be to define your classes separately and then only configure the inheritance inside the initMap:所以(一种可能的)选项C是单独定义你的类,然后只在 initMap 中配置继承:

function initMap() {

  Alpha.prototype = new google.maps.MVCObject();
  Bravo.prototype = new google.maps.MVCObject();
  ...
}

Or, even better, to keep everything together, you can have some bootstrap function inside your library file, so in the initMap you would just do this:或者,更好的是,为了将所有内容放在一起,您可以在库文件中包含一些引导函数,因此在initMap您只需执行以下操作:

// in my_library.js:
// For now we don't mention that our class extends MVCObject
function Alpha() {
    console.log('Constructed Alpha');
    this.my_method = function() {
       // the `parent_method` can be defined in the
       // prototype we assign later
       this.parent_method();
    }
}

function Bravo() {
    console.log('Constructed Alpha');
}    

// The function to dynamically subclass our classes.
function init() {
   Alpha.prototype = new google.maps.MVCObject();
   Bravo.prototype = new google.maps.MVCObject();
}

// The callback for google maps script.
function initMap() {
    // invoke the `init` from my_library.
    my_library.init();;
}

The above uses instance methods (we define Alpha methods inside the constructor), alternatively we could define the constructor without methods, immediately create the instance and define the methods on it:上面使用实例方法(我们在构造函数中定义Alpha方法),或者我们可以定义没有方法的构造函数,立即创建实例并在其上定义方法:

function Alpha() {
    console.log('Constructed Alpha');
}

var alpha = new Alpha();
alpha.my_method = function() {
   this.parent_method();
}

// The function to dynamically subclass our classes.
function init() {
   alpha.prototype = new google.maps.MVCObject();
}

To create more Alpha instances, we can clone the existing alpha object.要创建更多Alpha实例,我们可以克隆现有的alpha对象。

One more alternative is to define own object using the prototype and then use Alpha.prototype.prototype = MVCObject construct:另一种选择是使用原型定义自己的对象,然后使用Alpha.prototype.prototype = MVCObject构造:

function Alpha() {
    console.log('Constructed Alpha');
}

Alpha.prototype.my_method = function() {
   this.parent_method();
}

// The function to dynamically subclass our classes.
function init() {
   // We can not overwrite Alpha.prototype as we will loose
   // the methods we defined, so we assign the prototype of
   // the prototype
   Alpha.prototype.prototype = new google.maps.MVCObject();
}

You can use version A and later in your code you can append the initMap callback in your main.js file.您可以使用版本 A,然后在您的代码中,您可以在 main.js 文件中附加 initMap 回调。 IN this way you'll have to use ajax calls to apply yout callback function.通过这种方式,您将不得不使用 ajax 调用来应用您的回调函数。

Otherwise you'll have to use option B from the start, and define the initMap function in your main.js file.否则,您必须从一开始就使用选项 B,并在 main.js 文件中定义 initMap 函数。

You should also load the google maps api in async mode:您还应该以异步模式加载 google maps api:

<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY" async defer></script>

Documentation and example: https://developers.google.com/maps/documentation/javascript/examples/map-simple文档和示例: https : //developers.google.com/maps/documentation/javascript/examples/map-simple

in separate, say, my_lib.js file ( it should be declared after '... maps.googleapis.com/..etc ' string within < head > tag of your html page ) :在单独的,比如说, my_lib.js文件中它应该在 '... maps.googleapis.com/..etc ' 字符串之后声明在你的 html 页面的<head>标签内):

class MyPolygon extends google.maps.Polygon{ 
    field1;field2; map
    constructor(latlngs,bcolor,idLcl, id_of_start,id_of_finish,map ) {
        super({
        paths: latlngs,
        strokeColor: ''+bcolor,
        strokeOpacity: 0.8,
        strokeWeight: 2,
        suppressUndo: true,
        fillColor: '',
        fillOpacity: 0.35,
        my_id: idLcl,
        my_color: ''+bcolor,
        addrs_ids: [],
        start_id: id_of_start,
        finish_id:  id_of_finish,});
      this.map=map
      this.setMap(map)

   }
    func1(aParam){
       this.field1=0 ; this.field2=null
   }
}
class SomeOtherPlaceConsumableClass(){}

and in your html withinin javascript tag section you have,eg :并在您的 html insidein javascript 标记部分中,例如:

idLcl = idPrm
polygon=new MyPolygon(latlngs,color,idLcl, id_of_start,id_of_finish,map)

You can do that with any other Map API class您可以使用任何其他 Map API 类来做到这一点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM