我正在创建一个我正在使用GoogleMaps的应用程序。现在,我对MapActivity和ToursActivity之间的通信有疑问。为了简化它,我的应用程序是关于乐队Metallica,我有Tours列表。当用户单击其中一个Tours时,它应该使用该位置打开一个新的Activity。我不会在这里放置ToursActivity,因为有一堆你不需要的代码。另外,如果这很重要的话,我会将所有数据保存在Firebase上。
这是我的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);
}
}
答案 0 :(得分:0)
您可以将Intent与额外参数一起使用。 例如,在ToursActivity.java上
Intent intent = new Intent(ToursActivity.this, MapActivity.class);
intent.putExtra("lat", latitude);
intent.putExtra("long", longitude);
startActivity(intent);
然后你可以在MapActivity.java的onCreate()方法上获取这些参数:
Intent intent = getIntent();
long latitude = intent.getLongExtra("lat", 0);
long longitutde = intent.getLongExtra("long", 0);
答案 1 :(得分:0)
您可以使用带有额外参数的意图。
Intent intent = new Intent(ToursActivity.this, MapActivity.class);
intent.putExtra("latitude", latitude);
intent.putExtra("longitude", longitude);
startActivity(intent);
然后你可以在MapActivity的onCreate()方法中获取你的值。
Intent intent = getIntent();
Bundle extras = intent.getExtras();
double latitude = intent.getDouble("latitude");
double longitude = intent.getDouble("longitude");