1

I have a class named MapsActivity.java and the associated xml file called activity_maps.xml.I also have another class named Sigiriya.java where in that I have an onClick() method which contains the code to move from Sigirya.java to MapsActivity.java.The thing I wanted to do was to pass the longitude and latitiude of a desired place on the button click, therefore I created an method in MapsActivity.Java that gets three parameters and pass that to onMapReady() method.And on the Sigiriya.java I created an object of MapsActivity.java and called the method setMapInfo() and passed the values.The problem is that the values doesnot get passed and on the map, it just shows some ocean and marker on top of equator.Please help me.

Sigiriya.java

public class Sigiriya extends AppCompatActivity{

TextView sigiriyaoverview;
Button readmorebtn;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    Button mapbtn = (Button)findViewById(R.id.mapsbtn);

    mapbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            MapsActivity map = new MapsActivity();
            map.setMapInfo(7,80,"Sigiriya");
            startActivity(new Intent(Sigiriya.this, MapsActivity.class));
        }
    });
}

@Override
public boolean onSupportNavigateUp() {
    onBackPressed();
    return true;
}

}

MapsActivity.java

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback {

private GoogleMap mMap;
private double latitude;
private double longitude;
private String locname;

public void setMapInfo(double lat,double lon, String loc)
{
    latitude = lat;
    longitude = lon;
    locname = loc;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng location = new LatLng(latitude, longitude);
    mMap.addMarker(new MarkerOptions().position(location).title(locname));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
}

}

Please help me solve this

C. Chan
  • 57
  • 5
  • can anyone explain why my method is wrong? – C. Chan Mar 15 '18 at 12:36
  • Follow @NileshRathod link, but to explain: in your code the instance you create in Sigiriya onClick is not the same instance as created when invoking startActivity. –  Mar 15 '18 at 12:36

0 Answers0