-2

i want to get the marker latitude to another class using getters and setters. but getters always return null value. it totally make no sense to me. i am struggling to find out what's wrong here.

Here is my Location1 class Where i use Setters

public class Location1 extends AppCompatActivity implements OnMapReadyCallback {

//do changes inside loaded map
@Override
public void onMapReady(GoogleMap googleMap) {
    Toast.makeText(Location1.this, "Map Is Ready", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onMapReady: map is ready");
    mMap = googleMap;

    if (permission_Granted) {

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }
        mMap.setMyLocationEnabled(true);
        mMap.getUiSettings().setMyLocationButtonEnabled(false);
        mMap.getUiSettings().setCompassEnabled(true);



        //Afer Clicking google map marker info window
        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {


                //Here using setters 
                marker1 marker1 = new marker1();
                marker1.setMarkerLat(marker.getPosition().latitude);

                Toast.makeText(Location1.this,String.valueOf(marker.getPosition().latitude),Toast.LENGTH_SHORT).show();

                startActivity(new Intent(Location1.this, pop.class));
            }
        });
}

}

and my marker1 class

public class marker1 {

Double markerLat;

public Double getMarkerLat() {
    return markerLat;
}

public void setMarkerLat(Double markerLat) {
    this.markerLat = markerLat;
}
}

and pop class where i use Getters which return null value

public class pop extends AppCompatActivity   {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.garagepopup_1);


    GarageNameTextView = (TextView) findViewById(R.id.nameView);
    GarageAddrTextView = (TextView) findViewById(R.id.addresView);
    GarageContactTextView = (TextView) findViewById(R.id.phoneView);

    dbGarages= FirebaseDatabase.getInstance().getReference("Garages");

    //Using getters
    marker1 marker1 = new marker1();
    String lat=String.valueOf(marker1.getMarkerLat());

    Toast.makeText(pop.this,lat, Toast.LENGTH_SHORT).show();

}
}

i am stuck here. cannot proceed forward..any help would be appreciated.. Thanks.

1 Answers1

0

@Kasun_Chamara for get location in second activity you have some ways:

first: you must set markerLat as static field.

like this :

public class marker1 {

static Double markerLat;

public Double getMarkerLat() {
    return markerLat;
}

public void setMarkerLat(Double markerLat) {
    this.markerLat = markerLat;
}
}

second: use putExtra to send locationparameter :

mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {


                //Here using setters 
                marker1 marker1 = new marker1();
                marker1.setMarkerLat(marker.getPosition().latitude);

                Toast.makeText(Location1.this,String.valueOf(marker.getPosition().latitude),Toast.LENGTH_SHORT).show();

                Intent myintent = new Intent(Location1.this, pop.class);
                myintent.putExtra("Location",marker.getPosition().latitude);
                startActivity(myintent);
            }
        });

in second Activity:

getIntent().getIntExtra("Location",0);

Third:

According to @skandigraun you can send its object:

How to pass an object from one activity to another on Android

Amir133
  • 2,372
  • 2
  • 18
  • 34
  • This effectively restricts the programmer to have only one instance of this class... – skandigraun Sep 28 '18 at 16:41
  • Thanks. i simply missed it – Kasun Chamara Sep 28 '18 at 16:47
  • thanks @skandigraun , I think problem solved , see edited answer – Amir133 Sep 28 '18 at 17:11
  • If you have a static class variable, it will be the same in all instances. consider this: `marker1 m1=new marker1(); m1.setMarkerLat(1); marker1 m2=new marker1();m2.setMarkerLat(2); System.out.println("first marker: " + m1.getMarkerLat()); System.out.println("second marker: " + m2.getMarkerLat());` Both will print 2... Update: I missed the second version. Yeah, looks better, downvote retracted. – skandigraun Sep 28 '18 at 17:14
  • Thanks @amir and skandigraun – Kasun Chamara Sep 28 '18 at 17:33