devs只是问题的快速解决方案。我有一个不同高速公路和地图活动的静态列表视图。现在,我想要实现的功能是,当用户单击列表中的任何高速公路时,地图上都会显示标记。同样的功能,比如,有一个已知位置的列表,比如FB,HQ,Eifel,塔等等,当用户点击按钮时,标记会移动到特定的位置。我只是想通过listview来实现这种功能。到目前为止,我所做的工作如下所示。我的列表编码:
public class Motorway_List extends AppCompatActivity {
Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
ListView list;
String[] maintitle ={
"Motorway 1","Motorway 2",
"Motorway 3",
"Motorway 9"
};
Integer[] imgid={
R.drawable.1,R.drawable.2,
R.drawable.3,R.drawable.9,
};
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_motorway__list);
Motorway_list_adapter adapter=new Motorway_list_adapter(this, maintitle,imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
if(position == 0) {
//code specific to first list item
// Toast.makeText(getApplicationContext(),"Place Your First Option Code",Toast.LENGTH_SHORT).show();
//this is where I call maps activity but it's open full map activity I want to click to open specific markers.
i = new Intent(Motorway_List.this,EmergencyMapsActivity.class);
startActivity(i);
}
else if(position == 1) {
//code specific to 2nd list item
Toast.makeText(getApplicationContext(),"Place Your Second Option Code",Toast.LENGTH_SHORT).show();
}
else if(position == 2) {
Toast.makeText(getApplicationContext(),"Place Your Third Option Code",Toast.LENGTH_SHORT).show();
}
else if(position == 3) {
Toast.makeText(getApplicationContext(),"Place Your Forth Option Code",Toast.LENGTH_SHORT).show();
}
else if(position == 4) {
Toast.makeText(getApplicationContext(),"Place Your Fifth Option Code",Toast.LENGTH_SHORT).show();
}
}
});
}
}
和我的地图编码:
public class EmergencyMapsActivity extends FragmentActivity implements OnMapReadyCallback
{
private static final int REQUEST_LOCATION_PERMISSION = 1 ;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emergency_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;
enableMyLocation();
// Add a marker in Sydney and move the camera
LatLng islamabad = new LatLng(33.692390, 73.055848);
mMap.addMarker(new MarkerOptions().position(islamabad).title("Marker in Islamabad"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(islamabad,10f));
}
private void enableMyLocation() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mMap.setMyLocationEnabled(true);
} else {
ActivityCompat.requestPermissions(this, new String[]
{Manifest.permission.ACCESS_FINE_LOCATION},
REQUEST_LOCATION_PERMISSION);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions,
@NonNull int[] grantResults) {
// Check if location permissions are granted and if so enable the
// location data layer.
switch (requestCode) {
case REQUEST_LOCATION_PERMISSION:
if (grantResults.length > 0
&& grantResults[0]
== PackageManager.PERMISSION_GRANTED) {
enableMyLocation();
break;
}
}
}
}
发布于 2021-02-02 12:59:15
public class Motorway_List extends AppCompatActivity {
Intent i;
@Override
protected void onCreate(Bundle savedInstanceState) {
ListView list;
String[] maintitle ={
"Motorway 1","Motorway 2",
"Motorway 3",
"Motorway 9"
};
Integer[] imgid={
R.drawable.1,R.drawable.2,
R.drawable.3,R.drawable.9,
};
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_motorway__list);
Motorway_list_adapter adapter=new Motorway_list_adapter(this, maintitle,imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//TODO SENDING POSITION WHICH ITEM IS CLICK to MAP ACTIVITY
i = new Intent(Motorway_List.this,EmergencyMapsActivity.class);
i.putExtra("which",position);
startActivity(i);
});
}
}第二次活动
Bundle bundle = getIntent().getExtras();
int position = (int) bundle.get("which");
if (position==0){
// motoway 1 Coding
}else if (position==1){
//motorway 2 coding
}else if(position ==2){
//motorway 3 coding
}else if(position == 3){
//motoway 4 coding
}
}https://stackoverflow.com/questions/65992197
复制相似问题