所以我有一个位置列表(在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));
}
}
答案 0 :(得分:1)
由于Latlng已经实现了Parcable类,所以将其置于意图中并发送到Map活动,从getIntent()获取That Latlng。getParcableExtra(&#34; your_key&#34;),然后将有关于MapReady的方法( GoogleMap googleMap)(如果您已经在那里实施了地图)请将您的latlng添加到地图中,请参阅this answer以获取帮助。