所以我有一个位置列表(在一个ListFragment内)及其相应的坐标,我想在按下其中一个时启动一个新的地图活动。如何在我的应用程序内的特定位置启动地图活动?
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), "Item: " + locations[position], Toast.LENGTH_SHORT).show();
Intent eventActivity = new Intent(getContext(), MapsActivity.class);
startActivity(eventActivity);
}
目前,我有这样,当被点击的项目之一,它显示该位置的名称,然后打开该应用程序内的地图(0,0)。我怎样才能打开地图对应的位置坐标(我有)?
感谢Ajeet Choudhary。解决了。以下解决方案
MapFragment.java
public class MapFragment extends ListFragment{
...
LatLng[] coordinates = new LatLng[] {
new LatLng(..., ...),
new LatLng(..., ...),
new LatLng(..., ...),
...
};
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new MapAdapter(getActivity(), locations);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Toast.makeText(getActivity(), "Item: " + locations[position], Toast.LENGTH_SHORT).show();
Intent eventActivity = new Intent(getContext(), MapsActivity.class);
eventActivity.putExtra("latlng", coordinates[position]);
startActivity(eventActivity);
}
}
MapsActivity.java
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker and move the camera.
LatLng location = getIntent().getExtras().getParcelable("latlng");
mMap.addMarker(new MarkerOptions().position(location).title("Marker"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(location));
}
}
您是否只有Latlng的特定位置或地址? –
两者。我有一个位置名称列表,它的物理地址和LatLng。位置名称和物理地址显示在列表中。 – KuroMajutsu
什么是MainActivity?如果这是你的课程,那么请将相应的代码加入问题中。 –