我的应用程序中有一个带有 2 种标记的 map fragment :我通过事件 OnMapLongClick() 添加的标记和我从 .csv 文件添加的标记。我想通过单击标记时启动新 Activity 来保存有关标记中每个坐标的一些详细信息。我遇到的问题是,使用从 .csv 文件添加的标记,它始终显示最后一个标记的位置。所以我的问题是,当我单击 .csv 时,如何获取来自 .csv 的相应标记的位置?这可以通过用 Hashmap 更改 ArrayList 来实现吗?
MapsActivity(相关代码):
public void onMapLongClick(LatLng latLng) {
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng)
.title("Introdus!");
markerOptions.draggable(true);
mMap.addMarker(markerOptions);
mMap.setOnMarkerClickListener(this);
latlngNew = latLng;
LatLng latlngNew;
}
@Override
public boolean onMarkerClick(Marker marker) {
if (counter==0) {
openDialog();
}
else
{
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
counter--;
}
return false;
}
public void openDialog() {
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog);
dialog.setTitle(R.string.default_info_title);
dialog.show();
Button bt_da = (Button) dialog.findViewById(R.id.dialog_ok);
Button bt_nu = (Button) dialog.findViewById(R.id.dialog_cancel);
bt_da.setOnClickListener(this);
bt_nu.setOnClickListener(this);
bt_nu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
}
);
bt_da.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
counter=1;
Intent intent = new Intent(MapsActivity.this, MainActivity.class);
intent.putExtra("markerLat", latlngNew.latitude);
intent.putExtra("markerLong", latlngNew.longitude);
startActivity(intent);
}
});
}
public void Upload(View view) throws IOException {
File file = new File(getExternalFilesDir(null), "date.csv");
if (file.exists()) {
InputStream instream = new FileInputStream(file);
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader reader = new BufferedReader(inputreader);
List<LatLng> latLngList = new ArrayList<LatLng>();
String line = " ";
while ((line = reader.readLine()) != null) // Read until end of file
{
double lat = Double.parseDouble(line.split(",")[0]);
double lon = Double.parseDouble(line.split(",")[1]);
latLngList.add(new LatLng(lat, lon));
mMap.setOnMarkerClickListener(this);
}
for (LatLng pos : latLngList) {
latlngNew=pos;
Marker m=mMap.addMarker(new MarkerOptions()
.position(pos)
.title("Din CSV!")
.draggable(true)
);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(pos, DEFAULT_ZOOM));
}
}
else
{
Toast.makeText(MapsActivity.this,"Nu exista fisierul!", Toast.LENGTH_SHORT).show();
}
}
public void Sterge(View view){
mMap.clear();
}
}
最佳答案
那是因为您传递给 Intent latlngNew
坐标:
intent.putExtra("markerLat", latlngNew.latitude);
intent.putExtra("markerLong", latlngNew.longitude);
由于循环而指向最后一个标记:
for (LatLng pos : latLngList) {
latlngNew=pos;
...
}
因此,为了传递到所选标记的 Intent 坐标,您应该通过添加 LatLng
参数(如 openDialog(LatLng coords)
)来修改 openDialog()
,并传递到所选标记的 openDialog(LatLng coords)
坐标。类似这样的事情:
public void openDialog(LatLng coords) {
//... your code without changes
bt_da.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v) {
counter=1;
Intent intent = new Intent(MapsActivity.this, MainActivity.class);
// pass coords instead of latlngNew:
intent.putExtra("markerLat", coords.latitude);
intent.putExtra("markerLong", coords.longitude);
startActivity(intent);
}
});
}
和onMarkerClick()
更改为
@Override
public boolean onMarkerClick(Marker marker) {
if (counter==0) {
// pass marker coordinates here:
openDialog(marker.getPosition());
}
else
{
marker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
counter--;
}
return false;
}
关于java - 从 .csv 文件加载标记时如何获取标记的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51752961/