0

I'm making an app in which I'm using GoogleMaps.Now, I have a question about communication between MapActivity and ToursActivity.To simplify it, my app is about band Metallica, and I have list of Tours. When user clicks on one of the Tours, it should open a new Activity with that location. I won't put ToursActivity here, cause there is a bunch of code that you don't need.Also, I'm keeping all my data on Firebase, if that's important.

This is my MapActivity:

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback {



private static final int REQUEST_LOCATION_PERMISSION = 10;
private GoogleMap.OnMapClickListener mCustomOnMapClickListener;

private GoogleMap mGoogleMap;
private MapFragment mMapFragment;
@BindView(R.id.lvTours) ListView lvTours;


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

    lvTours.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
}


public void initialize(){
    this.mMapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.fGoogleMap);
    this.mMapFragment.getMapAsync(this);
    this.mCustomOnMapClickListener = new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            MarkerOptions newMarkerOptions = new MarkerOptions();
            newMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.tour));
            newMarkerOptions.title("Tour");
            newMarkerOptions.snippet("It' was here!");
            newMarkerOptions.position(latLng);
            mGoogleMap.addMarker(newMarkerOptions);
        }
    };
}


@Override
public void onMapReady(GoogleMap googleMap) {
    this.mGoogleMap = googleMap;
    UiSettings uiSettings = this.mGoogleMap.getUiSettings();
    uiSettings.setZoomControlsEnabled(true);
    uiSettings.setMyLocationButtonEnabled(true);
    uiSettings.setZoomGesturesEnabled(true);
    this.mGoogleMap.setOnMapClickListener(this.mCustomOnMapClickListener);
   goToLocation(33.835293 , -117.914505);
}


public void goToLocation(double lat, double lng){

    LatLng latLng = new LatLng(lat, lng);


    CameraPosition position = CameraPosition.builder()
            .target(latLng)
            .zoom(16f)
            .bearing(0.0f)
            .tilt(0.0f)
            .build();

    mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(position),null);
}




private boolean hasLocationPermission() {

    String LocationPermission = android.Manifest.permission.ACCESS_FINE_LOCATION;
    int status = ContextCompat.checkSelfPermission(this, LocationPermission);
    if (status == PackageManager.PERMISSION_GRANTED) {

        this.mGoogleMap.setMyLocationEnabled(true);

        return true;

    }
    return false;
}

private void requestPermission() {
    String[] permission = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
    ActivityCompat.requestPermissions(MapActivity.this, permission, REQUEST_LOCATION_PERMISSION);

}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 1
    You can use Intent and Bundle to open the new activity and pass data along. [Check this answer here](https://stackoverflow.com/a/819427/4329778) – Stephen Dec 22 '17 at 15:44
  • Possible dupe of https://stackoverflow.com/questions/21393287/how-should-i-communicate-between-activities – Zoe Dec 22 '17 at 15:56

2 Answers2

1

You can use Intent with extra parameters. For example, on ToursActivity.java

Intent intent = new Intent(ToursActivity.this, MapActivity.class);
intent.putExtra("lat", latitude);
intent.putExtra("long", longitude);
startActivity(intent);

Then you can get get these parameters on onCreate() method of MapActivity.java:

Intent intent = getIntent();
long latitude = intent.getLongExtra("lat", 0);
long longitutde = intent.getLongExtra("long", 0);
Eli Zhang
  • 44
  • 1
  • 8
0

You can use intent with extra parameters.

Intent intent = new Intent(ToursActivity.this, MapActivity.class);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
startActivity(intent);

Then you can get your values in onCreate() method of MapActivity.

Intent intent = getIntent();
Bundle extras = intent.getExtras();
double latitude = intent.getDouble("latitude");
double longitude = intent.getDouble("longitude");
Karl Ghosn
  • 117
  • 3
  • 13
  • Thanks for quick answer, but I have one more question. When I write parameters latitude and longitude, they are getting red cause those parameters are from MapActivity.Now, in my ToursFragment(not ToursActivity, my mistake) is a method onItemClick, in which I want Intent, and when you click on it, it should open a new Activity with a map –  Dec 23 '17 at 13:41
  • Can you put the onItemClick code so I can see what you mean? – Karl Ghosn Dec 23 '17 at 14:03
  • @OnItemClick(R.id.lvTours) public void onItemClick(View v,int position) { Intent intent = new Intent(v.getContext(),MapActivity.class); intent.putExtra("latitude", lat); intent.putExtra("longitude", lng ); startActivity(intent); } } –  Dec 23 '17 at 14:06
  • I don't know if I understand your question correctly but here goes. In MapActivity when you're getting the intent, you can name the parameters whatever you want. If this is not the answer to your question please clarify what you mean by "red" and where this is happening. – Karl Ghosn Dec 23 '17 at 15:50
  • I'll try to explain better. I have list of Tours on Firebase. That list contains next parameters: coord, title, id and year. I know how to show that data in my Fragment and all that. But, I did what you said(with Intent), and the problem is when I enter my parameters (ToursFragment.this, MapActivitiy) those two get red underline and it says: Cannot resolve constructor. Now, other problem is in the second line of code.When I enter longitude, that's getting red. –  Dec 23 '17 at 16:08
  • Intent intent = new Intent(getContext() , MapActivity.class); intent.putExtra("latitude", latitude); intent.putExtra("longitude", longitude); startActivity(intent); – Karl Ghosn Dec 23 '17 at 16:45