Home > other >  Doud with the addMarker method
Doud with the addMarker method

Time:01-22

my name is George, I´m making an android app in android studio with google maps, I have a question, I´m still a beginner so I don´t understand why the data that you send to the method addmarker has the constructor method and others methods united whith points, what this methods chain means? why they are united with points and sended to the method addmarker? thanks for your help and if it needs a large explication you can help me telling what lesson or theme I have to study:) i atach the part of the code that I refer

mMap.addMarker(new MarkerOptions().position(posicion).title("Primer posicion").snippet("Hora de llegada: "   hora1));

CodePudding user response:

The chain term you used is a good one as it is referred to as method chaining (or more specifically method cascading) and is simply a way of invoking a series of methods on an object (the MarkerOptions instance) in one statement rather than a series of statements. (It is not unique to Java.)

Here is an equivalent without the chaining:

MarkerOptions mo = new MarkerOptions().
mo.position(posicion);
mo.title("Primer posicion");
mo.snippet("Hora de llegad: "   hora1);
mMap.addMarker(mo);

That's all it is - syntactic sugar.

Now for a class to implement method chaining each of the methods above would return the this object. So for example the position method (in the MarkerOptions implementation) would look something like:

public MarkerOptions position(LatLng latLng) {
    // do something with latLng
    return this;
}

So each method that supports chaining would return the current instance with the this keyword.

As for the syntax that is just a series of object method invocations just as the expanded version but in series.

Since addMarker accepts a MarkerOptions instance, the concluding snippet method in the series satisfies the signature since it returns a MarkerOptions.

The Builder Pattern is a one design which typically uses method chaining.

One limitation of method chaining is a method can't return anything but the instance reference.

  •  Tags:  
  • Related