最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Best way to extend Google Maps API v3 Classes - Stack Overflow

programmeradmin1浏览0评论

A number of the classes in Google Maps API v3 can be extended, specifically google.maps.MVCObject and google.maps.OverlayView.

In some of the examples, they will extend a class in the callback function 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? Or (B) do I just define everything in the callback function? Or (C) some other approach.

Option A

<script src=""></script>
<script src="./assets/js/main.js" type="module"></script>

Option B

<script src="./assets/js/main.js" type="module"></script>
<script src=";callback=initMap"></script>

Where initMap is in main.js and looks something like this:

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

Some other approach.

A number of the classes in Google Maps API v3 can be extended, specifically google.maps.MVCObject and google.maps.OverlayView.

In some of the examples, they will extend a class in the callback function 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? Or (B) do I just define everything in the callback function? Or (C) some other approach.

Option A

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

Option B

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

Where initMap is in main.js and looks something like this:

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

Some other approach.

Share Improve this question edited Nov 27, 2017 at 20:41 hungerstar asked Nov 15, 2017 at 18:13 hungerstarhungerstar 21.7k6 gold badges51 silver badges62 bronze badges 2
  • The Google Maps JavaScript API is loading asynchronously, I think the callback approach is the most appropriate for async nature of Maps JavaScript API. – xomena Commented Nov 17, 2017 at 12:31
  • I'm not totally against using a callback. I'm just trying to avoid having 600 lines of class definitions in my callback. Thanks for the feedback. – hungerstar Commented Nov 17, 2017 at 20:01
Add a ment  | 

3 Answers 3

Reset to default 4

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();

And

Inherit from this class by setting your overlay's prototype: MyOverlay.prototype = new google.maps.OverlayView();. The OverlayView constructor is guaranteed to be an empty function.

So the (one possible) option C would be to define your classes separately and then only configure the inheritance inside the 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:

// 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:

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.

One more alternative is to define own object using the prototype and then use Alpha.prototype.prototype = MVCObject construct:

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. IN this way you'll have to use ajax calls to apply yout callback function.

Otherwise you'll have to use option B from the start, and define the initMap function in your main.js file.

You should also load the google maps api in async mode:

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

Documentation and example: https://developers.google./maps/documentation/javascript/examples/map-simple

in separate, say, my_lib.js file ( it should be declared after '...maps.googleapis./..etc' string within < head > tag of your html page ) :

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,e.g. :

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

发布评论

评论列表(0)

  1. 暂无评论